7

I want show keyboard when my EditText receives focus. I tried many methods but nothing not helped. I tried: 1.

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

whith different flags.

2.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

  1. <requestFocus />

4.

 editText.setOnFocusChangeListener(new OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    editText.post(new Runnable() {
                        @Override
                        public void run() {
                            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
                        }
                    });
                }
            });
            editText.requestFocus();

4 method is fork but it bad solution. Thus it is written here Show soft keyboard automatically when EditText receives focus

Before, I used the method 2 and it worked. but now no longer. and I created a blank Project and it does not work, none of the methods

UPDATE:

<style name="Theme.TransparencyDemo" parent="android:Theme.Light.NoTitleBar">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
    </style>
ip696
  • 6,574
  • 12
  • 65
  • 128
  • Possible duplicate of [How to show soft-keyboard when edittext is focused](http://stackoverflow.com/questions/5105354/how-to-show-soft-keyboard-when-edittext-is-focused) – raukodraug May 20 '17 at 22:08

2 Answers2

8

You can add flags to your activity as well which will show the keyboard automatically

<activity name="package.ActivityName" android:windowSoftInputMode="stateVisible"/>

this is mostly useful if you expect the focus to be applied when the activity launches

Also you can use in Fragment:

InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

or in Activity

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Mithun
  • 2,075
  • 3
  • 19
  • 26
kandroidj
  • 13,784
  • 5
  • 64
  • 76
1

Use WindowManager instead of InputMehtodManager inside onFocusChange listener of edittext, As Its reliable.

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
    if (hasFocus) {
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
   } });
Jai
  • 1,974
  • 2
  • 22
  • 42
  • I can not understand why does not work the usual way, without listeners – ip696 Aug 03 '15 at 03:40
  • @ip696 : Because may be you are using other components which get focused earlier than edittext. So its depends on hierarchy you have designed with your layout. – Jai Aug 03 '15 at 03:43
  • hierarchy ? you mean in order of XML? does it matter? – ip696 Aug 03 '15 at 03:44
  • @ip696 : Yeah, I mean the layout design of XML, It matters. – Jai Aug 03 '15 at 03:45
  • New Projects (test) your method worked. but not in the working. I will deal with XML thank! – ip696 Aug 03 '15 at 03:47
  • i copy layout from my project to test project and keyboard showed. But in the project do not show. – ip696 Aug 03 '15 at 04:17
  • @ip696 : Make sure that there is no any depedancy on your activity code on which you are using that layout. And which theme are you applying to your activity? – Jai Aug 03 '15 at 04:21
  • I updated the question of added subject. what do you mean "depedancy on your activity"? – ip696 Aug 03 '15 at 04:42
  • @ip696 : First can you confirm that your edittext setOnFocusChange listener is working or not? Keep log and try to check it first. – Jai Aug 03 '15 at 04:46
  • To be honest I do not understand you – ip696 Aug 03 '15 at 04:53
  • @ip696 : Post your activity code that you have written – Jai Aug 03 '15 at 04:56