Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why or when does the InputMethodManager.showSoftInput return false?

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 ?

like image 974
Harvey Avatar asked Jan 27 '16 04:01

Harvey


People also ask

What is the use of InputMethodManager in android?

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.

What is soft input mode android?

The Android system shows an on-screen keyboard—known as a soft input method—when a text field in your UI receives focus.

How do I hide the keyboard on my android?

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.

How do I show soft keyboard when Edittext is focused?

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.


1 Answers

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().

like image 124
Ben Avatar answered Oct 03 '22 21:10

Ben