How to change background color or theme of keys dynamically in Custom Keyboard Android

后端 未结 2 974
栀梦
栀梦 2021-01-06 16:04

I am working on Custom keyboard app I need to set or change background theme or color of keyboard .their setting.xml view in my app where user can select different backgrou

相关标签:
2条回答
  • 2021-01-06 16:28

    When the keyboard is shown, the framework calls onStartInputView. You can program that function to look at the values of the shared preferences and set the colors/themes appropriately on the keyboard view.

    0 讨论(0)
  • 2021-01-06 16:29

    Get solution to change the layout of custom keyboard.

    When keyboard first time load onCreateInputView() is called. After that when keyboard open onStartInputView(EditorInfo attribute, boolean restarting) called every time.

    So, now layout of keyboard(Theme) have to define in onCreateInputView() Like This

    public KeyboardView mInputView;
    public View onCreateInputView() {
    
        SharedPreferences pre = getSharedPreferences("test", 1);
        int theme = pre.getInt("theme", 1);
    
        if(theme == 1)
        {
            this.mInputView = (KeyboardView) this.getLayoutInflater().inflate(R.layout.input, null);
        }else
        {
            this.mInputView = (KeyboardView) this.getLayoutInflater().inflate(R.layout.input_2, null);
    
        }
        this.mInputView.setOnKeyboardActionListener(this);
        this.mInputView.setKeyboard(this.mQwertyKeyboard);
        return this.mInputView;
    }
    

    and do this in onStartInputView

    public void onStartInputView(EditorInfo attribute, boolean restarting) {
        super.onStartInputView(attribute, restarting);
    
        setInputView(onCreateInputView());
    }
    
    0 讨论(0)
提交回复
热议问题