Show keyboard automatically
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);