31

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.

Mike T
  • 4,747
  • 4
  • 32
  • 52
  • To hide the keyboard within a fragment try: InputMethodManager imm = (InputMethodManager) getSherlockActivity().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(inputSearch.getWindowToken(), 0); P.S the inputSearch is the EditText field – Mazen Kasser Jul 18 '13 at 05:11
  • Hi @Mike T, In my case i am using tablayout with viewpager and also 5 fragment pages when soft keyboard popups tab also move to top of keyboard, I tried everything above you mentioned but still i did not get answer..please help mee – Vijay Sep 14 '18 at 07:20
  • @Vijaykumar if my answer below didn't help, then I'm out of ideas. Sorry – Mike T Sep 17 '18 at 09:21

7 Answers7

82

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.

Community
  • 1
  • 1
Mike T
  • 4,747
  • 4
  • 32
  • 52
8
 <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.

5

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();
Mohsin Naeem
  • 12,542
  • 3
  • 39
  • 53
  • combination of android:configChanges and android:windowSoftInputMode is important. – Mohsin Naeem Jul 04 '12 at 11:05
  • Your edit worked the first time I swiped to the Fragment's view but it didn't work after that. It definitely doesn't seem like the right way of doing this – Mike T Jul 04 '12 at 11:36
  • I know it is not the correct method of doing it..:) But I am also end up with this dirty solution. are try my edit on onPageChangeListener?? It is working for my case. – Mohsin Naeem Jul 04 '12 at 11:38
  • That is where I tried it. It worked the first time but not after that. – Mike T Jul 04 '12 at 11:41
1

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"
Derlin
  • 9,572
  • 2
  • 32
  • 53
Rahul
  • 31
  • 4
1

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

Shruti
  • 803
  • 9
  • 26
miberg81
  • 41
  • 4
0

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);  
}
Aleksei Zyrianov
  • 2,294
  • 1
  • 24
  • 32
ASP
  • 3,645
  • 1
  • 31
  • 45
0

Try This

exitText.setFocusableInTouchMode(true);
Derlin
  • 9,572
  • 2
  • 32
  • 53
Shreyas K C
  • 140
  • 6