9

In my android app, in my profile edit page, when I start the activity, the first edittext field is focused (blinking cursor), and the keyboard gets displayed.

How can I keep it being focused on startup (blinking cursor) but prevent the keyboard from showing up? If that is not possible, then just not focus the edittext on startup.

Thanks.

        <EditText
            android:id="@+id/textinput_firstname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="test" />
omega
  • 40,311
  • 81
  • 251
  • 474
  • possible duplicate http://stackoverflow.com/questions/2466516/show-soft-keyboard-when-activity-starts – DynamicMind Jul 18 '13 at 01:32
  • That question is about showing keyboard and this Question is about hiding keyboard, what makes you think of that as a possible duplicate ? – Salman Khakwani Mar 01 '14 at 12:27

2 Answers2

18

Just add this line of code in your onCreate(...) method

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Salman Khakwani
  • 6,684
  • 7
  • 33
  • 58
  • 1
    I have to wait 5 minutes before I can... The site wont let me. – omega Jul 18 '13 at 01:09
  • this will break resizing `Activity` feature set by `android:windowSoftInputMode="adjustResize"` - will behave like `adjustPan` and stop resizing... – snachmsm Apr 01 '20 at 14:32
2

This is probably happening for EditText for most users, all you have to do is, just go to the layout file and set the layout's view groups attributes as follows:

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

For Example.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:background="@color/colorPrimary"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:focusable="true"
    android:focusableInTouchMode="true"> 
    <EditText
        android:layout_width="match_parent"
        android:layout_height="41dp"
      />
</LinearLayout>

Note: you have to set the attributes for the first view in the layout no matter what it is This will set the default focus to false;

Harshit Jain
  • 885
  • 11
  • 15