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

牧云@^-^@ 提交于 2019-11-30 23:16:20

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());
}

When the keyboard is show, the framework calls onStartInputView. This function should look at the values of the shared preferences and set the colors/themes appropriately on the keyboard view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!