93

In my app the first view of all my screens is an EditText, so every time i go to a screen the onscreen keypad pops up. how can i disable this popingup and enable it when manually clicked on the EditText????

    eT = (EditText) findViewById(R.id.searchAutoCompleteTextView_feed);

    eT.setOnFocusChangeListener(new OnFocusChangeListener() {

        public void onFocusChange(View v, boolean hasFocus) {

            if(hasFocus){
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
            imm.hideSoftInputFromWindow(eT.getWindowToken(), 0);
            }
        }
    });

xml code:

<ImageView
    android:id="@+id/feedPageLogo"
    android:layout_width="45dp"
    android:layout_height="45dp"
    android:src="@drawable/wic_logo_small" />

<Button
    android:id="@+id/goButton_feed"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="@string/go" />

<EditText
    android:id="@+id/searchAutoCompleteTextView_feed"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/goButton_feed"
    android:layout_toRightOf="@id/feedPageLogo"
    android:hint="@string/search" />

<TextView
    android:id="@+id/feedLabel"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/feedPageLogo"
    android:gravity="center_vertical|center_horizontal"
    android:text="@string/feed"
    android:textColor="@color/white" />

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ButtonsLayout_feed"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >

    <Button
        android:id="@+id/feedButton_feed"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_margin="0dp"
        android:layout_weight="1"
        android:background="@color/white"
        android:text="@string/feed"
        android:textColor="@color/black" />

    <Button
        android:id="@+id/iWantButton_feed"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_margin="0dp"
        android:layout_weight="1"
        android:background="@color/white"
        android:text="@string/iwant"
        android:textColor="@color/black" />

    <Button
        android:id="@+id/shareButton_feed"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_margin="0dp"
        android:layout_weight="1"
        android:background="@color/white"
        android:text="@string/share"
        android:textColor="@color/black" />

    <Button
        android:id="@+id/profileButton_feed"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_margin="0dp"
        android:layout_weight="1"
        android:background="@color/white"
        android:text="@string/profile"
        android:textColor="@color/black" />
</LinearLayout>

<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/feedListView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/ButtonsLayout_feed"
    android:layout_below="@id/feedLabel"
    android:textSize="15dp" >
</ListView>

the third view (EditText) is where the focus is.

Housefly
  • 4,324
  • 11
  • 43
  • 70

29 Answers29

183

The best solution lies in the Project Manifest file (AndroidManifest.xml), add the following attribute in the activity construct

android:windowSoftInputMode="stateHidden"


Example:

    <activity android:name=".MainActivity" 
              android:windowSoftInputMode="stateHidden" />

Description:

  • The state of the soft keyboard — whether it is hidden or visible — when the activity becomes the focus of user attention.
  • The adjustment made to the activity's main window — whether it is resized smaller to make room for the soft keyboard or whether its contents pan to make the current focus visible when part of the window is covered by the soft keyboard.

Introduced in:

  • API Level 3.

Link to the Docs

Note: Values set here (other than "stateUnspecified" and "adjustUnspecified") override values set in the theme.

Skynet
  • 7,820
  • 5
  • 44
  • 80
  • 3
    This approach fails if you have a mix of text fields in the activity, some which you want to have a keyboard for, others for which you do not. See my answer for how this can be achieved on a per-text field basis in such a scenario. – slogan621 Dec 20 '17 at 08:23
  • @slogan621please spare a moment in understanding the question here. You are on a different tangent! – Skynet Dec 21 '17 at 10:53
85

You have to create a view, above the EditText, that takes a 'fake' focus:

Something like :

<!-- Stop auto focussing the EditText -->
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@android:color/transparent"
    android:focusable="true"
    android:focusableInTouchMode="true">
</LinearLayout>

<EditText
    android:id="@+id/searchAutoCompleteTextView_feed"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:inputType="text" />

In this case, I used a LinearLayout to request the focus. Hope this helps.

This worked perfectly...thanks to Zaggo0

Housefly
  • 4,324
  • 11
  • 43
  • 70
  • 26
    Adding a useless view is a sub-optimal solution compared with adding android:windowSoftInputMode="stateHidden" to the Activity's manifest entry. – Chris Horner Feb 19 '13 at 22:33
  • @ChrisHorner in both you need to specified for activity or layout... is the same annoying thing.. both works well for this solution. – Nicolas Jafelle Aug 09 '13 at 20:36
  • Adding a useless view isn't a good idea as @ChrisHorner has said, better add an attribute into Manifest file as suggested by him or add this line of code into your activity `InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(), 0);` – aB9 Sep 21 '15 at 06:43
  • @ChrisHorner: that line has no effect in a Material app on Android 6. – Violet Giraffe May 05 '16 at 13:22
  • I have to agree with Housefly's answer. ChrisHorner's answer is for an activity, what if you have an alert dialog with some input and you don't want the keyboard showing? This is a great solution, just add a "space" above it with 0 dimensions. – Seth May 24 '16 at 19:21
  • In my case everytime I click the Snackbar's button the keyboard pops up. I tried everything in this, how can I solve it? – Ali Bdeir Jul 16 '16 at 09:06
  • android:windowSoftInputMode="stateHidden" did not work. But this fake focus layout of 0 width/height did work. Thanks. – TomV Jan 25 '17 at 14:06
  • 1
    Such a bad implementation of View hierarchy! I think such should be avoided if possible. – sud007 Jul 05 '17 at 10:29
45
     edittext.setShowSoftInputOnFocus(false);

Now you can use any custom keyboard you like.

Matthew556
  • 713
  • 8
  • 11
23

People have suggested many great solutions here, but I used this simple technique with my EditText (nothing in java and AnroidManifest.xml is required). Just set your focusable and focusableInTouchMode to false directly on EditText.

 <EditText
        android:id="@+id/text_pin"
        android:layout_width="136dp"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:textAlignment="center"
        android:inputType="numberPassword"
        android:password="true"
        android:textSize="24dp"
        android:focusable="false"
        android:focusableInTouchMode="false"/>

My intent here is to use this edit box in App lock activity where I am asking user to input the PIN and I want to show my custom PIN pad. Tested with minSdk=8 and maxSdk=23 on Android Studio 2.1

enter image description here

A.B.
  • 1,554
  • 1
  • 14
  • 21
20

Add below code on your activity class.

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Keyboard will pop-upped when user clicks on the EditText

Sonia John Kavery
  • 2,099
  • 2
  • 20
  • 36
10

You can use following code for disabling OnScreen Keyboard.

InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(editText.getWindowToken(), 0);
Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • 1
    and i can enable it again by using EditText.onClick(im.unhideSoftInputFromWindow(editText.getWindowToken(), 0);) or something like that?? – Housefly May 16 '12 at 04:08
8

Two Simple Solutions:

First Solution is added below line of code in manifest xml file. In Manifest file (AndroidManifest.xml), add the following attribute in the activity construct

android:windowSoftInputMode="stateHidden"

Example:

<activity android:name=".MainActivity" 
          android:windowSoftInputMode="stateHidden" />

Second Solution is adding below line of code in activity

//Block auto opening keyboard  
  this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

We can use any one solution of above. Thanks

kgsharathkumar
  • 1,369
  • 1
  • 19
  • 36
5

Declare the global variable for InputMethodManager:

 private InputMethodManager im ;

Under onCreate() define it:

 im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
 im.hideSoftInputFromWindow(youredittext.getWindowToken(), 0);

Set the onClickListener to that edit text inside oncreate():

 youredittext.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        im.showSoftInput(youredittext, InputMethodManager.SHOW_IMPLICIT);
    }
});

This will work.

Ky -
  • 30,724
  • 51
  • 192
  • 308
AndroGeek
  • 1,280
  • 3
  • 13
  • 25
  • all the code is in the question, the onscreen keypad is not showing up when i click on the edit text – Housefly May 16 '12 at 07:25
  • Are you testing this using an emulator or a device. If you are trying it using an emulator then you will not get the softkeys. Test it using a device. It will work. – AndroGeek May 16 '12 at 07:34
  • yeah i am using a device to test – Housefly May 16 '12 at 07:35
  • Hmmm that's strange. If you could post your code, the method in which you have written then it could be of some help. Or else the above code is working absolutely fine in my device. – AndroGeek May 16 '12 at 07:38
  • oh...but no problem it got solved though...in a complete different way...thank you anyways...check my answer...that might help you as well :) – Housefly May 16 '12 at 07:43
4

Use the following code, write it under onCreate()

InputMethodManager inputManager = (InputMethodManager)
                                   getSystemService(Context.INPUT_METHOD_SERVICE); 
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                                   InputMethodManager.HIDE_NOT_ALWAYS);         
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Lucky
  • 16,787
  • 19
  • 117
  • 151
Dharmaraj Patil
  • 116
  • 1
  • 6
4

try it.......i solve this problem using code:-

EditText inputArea;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
inputArea = (EditText) findViewById(R.id.inputArea);

//This line is you answer.Its unable your click ability in this Edit Text
//just write
inputArea.setInputType(0);
}

nothing u can input by default calculator on anything but u can set any string.

try it

S.M.Fazle Rabbi
  • 541
  • 5
  • 13
3

Well, I had the same problem and I just tackled with focusable in the XML file.

<EditText
            android:cursorVisible="false"
            android:id="@+id/edit"
            android:focusable="false"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

You probably are looking for security also. This will help in that also.

Divyansh
  • 31
  • 4
3

Thanks @A.B for good solution

    android:focusableInTouchMode="false"

this case if you will disable keyboard in edit text , just add android:focusableInTouchMode="false" in edittext tagline.

work for me in Android Studio 3.0.1 minsdk 16 , maxsdk26

NZXT
  • 31
  • 1
  • 1
    Is this a comment to `A.B`s answer? Or is it an answer to the original question? If this is a comment to `A.B`s answer, then you should use the comment option provided by StackOverflow and delete this answer to the original question. – David Walschots Mar 12 '18 at 19:33
  • sorry if i didn't understand to answer the solved answer . i say thank to A.B for a good answer to clear my problem.. if i wrong , i can delete that answer , sorry @DavidWalschots . – NZXT Mar 16 '18 at 07:27
2

Try With this:

EditText yourEditText= (EditText) findViewById(R.id.yourEditText); 
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT); 

To close, you can use:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
 imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0); 

Try it like this in your code:

ed = (EditText)findViewById(R.id.editText1);

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
imm.hideSoftInputFromWindow(ed.getWindowToken(), 0);  

ed.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
        imm.showSoftInput(ed, InputMethodManager.SHOW_IMPLICIT);  
    }
});
Ky -
  • 30,724
  • 51
  • 192
  • 308
user1213202
  • 1,305
  • 11
  • 23
  • it is working the same way as previously it was....are thr any changes i need to do in the layout??? for this EditText view?? – Housefly May 16 '12 at 05:11
  • okay, now it works after adding few layout attributes... android:clickable="true" android:cursorVisible="false" android:focusable="false" thank you – Housefly May 16 '12 at 05:15
  • no.do one thing.write the hiding the keyboard code in edittext.setOnFocusChangeListener() method and try – user1213202 May 16 '12 at 05:15
  • this would be the solution IF you could specify exactly when to use calling hideSoftInput. the keyboard automatically pops up AFTER onCreate and onResume – ahmet emrah Dec 22 '12 at 08:36
2

Only thing you need to do is that add android:focusableInTouchMode="false" to the EditText in xml and thats all.(If someone still needs to know how to do that with the easy way)

GoldFish
  • 107
  • 8
1

Use following code in your onCreate() method-

    editText = (EditText) findViewById(R.id.editText);
    editText.requestFocus();
    editText.postDelayed(new Runnable() {
        public void run() {
            InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            keyboard.hideSoftInputFromWindow(
                    editText.getWindowToken(), 0);
        }
    }, 200);
Priyanka
  • 677
  • 5
  • 20
1
       <TextView android:layout_width="match_parent"
                 android:layout_height="wrap_content"

                 android:focusable="true"
                 android:focusableInTouchMode="true">

                <requestFocus/>
      </TextView>

      <EditText android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
NickUnuchek
  • 11,794
  • 12
  • 98
  • 138
1

If you are using Xamarin you can add this

Activity[(WindowSoftInputMode = SoftInput.StateAlwaysHidden)]

thereafter you can add this line in OnCreate() method

youredittext.ShowSoftInputOnFocus = false;

If the targeted device does not support the above code, you can use the code below in EditText click event

InputMethodManager Imm = (InputMethodManager)this.GetSystemService(Context.InputMethodService);
Imm.HideSoftInputFromWindow(youredittext.WindowToken, HideSoftInputFlags.None);
Ashish Sinha
  • 87
  • 2
  • 10
1

I found the following pattern to work well for me in code where I want to show a dialog to get the input (e.g., the string displayed in the text field is the result of selections made from a list of checkboxes in a dialog, rather than text entered via the keyboard).

  1. Disable soft input focus on the edit field. I can't disable for the entire activity, as there are edit fields I do want the keyboard to be used for in the same layout.
  2. Initial clicks in the text field yield a focus change, a repeated click yields a click event. So I override both (here I don't refactor the code out to illustrate both handlers do the same thing):

    tx = (TextView) m_activity.findViewById(R.id.allergymeds);
    if (tx != null) {
        tx.setShowSoftInputOnFocus(false);
        tx.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    MedicationsListDialogFragment mld = new MedicationsListDialogFragment();
                    mld.setPatientId(m_sess.getActivePatientId());
                    mld.show(getFragmentManager(), "Allergy Medications Dialog");
                }
            }
        });
        tx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MedicationsListDialogFragment mld = new MedicationsListDialogFragment();
                mld.setPatientId(m_sess.getActivePatientId());
                mld.show(getFragmentManager(), "Allergy Medications Dialog");
            }
        });
    }
    
slogan621
  • 1,300
  • 13
  • 9
0

In a Android app I was building, I had three EditTexts in a LinearLayout arranged horizontally. I had to prevent the soft keyboard from appearing when the fragment loaded. In addition to setting focusable and focusableInTouchMode to true on the LinearLayout, I had to set descendantFocusability to blocksDescendants. In onCreate, I called requestFocus on the LinearLayout. This prevented the keyboard from appearing when the fragment is created.

Layout -

    <LinearLayout
       android:id="@+id/text_selector_container"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:weightSum="3"
       android:orientation="horizontal"
       android:focusable="true"
       android:focusableInTouchMode="true"
       android:descendantFocusability="blocksDescendants"
       android:background="@color/black">

       <!-- EditText widgets -->

    </LinearLayout>

In onCreate - mTextSelectorContainer.requestFocus();

indyfromoz
  • 4,045
  • 26
  • 24
0

If anyone is still looking for the easiest solution, set the following attribute to true on your parent layout

android:focusableInTouchMode="true"

Example:

<android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusableInTouchMode="true">

.......
......
</android.support.constraint.ConstraintLayout>
Ajmal Salim
  • 4,142
  • 2
  • 33
  • 41
0

Use This to enable and disable EditText....

InputMethodManager imm;

imm = (InputMethodManager)
getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);

if (isETEnable == true) {

    imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
    ivWalllet.setImageResource(R.drawable.checkbox_yes);
    etWalletAmount.setEnabled(true);
    etWalletAmount.requestFocus();
    isETEnable = false;
    } 
else {

    imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY,0);
    ivWalllet.setImageResource(R.drawable.checkbox_unchecked);
    etWalletAmount.setEnabled(false);
    isETEnable = true;
    }
cagatayodabasi
  • 762
  • 11
  • 34
0

For Xamarin Users:

[Activity(MainLauncher = true, 
        ScreenOrientation = ScreenOrientation.Portrait, 
        WindowSoftInputMode = SoftInput.StateHidden)] //SoftInput.StateHidden - disables keyboard autopop
Roy Doron
  • 585
  • 9
  • 23
0

Try this answer,

editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
editText.setTextIsSelectable(true);

Note: only for API 11+

Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
0
private InputMethodManager imm;

...


editText.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        v.onTouchEvent(event);
        hideDefaultKeyboard(v);
        return true;
    }
});

private void hideDefaultKeyboard(View et) {
  getMethodManager().hideSoftInputFromWindow(et.getWindowToken(), 0);
}

private InputMethodManager getMethodManager() {
        if (this.imm == null) {
            this.imm = (InputMethodManager)  getContext().getSystemService(android.content.Context.INPUT_METHOD_SERVICE);
        }
  return this.imm;
}
Denis Raison
  • 41
  • 1
  • 1
  • 6
0

Try below lines of code when you want to hide keyboard on screen on any event. It worked for me.

context.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN );

final InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);

if (inputMethodManager.isAcceptingText())       inputMethodManager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), 0);
Kristian
  • 2,456
  • 8
  • 23
  • 23
Rahul Jadhav
  • 41
  • 1
  • 4
-1

for any textview at your activity (or create fake empty textfiew with android:layout_width="0dp" android:layout_height="0dp" ) and add for this textview next: android:textIsSelectable="true"

-1

Just remove the <request focus/> tag from the xml file.

Pang
  • 9,564
  • 146
  • 81
  • 122
Jeyanth
  • 5
  • 1
-1

Just need to add property android:focusable="false" in particular EditText in the layout xml. Then you can write click listner for EditText without keypad popup.

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
Sree
  • 1
  • 6
-1

It issue can be sorted by using, No need to set editText inputType to any values, just add the below line, editText.setTextIsSelectable(true);

arun
  • 95
  • 13
  • Hey, thanks for writing up an answer! However, since there are already so many answers here, it might be even more helpful if you actually wrote this as a comment on the answer of Ranjith Kumar instead of making it its own answer. It might also be interesting to mention what the effect of leaving out the `inputType` would be. But it does look like this would be good to know if I went with that other answer, so even if you insist on keeping this around as its own answer, please consider leaving a comment there anyway. – Mark Dec 05 '18 at 14:34