Show soft keyboard automatically when EditText receives focus
Posted by David Chandler on May 2, 2012
I’ve been unpleasantly surprised at how difficult it is to find info on showing or hiding the Android soft keyboard, so I’m putting a few code snippets out here for posterity.
Problem
I have a DialogFragment with an EditText. When the dialog is shown, I want to immediately focus the EditText and show the keyboard.
Solution 1 (recommended)
Set the dialog Window’s soft input method.
public class EditNameDialog extends DialogFragment { private EditText editText; public EditNameDialog() { // Empty constructor required for DialogFragment } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_edit_name, container); editText = (EditText) view.findViewById(R.id.txt_yourName); // Request focus and show soft keyboard automatically editText.requestFocus(); getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); return view; } }
As an alternative to editText.requestFocus(), you can use the <requestFocus /> tag in the fragment’s XML layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/edit_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical" > <TextView android:id="@+id/lbl_yourName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="What is your name?" /> <EditText android:id="@+id/txt_yourName" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" > <requestFocus /> </EditText> </LinearLayout>
Setting the Window’s soft input method is definitely the easiest way to show the keyboard. However, in some circumstances, you may need to “force” it open or shut by invoking the InputMethodManager directly.
Solution 2
Invoke the InputMethodManager service.
This approach is more complicated because the input method manager ignores keyboard requests unless the EditText and the Window it’s in both have focus. So upon entering an Activity / Dialog, you need to wait until focus has settled down before you request the keyboard. You can do this by calling the InputMethodManager service in an OnFocusChangeListener, but Views can get focus before the Window itself does, so you have to wait for Window focus. There is no way that I can find to add an OnFocusChangeListener on the Window object itself, but listening for View focus and posting a Runnable seems to work:
... 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(); ...
This solution is not recommended because it may be buggy, but illustrates using post() to wait for the UI to settle, which may be useful in other circumstances. Note that this solution won’t work with the requestFocus tag in the layout XML because in that case, the EditText receives focus before the OnFocusChangeListener has been registered. Therefore, in the example above, we programmatically request focus after adding the listener.
Bad solutions
You can set the attribute android:windowSoftInputMode=”stateVisible” for your activity in AndroidManifest.xml. However, in the case of a DialogFragment, that will show the keyboard in the Activity’s window, which is behind the Window associated with the Dialog in the DialogFragment.
In order to get around possible Window / View focus bugs, you can use postDelayed() with a number of milliseconds to post a Runnable; however, hard-coded delays are never recommended because they may introduce unpredictable behavior under different conditions / different devices.
Summary
Ordinarily, the soft keyboard default behavior should be sufficient; however, when you programmatically focus on an EditText, you can set the Window’s soft input method to show the keyboard.
David Chandler said
More info on using DialogFragments can be found in my post on the Android Developers Blog: http://android-developers.blogspot.com/2012/05/using-dialogfragments.html
Chris Raley said
David, your post’s technique is great for showing the keyboard; however in using it, I have experienced in my app that the window will no longer resize/pan based on the windowSoftInputMode specified in my manifest. I assume this is because by calling setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE) that I am overriding what was set in the manifest. Is there a way to achieve showing the keyboard while still allowing the window to resize? My problem is that if I show a dialog fragment in landscape mode on a phone, the edit text of the dialog is hidden by the keyboard. If I don’t call setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE) when creating my fragment, the window pans correctly. Is there a way to do both?
Chris Raley said
I found the answer to my own problem after reading the documentation for windowSoftInputMode. The first time reading it I didn’t notice the part about combining one “state” value with one “adjust” value. [Slap forehead]. In case others are interested, you can do the following to show and pan:
setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE | LayoutParams.SOFT_INPUT_ADJUST_PAN )
David Chandler said
Thanks for sharing, Chris. Long live bitmasking!
David M. Karr said
The requirement to have a no-args constructor is often misinterpreted to mean that you have to define one. If you don’t define any constructor, the “default” constructor is implicitly defined, which is a no-args constructor. In addition, calling it an “empty” constructor is also off, because it doesn’t matter whether the body of the constructor is empty. What matters is that there is a no-args constructor defined. It doesn’t matter what’s in the body of the constructor.
David Chandler said
Thanks, David, I agree that no-args is a better word than empty. In this case, an earlier iteration of EditNameDialog() also defined a constructor with arguments, hence the need for the explicit no-args constructor at that time. But as you point out, it’s not needed now.
geezenslaw said
Hi Turbomanage enthusiasts, though this blog entry is somewhat dated it is the closest reference to I have found to a somewhat similar problem.
I’m not using DialogFragment but just a plain ol’ Fragment (POF).
And, I am employing actiongo to get the softkeyboard to navigate the displayed EditText input fields.
My problem is how to persist in some local variable the newly input data?
In the onCreateView() I tried the findViewById() but it does not compile.
I also created a new onCreate() but likewise won’t compile.
Howto do something like the following with soft keyboard user input?:
Of course in a regular Activity, Intent and View the statement above does compile.
Any hints, Rants or Raves welcomed along with advice.
David.
Sanjaya Verma said
I am not able to enter text in EditText ,used in fragment class. Plz give the solution
My code is here-
public class CreateNewFolder extends Fragment {
/*
* @Override public void onResume() { // TODO Auto-generated method stub
* super.onResume(); description1= edtFldrDesp.getText().toString().trim();
* title1=edtFldrName.getText().toString().trim(); }
*/
private static Button btnSave, btnCancel;
private static EditText edtFldrName, edtFldrDesp;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
public ProgressDialog dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), “CreateNewFolder class”,
Toast.LENGTH_LONG).show();
View view = inflater
.inflate(R.layout.create_newfolder,
container, false);
btnSave = (Button) view.findViewById(R.id.btn_save);
btnCancel = (Button) view.findViewById(R.id.btn_cancel);
edtFldrName = (EditText) view.findViewById(R.id.edt_folderName);
edtFldrDesp = (EditText) view.findViewById(R.id.edt_folderDescription);
return view;
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
btnSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
title1 = edtFldrName.getText().toString();
description1 = edtFldrDesp.getText().toString();
Query Here –
??? I am not able to enter text in edtFldrName and edtFldrDesp . when I am Trying to enter text control will
goes to the My Folder tab(Because it is the initial tab under which I am doing operations) ????
}
}
}
}
sanjay verma said
HI Friends, I have Resolved The Problem MySelf
use this code for getting the focus on editText . then you will able to enter text into editText . Enjoy..
userNameEditText.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
userNameEditText.requestFocusFromTouch();
return false;
}
});
hela said
Best and easiest answer is that.
add
android:windowSoftInputMode=”stateHidden”
in activity tag where in androidmanifest.xml
here details :
http://www.erhancetin.com.tr/2013/09/android-activity-ilk-acls-edittext.html
Polar B said
Thank you! Solution 2 works for me.
ajay said
i am facing problem with next click of soft keyboard … i have three grid view in main screen and i am using custom adapter to set data to three grid view …. all this grid holding edit text only . when i click next button of last edittext of first grid view not focusing edittext of second gridview ….. every edittext holding only integer data only
Soft keyboard not showing on phonegap - Popular Cordova Questions said
[…] https://turbomanage.wordpress.com/2012/05/02/show-soft-keyboard-automatically-when-edittext-receives-… […]
Fallen Angel said
Your solution is what I’m looking for. Thank you very much! ^_^
fdasfdasf said
Thank you very much!
optimizacion1dayrannabarcenas said
Great, without words, I had this problem since two weeks, Congratulations.
Just a observation my code work just writting:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Note: I work whit Android Studio 3.3
Muhammad Solihin said
Thx a lot David..it’s work for me
chairsireland said
Wonderful work, thanks for taking the time to create this, i will give it
a share on my twitter.