filipst
filipst

Reputation: 1573

Android Custom Keyboard changing from Activity

I have made an Custom Keyboard app:

public class SimpleIME extends InputMethodService implements KeyboardView.OnKeyboardActionListener
{
    private KeyboardView kv;
    private Keyboard keyboard;
    private boolean caps = false;

    @Override
    public View onCreateInputView()
    {
        kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
        keyboard = new Keyboard(this, R.xml.qwerty);
        kv.setKeyboard(keyboard);
        kv.setOnKeyboardActionListener(this);
        return kv;
    }
}

XML:

<android.inputmethodservice.KeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:keyBackground="@drawable/key_image"
android:background="@drawable/keyboard_image"
android:keyPreviewLayout ="@layout/preview"
/>

Now, there is a settings key, and when user presses it, new Activity opens. This Activity has two buttons. First one should change keyboards background image, and the second one should change button background image.

I was trying some things, but I can't manage to do it. Can someone please tell me how should I do that?

Thank you.

Upvotes: 2

Views: 1051

Answers (1)

filipst
filipst

Reputation: 1573

I have found a solution. It is easy to change the keyboard by changing its theme from activity:

themePreferencesEditor.putInt(THEME_KEY, 2);
themePreferencesEditor.commit();

And then in IME:

@Override
    public View onCreateInputView()
    {
        pre = getSharedPreferences("CUSTOM_KEYBOARD_PREFERENCES", 1);
        theme = pre.getInt(THEME_KEY, 1);
        switch (theme)
        {
            case 1:
                kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
                break;
            case 2:
                kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard1, null);
                break;
            case 3:
                kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard2, null);
                break;
            case 4:
                kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard3, null);
                break;
            case 5:
                kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard4, null);
                break;
            case 6:
                kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard5, null);
                break;
            default:
                kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
                break;
        }
        keyboard = new Keyboard(this, R.xml.qwerty);
        kv.setKeyboard(keyboard);
        kv.setOnKeyboardActionListener(this);
        return kv;
    }

    @Override
    public void onStartInputView(EditorInfo info, boolean restarting)
    {
        super.onStartInputView(info, restarting);
        setInputView(onCreateInputView());
    }

Upvotes: 2

Related Questions