Android: How to open keyboard for editing EditText when click button?

48,697

Solution 1

Thanks guys for your help :) I used all suggestions that you gave me, searched and tested a lot of other scripts and finally my code is working :)

Here is my final code:

InputEditText = (EditText) findViewById(R.id.InputText);

public void InputData() {

        /* Keyboard Button Action */
        KeyboardButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Log.v(TAG, "On Keyboard Button click event!");

                InputEditText.requestFocus();
                InputEditText.setFocusableInTouchMode(true);

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(InputEditText, InputMethodManager.SHOW_FORCED);

            }

        });

}

It may be useful for someone :) Thank you!

Solution 2

The design you've described isn't recommended. You're violating the focusable attribute's purpose which is not to control whether the user can alter the text in a EditText component.

If you plan to provide an alternative input method because the use case seems to require this (e.g. you allow only a certain set of symbols in the editable text field) then you should probably disable text editing altogether for the time the user isn't allowed to change the value of the field.

Declare your editable field:

<EditText
    android:id="@+id/edit_text_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" />

notice that its focusable attribute is left with the default value. That's ok, we'll handle that later. Declare a button which will start editing process:

<Button
    android:id="@+id/button_show_ime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start editing" />

Now, in your Activity declare:

private EditText editText2;
private KeyListener originalKeyListener;
private Button buttonShowIme;

And in onCreate() do this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ime_activity);

    // Find out our editable field.
    editText2 = (EditText)findViewById(R.id.edit_text_2);
    // Save its key listener which makes it editable.
    originalKeyListener = editText2.getKeyListener();
    // Set it to null - this will make the field non-editable
    editText2.setKeyListener(null);

    // Find the button which will start editing process.
    buttonShowIme = (Button)findViewById(R.id.button_show_ime);
    // Attach an on-click listener.
    buttonShowIme.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Restore key listener - this will make the field editable again.
            editText2.setKeyListener(originalKeyListener);
            // Focus the field.
            editText2.requestFocus();
            // Show soft keyboard for the user to enter the value.
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(editText2, InputMethodManager.SHOW_IMPLICIT);
        }
    });

    // We also want to disable editing when the user exits the field.
    // This will make the button the only non-programmatic way of editing it.
    editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // If it loses focus...
            if (!hasFocus) {
                // Hide soft keyboard.
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(editText2.getWindowToken(), 0);
                // Make it non-editable again.
                editText2.setKeyListener(null);
            }
        }
    });
}

I hope the code with all the comments is self-explanatory. Tested on APIs 8 and 17.

Solution 3

Try this :

        final EditText myedit2 = (EditText) findViewById(R.id.myEditText2);

        Button btsmall = (Button) findViewById(R.id.BtSmall);
        btsmall.setOnClickListener(new OnClickListener() {              
            @Override
            public void onClick(View arg0) {
                myedit2.requestFocus();
            }
        });

Solution 4

For those that uses fragments you can use InputMethodManager that way:

InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
            }

Full Code:

btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editText.setFocusable(true);
                editText.setFocusableInTouchMode(true);
                editText.requestFocus();
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
                }
            }
        });

Solution 5

I Have Work this code for open a keybord when button click. Like .

btn1 = (Button) findViewById(R.id.btn1);
edt1 = (EditText) findViewById(R.id.edt1);
btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            edt1.requestFocus();
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(edt1, InputMethodManager.SHOW_IMPLICIT);
        }
    });

its completed work.

Share:
48,697
Dodo
Author by

Dodo

PHP Developer, Android Developer, Lunux Administration and others...

Updated on July 27, 2022

Comments

  • Dodo
    Dodo almost 2 years

    my case is: I have one EditText field that is with disabled focus. Beside EditText field I has two buttons for Input methods. So I want when click first button: to open soft keybord and edit text in EditText field. I try many ways with:

    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
    

    and doesn't work for me. Only way to open soft keyboard is:

    toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)

    but there is no way to edit info from EditText field.

    May you suggest me how to open keyboard and edit text for some EditText when click button. Thanks a lot!

    Edited:

    So, EditText is not focusable be default. When I click Keyboard button - should be focusable, then show me soft keyboard to enter text and appear in EditText. Other method to insert is A-B-C button which not required keyboard. It will be something like Morse code input - touch and hold A-B-C button :) I'll try suggested example to implement in my case. Thank you guys :)

    my case

  • andr
    andr about 11 years
    I see. try it and if you'll have issues maybe I'll manage to fix it the way you'd like it to be. but don't use the focusable attribute - it means the user won't be able to copy the field contents for example, which removes natural functionality of your UI. users don't like limitations like that.
  • Dodo
    Dodo about 11 years
    Thanks a lot for your suggestions :) Finally I made it with your and some other example. Will share my code in answer.
  • Sujay U N
    Sujay U N over 6 years
    Which is KeyboardButton in ur case ?
  • TheCoderGuy
    TheCoderGuy about 5 years
    @Dodo I found this very helpfull for mein app to thanks a lot to you man, keep it up.