Mike T Mike T - 6 months ago 99
Android Question

EditText automatically opens soft keyboard when Fragment is visible with ViewPager

I have a

Fragment
(the compatibility version) with an
EditText
in its layout. I'm using a
ViewFlipper
to flip between fragments. When I get to this particular
Fragment
, the soft keyboard opens up automatically. This is not what I want. Here is what I've tried to stop it or hide it.

Tried:

android:descendantFocusability="beforeDescendants"


on the fragment's main view

Tried:

android:windowSoftInputMode="stateHidden"


and

android:windowSoftInputMode="stateAlwaysHidden"


in the manifest for the activity

Tried:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mViewPager.getChildAt(position).getWindowToken(), 0);


on the OnPageChangeListener of my ViewPager

Tried:

InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(voucherView.findViewById(R.id.redeem_mobile_number).getWindowToken(), 0);


in onCreateView in my Fragment

Tried:

InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getView().findViewById(R.id.redeem_mobile_number).getWindowToken(), 0);


in onStart in my Fragment

Tried:

voucherView.findViewById(R.id.redeem_mobile_number).clearFocus();


in onCreateView in my Fragment

It seems to me like onPageChangeListener is the place to do this because the other calls happen before the soft keyboard is actually open. Any help would be great.

Answer

This post has a solution to the problem.

The answer was to add android:focusableInTouchMode="true" to the LinearLayout containing the EditText. Now it doesn't bring up the soft keyboard automatically.

Comments