18

I have a simple EditText over a ListView defined below.

When the app runs, the EditText is selected and the keyboard appears.

I don't want that to happen. I don't want it selected or the keyboard to appear by default.

<EditText
    android:id="@+id/search_box"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="type to search titles"
    android:inputType="text"
    android:maxLines="1"
    android:capitalize="none"
    android:linksClickable="false"
    android:autoLink="none"
    android:autoText="true"
    android:singleLine="true" />
<ListView
    android:id="@+id/DetailsListView"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:background="@color/transparent"
    android:cacheColorHint="@color/transparent"
    android:listSelector="@drawable/list_selector"
    android:fastScrollEnabled="true" />
Ian Vink
  • 66,960
  • 104
  • 341
  • 555

6 Answers6

29

First the tag is monodroid so it's on C# so the accepted answer is correct.

But is not what I think the author wants... this trick just hide the keyboard but the editText still have the focus.

To do this both in java and c#, you have to put this in your root Layout :

android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"

For example :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
              >
....
</LinearLayout>
Dahevos
  • 1,495
  • 5
  • 16
  • 26
13

I found this to be the solution:

Window.SetSoftInputMode (SoftInput.StateAlwaysHidden);

Note: This is in C# not Java for Android

Ian Vink
  • 66,960
  • 104
  • 341
  • 555
  • 6
    why is this the accepted answer. The question is for Android development in Java not C#. – WallMobile Mar 04 '13 at 01:01
  • You can write Android native apps in C#. In fact you can write iOS in C# too and share 80% of the code between the two plateforms. 100% native code. (Xamarin.com) – Ian Vink Apr 03 '13 at 16:27
  • Regarding the answer above, you can also specify this in the manifest for the Activity as follows: `android:windowSoftInputMode="stateAlwaysHidden"`. – Adil Hussain Apr 02 '15 at 09:41
8

Add this in onCreate

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Kevin Parker
  • 16,975
  • 20
  • 76
  • 105
4

try this in layout

<EditText
                    android:id="@+id/myid2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/myid1"
                    android:clickable="true"
                    android:cursorVisible="false"
                    android:focusable="false"
                    android:hint="@string/hint"
                    android:singleLine="true"
                    android:textColor="@color/black"
                    android:textSize="15dp" />

and this in your code

    myEditText.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_UP) {
                //do tasks
            }
            return false;
        }
    });

i dont know if it is of any use. i dont even know what it does. but it solved a similar problem for me. sry if i wasted your time

Housefly
  • 4,324
  • 11
  • 43
  • 70
1

i found my solution , you can try this one and it works perfect.

xml (i set editText focusableInTouchMode to false in defualt):

       <EditText ... android:focusableInTouchMode="false" .../>

and i changed focusableInTouchMode after touching in java :

        yourEditText.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            yourEditText.setFocusableInTouchMode(true);
            return false;
        }
    });
Diyako
  • 651
  • 1
  • 9
  • 24
0

android:windowSoftInputMode="stateHidden" did not work for me. Neither did messing around with the elements focus and other techniques (even setting it as enabled=false shows the keyboard).

My Solution: Create a hideKeyboard Function:

private void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

And trigger it whenever the UI that's hosting the textfield becomes visible:

For a Dialog:

 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            hideKeyboard();
        }

For a widget:

@Override
public void setVisibility(int visibility) {
    super.setVisibility(visibility);
    hideKeyBoard();
}
TomDK
  • 1,321
  • 11
  • 16