Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: EditText turns on keyboard automatically, how to stop?

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" />
like image 836
Ian Vink Avatar asked May 15 '12 01:05

Ian Vink


People also ask

How do I hide the soft keyboard on Android after clicking outside EditText?

Ok everyone knows that to hide a keyboard you need to implement: InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);


3 Answers

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>
like image 140
Dahevos Avatar answered Sep 21 '22 21:09

Dahevos


I found this to be the solution:

Window.SetSoftInputMode (SoftInput.StateAlwaysHidden);

Note: This is in C# not Java for Android

like image 27
Ian Vink Avatar answered Sep 20 '22 21:09

Ian Vink


Add this in onCreate

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
like image 24
Kevin Parker Avatar answered Sep 23 '22 21:09

Kevin Parker