I'm trying to popup an soft keyboard on the screen first load programmatically (not change windowSoftInputMode in Manifest).
The funny thing is on the screen first load, it didn't work at all. Here is the code block.
mEDT.requestFocus();
mEDT.requestFocusFromTouch();
mImm.showSoftInput(mEDT, InputMethodManager.SHOW_IMPLICIT);
The showSoftInput is return false, this cause the soft keyboard didn't show.
But when i click on the EditText. The showSoftInput return true and the soft keyboard was shown.
Can anyone explain to me what was happened ?
Stay organized with collections Save and categorize content based on your preferences. Central system API to the overall input method framework (IMF) architecture, which arbitrates interaction between applications and the current input method.
The Android system shows an on-screen keyboard—known as a soft input method—when a text field in your UI receives focus.
You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow , passing in the token of the window containing your focused view. This will force the keyboard to be hidden in all situations. In some cases, you will want to pass in InputMethodManager.
android:windowSoftInputMode="stateAlwaysVisible" -> in manifest File. edittext. requestFocus(); -> in code. This will open soft keyboard on which edit-text has request focus as activity appears.
Are you using Fragments? I have found showSoftInput()
to be unreliable in Fragments.
After inspecting the source code, I discovered that calling requestFocus()
in onCreate()
/onCreateView()
or onResume()
does not immediately cause the object to be focused. This is most likely because the content View hasn't been created yet. So the focus happens sometime later during the initialization of the Activity or Fragment.
I have much more success calling showSoftInput()
in onViewCreated()
.
public class MyFragment extends Fragment {
private InputMethodManager inputMethodManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
EditText text1 = (EditText) view.findViewById(R.id.text1);
text1.requestFocus();
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(view.findFocus(), InputMethodManager.SHOW_IMPLICIT);
super.onViewCreated(view, savedInstanceState);
}
}
Even if you aren't using Fragments, I bet the same rules apply. So make sure the View is created before calling showSoftInput().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With