Home » Android » android – EditText automatically opens soft keyboard when Fragment is visible with ViewPager

android – EditText automatically opens soft keyboard when Fragment is visible with ViewPager

Posted by: admin April 23, 2020 Leave a comment

Questions:

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.

How to&Answers:

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.

Answer:

 <LinearLayout 
         android:id="@+id/layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:focusable="true" 
        android:focusableInTouchMode="true"         
        >

    <EditText
        android:id="@+id/retailer_search_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

</LinearLayout>

Just follow it . And EditText should be within a LinearLayout.

Answer:

Have you tried this?

<activity
    android:name=".YourActivityName"
    android:configChanges="keyboardHidden|orientation"
    android:windowSoftInputMode="stateHidden" />

EDIT:

try this (I now it is a bad one but give a try to this) 🙂

    Thread splashTread = new Thread() {
        @Override
        public void run() {
            try {

                sleep(1);
            } catch (InterruptedException e) {
                // do nothing
            } finally {
                runOnUiThread(new Runnable() {
                    public void run() {
                        InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(youreditText.getWindowToken(), 0);

                    }
                });

            }
        }
    };
    splashTread.start();

Answer:

I had a soft keyboard disturbingly popup and pushing all views up when I click on an edit text in a fragment view (my app is a app of nested fragments – fragment in fragment)

I tried a dozen solutions here and on the internet, but nothing helped except this, which is edit the EditText itself in XML (not the view above/not the manifest/not overwride on create activities/not any other)

by adding android:focusableInTouchMode="false" line to your EditText’s xml.

I borrowed the solution from ACengiz on this thread

how to block virtual keyboard while clicking on edittext in android?

Only 2 people voted for him? although for me it was a saver after hours of a headache

Answer:

Add this to your activity tag in AndroidManifest.xml
keyboardHidden: will not let it open soft input keyboard automatically.

android:configChanges="orientation|keyboardHidden|screenSize"
android:windowSoftInputMode="stateAlwaysHidden|adjustResize|stateHidden"

Answer:

This worked for me.

edittext.setInputType(InputType.TYPE_NULL);      
if (android.os.Build.VERSION.SDK_INT >= 11)   
{  
    edittext.setRawInputType(InputType.TYPE_CLASS_TEXT);  
    edittext.setTextIsSelectable(true);  
}

Answer:

Try This

exitText.setFocusableInTouchMode(true);