Blog Archive

Android EditText hide soft keyboard from displaying automatically

Well the soft keyboard is kind of annoying when you don't really want it. Let's say you have a EditText, AutoCompleteTextView, SearchView etc. on the screen layout. And as soon as you display the screen the field takes focus and displays the Soft Keyboard.

So to solve the issue you need to remove the focus from the EditText field. How to achieve that simple task? Well after trying out several solutions I found that adding an invisible LinearLayout before any EditText, AutoCompleteTextView, SearchView etc. on your screen layout will do the job. This LinearLayout will grab the focus from the EditText without giving it to an other editable element.
 <TextView android:textAppearance="?android:attr/textAppearanceMedium"
  android:id="@+id/itemText" android:text="Item Number:"
  android:layout_height="wrap_content" android:layout_width="wrap_content"
  android:layout_below="@id/locations" android:textStyle="bold" />
 <LinearLayout android:focusable="true"
  android:focusableInTouchMode="true" android:layout_width="0px"
  android:layout_height="0px" />
 <EditText android:id="@+id/itemNumber"
  android:layout_height="wrap_content" android:layout_width="match_parent"
  android:layout_below="@id/itemText" />
 <Button android:text="Submit" android:layout_height="wrap_content"
  android:id="@+id/itemInfo" android:layout_below="@id/itemNumber"
  android:layout_width="wrap_content" />

In case you found a better solution please share it!


11 comments :

  1. this works like charm, thanks:)

    ReplyDelete
  2. Thanks) that was the only solution that worked for me))

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. I think you can also disable the display of the keyboard in your manifest:

    activity
    android:name="com.example.Activity"
    android:label="@string/title"
    android:windowSoftInputMode="stateHidden" \>

    ReplyDelete
  5. This worked for me after doing a requestFocus on the LinearLayout in the code.

    ReplyDelete
  6. Thank you. Instead of adding a invisible LinearLayout, I do the same setting on the TextView which has already been in the layout, and it work.

    ReplyDelete
  7. you can also add this

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

    to your parent layout. This worked perfect for me

    ReplyDelete
  8. Thank you. Instead of adding a invisible LinearLayout, I do the same setting on the TextView which has already been in the layout, and it work.

    ReplyDelete