Show keyboard automatically

59,449

Solution 1

I think it's a bug or a feature which tries to present the whole activity to you without obscuring it with the soft keyboard at first. I've searched once for information regarding that but unfortunately found nothing coming from a really reliable source.

Anyway, to show the soft keyboard you can do this:

EditText editText = (EditText)findViewById(R.id.edit_text_id);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

I've also seen this code that should force the soft keyboard to become visible just after activity start, but never tried it:

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

And if you want to hide soft keyboard you can do this:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

Hope that helps.

Edit:

For a DialogFragment this should work: in the onCreateView() method do this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_id, container);
    EditText editText = (EditText)view.findViewById(R.id.edit_text_id);

    // show soft keyboard
    editText.requestFocus();
    getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);

    return view;
}

Solution 2

Open the Android Manifest file.

Look for the activity tag like this

<activity  
        android:name="com.example.framework.MainActivity"
        android:label="@string/app_name" 
        android:windowSoftInputMode="stateVisible"       //Add this line    
         >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Add the line android:windowSoftInputMode="stateVisible" as shown above

Solution 3

I know this has already been answered, but I found a way to do the accepted answer in onCreateDialog instead of just in onCreateView. When you finish with the builder, before you return do the following:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());  
// blah blah blah do builder stuff here like setTitle, setView, etc

Dialog d = builder.create();

Here's the important part:

d.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return d;

Solution 4

add to onCreate or onStart();

myView.requestFocus();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

Solution 5

Try this

@Override
public void onResume() {
   super.onResume();
   final View v = getDialog().findViewById(R.id.edit_text_id);
   v.post(new Runnable() {
      @Override
      public void run() {
        v.requestFocus();
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
      }
   });
 }
Share:
59,449
Foenix
Author by

Foenix

I am compleatly new in android and java. Thank you all very much for you help!

Updated on July 28, 2022

Comments

  • Foenix
    Foenix over 1 year

    Please explain me the issue about soft keyboard. For example, I have an EditText on my activity or dialogfragment or fragmentactivity, whatever. here it is:

    <EditText
        android:id="@+id/edPswrd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword" >
    
        <requestFocus />
    </EditText>
    

    When it shows for the first time I do not see the soft keyboard and have to press editText for it to receive focus and the keyboard appears. Another activities are different, when it appears on screen the keyboard are loaded without any help. I thought that

    < requestFocus />

    means that EditText will be focused and keyboard will appear, but I am wrong.

    How should I manage what component will receive focus and keyboard will automatically appear.

  • Foenix
    Foenix about 11 years
    ok, thank you, it worked for the activity, and it didn't suit for dialogs.
  • Foenix
    Foenix about 11 years
    I have got a dialogfragment and did this: edPswrd.requestFocus(); getActivity().getWindow().setSoftInputMode(WindowManager.L‌​ayoutParams.SOFT_INP‌​UT_STATE_VISIBLE); It doesn't work :(
  • Foenix
    Foenix about 11 years
    I tried, but not one of method here doesn't work with my dialogfragment. I did this: InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)‌​; imm.showSoftInput(edPswrd, InputMethodManager.SHOW_IMPLICIT); And it doesn't work :(
  • Konrad Winkowski
    Konrad Winkowski about 11 years
    which part, the focus on the dialogfrag or the keyboad comming up ?
  • Foenix
    Foenix about 11 years
    there is still no keyboard on screen when dialog appears
  • Foenix
    Foenix about 11 years
    thank you. I imported android.view.ViewGroup.LayoutParams but still there is a error SOFT_INPUT_STATE_VISIBLE cannot be resolved or is not a field
  • Foenix
    Foenix about 11 years
    I also did this: getActivity().getWindow().setSoftInputMode(WindowManager.Lay‌​outParams.SOFT_INPUT‌​_STATE_VISIBLE); like in advise below but keyboard still doesn't appear
  • Foenix
    Foenix about 11 years
    also I just tried you "hide keyboard" on other activity, wrote this InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SE‌​RVICE); imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); in onCreate() but keyboard appears :(
  • Foenix
    Foenix about 11 years
    It works like this: getDialog().getWindow().setSoftInputMode(WindowManager.Layou‌​tParams.SOFT_INPUT_S‌​TATE_VISIBLE); Thank you!
  • MidasLefko
    MidasLefko almost 9 years
    I got it to work in onCreateDialog also. See my response
  • sepehr
    sepehr over 7 years
    what about viewpager?!
  • JerabekJakub
    JerabekJakub over 7 years
    I had to use SOFT_INPUT_STATE_ALWAYS_VISIBLE to show keyboard (Nexus 5). Anyway, thanks for the solution in onCreateView() method.
  • VIN
    VIN over 7 years
    How do you get the keyboard to disappear after you dismiss the dialog? The keyboard stays up even after dismissing the dialog if you use getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_I‌​NPUT_STATE_VISIBLE);‌​.
  • VIN
    VIN over 7 years
    How do you dismiss the keyboard if you use this method of showing the keyboard?
  • MidasLefko
    MidasLefko over 7 years
    same as always. Check this and this