change the keyboard layout after a button click
Asked Answered
L

4

6

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.

Lituus answered 28/2, 2012 at 10:18 Comment(0)
X
11

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

Xantha answered 8/3, 2012 at 8:15 Comment(4)
thanks aaronsnoswell for your help , i will test your programm now and tell you back if it is working for me :) +1Lituus
Small improvement: int inputType = txtSearch.getInputType(); txtSearch.setInputType(InputType.TYPE_NULL); txtSearch.setImeOptions(EditorInfo.IME_ACTION_DONE); txtSearch.setInputType(inputType);Papacy
This is almost awesome. Problem is that it moves the cursor back to the beginning of the line. Thanks again, Android.Hydroscope
This works for me and didn't observe issue with moving cursor back to beginning of the line.Cutaneous
H
1

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.

Hydroscope answered 18/8, 2014 at 22:34 Comment(0)
I
0

try this RadioGroup. It will work definitely. OR use link

https://gist.github.com/475320

// This will get the radiogroup
RadioGroup rGroup = (RadioGroup)findViewById(r.id.radioGroup1);
// This will get the radiobutton in the radiogroup that is checked
RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(rGroup.getCheckedRadioButtonId());


// This overrides the radiogroup onCheckListener
rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
    public void onCheckedChanged(RadioGroup rGroup, int checkedId)
    {

    switch(checkedId){
        case R.id.btnA:
            txtSearch.setImeOptions(EditorInfo.IME_ACTION_DONE);
            break;
        case R.id.btnB:
             txtSearch.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
            break;enter code here
        default:
            break;
    }
       txtSearch.setCloseImeOnDone(true); // close the IME when done is pressed
    }
});
Indra answered 5/3, 2012 at 12:38 Comment(2)
there is no such method setCloseImeOnDone(true); which level of androdi sdk you have used ? i'm using api level 10Lituus
best example is here check this link at least once...gist.github.com/475320Indra
B
0

try use class handler to update views dynamically.

from: Change Button Text on Android

Bunni answered 5/3, 2012 at 19:37 Comment(1)
my code is running on the uiThread, so i don't need to use the handler to update the UI KeyboardLituus

© 2022 - 2024 — McMap. All rights reserved.