Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change the keyboard layout after a button click

I'm developping an android app , and i have an EditText and a two RadioButtons (A and B ),

what i'm trying to do is :

When RadioButton A is checked , i want to change the keyboard layout to display it with the Done button , When the RadioButton B is checked, i want to change the keyboard layout to display it with the Search Button .

I've tried to change the IMEOptions of my EditText like this , but it still doesn't work :

NB : the keyboard is already visible, what i want to do is just modify the button Search with the button Done in each case of the two radioButtons

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(btnA.isChecked() ) { 
        txtSearch.setImeOptions(EditorInfo.IME_ACTION_DONE);
//      txtSearch.invalidate(); 
    }
    else {
        txtSearch.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
//      txtSearch.invalidate();
    }   
}

any ideas about how to do that ??

Thanks in advance.

like image 380
Houcine Avatar asked Feb 28 '12 10:02

Houcine


2 Answers

What Android version are you targeting? I'm unable to post a comment sorry (new account), but am currently doing up a testcase to answer your question.

EDIT: Ok, I've figured it out. After a bit of googling (see this issue) and coding, I found that the imeOptions appear to be cached/tied to the input method. I'm not sure if this is a bug or intentional functionality. To switch the keyboard when the user taps either radio button, first make sure the inputType is set for your EditText (android:inputType="text"), then use the following in your onCreate method:

final RadioGroup btn_group = (RadioGroup) findViewById(R.id.btn_group);
final RadioButton btnA = (RadioButton) findViewById(R.id.btnA);
final RadioButton btnB = (RadioButton) findViewById(R.id.btnB);

btn_group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        final EditText txtSearch = (EditText) findViewById(R.id.edit_text);

        txtSearch.setInputType(InputType.TYPE_NULL);

        if(btnA.isChecked()) {
            txtSearch.setImeOptions(EditorInfo.IME_ACTION_DONE);
        } else {
            txtSearch.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        }

        txtSearch.setInputType(InputType.TYPE_CLASS_TEXT);
    }
});

Note the nulling out and re-setting of the InputType.

Finally, please be aware that many popular keyboard implementations don't give a damn what you set the imeOptions to, so don't rely on this functionality in your app. Swype for example;

In conclusion (see what I did there?), not to be outdone, I've bundled up my test program. You can find it here: http://dl.dropbox.com/u/52276321/ChangeKeyboardTest.zip

like image 74
aaronsnoswell Avatar answered Sep 20 '22 01:09

aaronsnoswell


Based on standing on the shoulders of giants, here's what I came up with. Thanks to @aaronsnoswell.

private void imeSetup(boolean isDoneOk){
    if (isDoneOk!=lastIsDoneOk) {
        int inputType = editText.getInputType();
        int selectionStart = editText.getSelectionStart();
        editText.setInputType(InputType.TYPE_NULL);
        if (isDoneOk)
            editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
        else
            editText.setImeOptions(EditorInfo.IME_ACTION_NEXT);
        editText.setInputType(inputType);
        editText.setSelection(selectionStart);
        lastIsDoneOk = isDoneOk;
    }
}

Without the lastIsDoneOk shenanigans I went into an infinite loop. YMMV because I'm calling this from within a TextWatcher, so you may not need it. Without keeping track of selectionStart, Android brings the cursor back to the start.

like image 25
JohnnyLambada Avatar answered Sep 19 '22 01:09

JohnnyLambada