Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText automatically opens soft keyboard when Fragment is visible with ViewPager

Tags:

android

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.

like image 863
Mike T Avatar asked Jul 04 '12 11:07

Mike T


4 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.

like image 100
Mike T Avatar answered Oct 15 '22 19:10

Mike T


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

like image 31
Yajneshwar Mandal Avatar answered Oct 15 '22 19:10

Yajneshwar Mandal


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();
like image 34
Mohsin Naeem Avatar answered Oct 15 '22 19:10

Mohsin Naeem


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

like image 37
miberg81 Avatar answered Oct 15 '22 17:10

miberg81