Hamid
Hamid

Reputation: 3052

Cannot change Enter Key label for Custom keyboard in Android

I'm designing a custom keyboard for Android. I want to have my custom label for ENTER key for some fields in my application. I used sample SoftKeyboard project for developing my keyboard. What I tried so far: 1- In one of my activities I have an EditText with the following attributes:

<EditText
    android:id="@+id/password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:imeActionId="@+id/action_sign_in"
    android:imeActionLabel="@string/sign_in"

    android:inputType="textPassword" />

If I use Native Android keyboard it shows "Sign In" on my enter key but if I use my custom keyboard it shows the default value of enter key in the following statement:

In LatinKeyboard.java

void setImeOptions(Resources res, int options)
    {
        if (mEnterKey == null)
        {
            return;
        }

        switch (options & (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION))
        {
            case EditorInfo.IME_ACTION_GO:
                mEnterKey.iconPreview = null;
                mEnterKey.icon = null;
                mEnterKey.label = res.getText(R.string.label_send_key);
                break;
            case EditorInfo.IME_ACTION_NEXT:
                mEnterKey.iconPreview = null;
                mEnterKey.icon = null;
                mEnterKey.label = res.getText(R.string.label_next_key);
                break;
            case EditorInfo.IME_ACTION_SEARCH:
                mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_search);
                mEnterKey.label = null;
                break;
            case EditorInfo.IME_ACTION_SEND:
                mEnterKey.iconPreview = null;
                mEnterKey.icon = null;
                mEnterKey.label = res.getText(R.string.label_send_key);
                break;
            case R.id.action_sign_in:
                mEnterKey.iconPreview = null;
                mEnterKey.icon = null;
                mEnterKey.label = res.getText(R.string.sign_in);
                break;
            default:
                mEnterKey.label = res.getText(R.string.label_send_key);
                mEnterKey.icon = null;
                break;
        }

    }
}

I would appreciate if someone can help me to solve this issue.

Upvotes: 0

Views: 1196

Answers (1)

Hamid
Hamid

Reputation: 3052

Finally I found the solution. Instead of passing int options, we have to pass EditorInfo attribute. We pass it like bellow

@Override
    public void onStartInput(EditorInfo attribute, boolean restarting)
    {
        super.onStartInput(attribute, restarting);
         ...
        yourSoftKeyboard.setImeOptions(getResources(), attribute);
}

Then we implement setImeOptions like bellow:

void setImeOptions(Resources res, EditorInfo ei)
    {
        if (enterKey == null)
        {
            return;
        }

        switch (ei.imeOptions & (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION))
        {
            case EditorInfo.IME_ACTION_SEND:
                enterKey.iconPreview = null;
                enterKey.icon = null;
                enterKey.label ="Send";
                break;
            case EditorInfo.IME_ACTION_GO:
                enterKey.iconPreview = null;
                enterKey.icon = null;
                enterKey.label ="Go";
                break;
            case EditorInfo.IME_ACTION_NEXT:
                enterKey.iconPreview = null;
                enterKey.icon = null;
                enterKey.label = "Next";
                break;
            case EditorInfo.IME_ACTION_SEARCH:
                enterKey.icon = res.getDrawable(R.drawable.sym_keyboard_search);
                enterKey.label = null;
                break;
            default:
                enterKey.iconPreview = null;
                enterKey.label = "Enter";
                enterKey.icon = null;
                break;
        }

        if (ei.actionLabel != null)
        {
            enterKey.iconPreview = null;
            enterKey.icon = null;
            enterKey.label = ei.actionLabel;
        }
    }

Upvotes: 2

Related Questions