6

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

Dodo
  • 469
  • 1
  • 4
  • 14
  • why is your `EditText` not focusable in the first place? how did you accomplished this? in XML? using what attributes exactly? – andr Feb 11 '13 at 16:01
  • 1
    because this editext will be unnormal edittext. There it will be text inputed when you click Keyboard button and show soft keyboard or some other way which I will implement latter. I disabled in XML in this way: `android:focusable="false"` – Dodo Feb 11 '13 at 16:18

7 Answers7

24

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!

abitofcode
  • 3,048
  • 1
  • 16
  • 11
Dodo
  • 469
  • 1
  • 4
  • 14
13

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.

andr
  • 15,970
  • 10
  • 45
  • 59
  • 1
    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. – andr Feb 12 '13 at 08:26
  • Thanks a lot for your suggestions :) Finally I made it with your and some other example. Will share my code in answer. – Dodo Feb 12 '13 at 13:48
5

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();
            }
        });
Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37
1

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.

Hardik Parmar
  • 712
  • 2
  • 13
  • 28
1
LinearLayout ll_about_me =(LinearLayout) view.findViewById(R.id.ll_about_me);
    ll_about_me.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub

            mEtEmpAboutYou.requestFocus();
            mEtEmpAboutYou.setFocusableInTouchMode(true);

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

            return true;
        }
    });
Monu Gupta
  • 153
  • 1
  • 8
1

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);
                }
            }
        });
Mahmoud Ayman
  • 1,157
  • 1
  • 17
  • 27
0

I hope this help

EditText txt_categorie = findViewById(R.id.txt_categorie);
txt_categorie.requestFocus();
Mohamed Ben Romdhane
  • 1,005
  • 3
  • 11
  • 22