3

I am using EditText but when Activity is started my EditText is already focused. I want to remove focus and also make it editable on click.

Here is the code of my edittext.

<EditText
            android:id="@+id/et_HotelLocation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:drawableLeft="@drawable/ic_edit_location_black_24dp"
            android:drawablePadding="8dp"/>
musica
  • 1,373
  • 3
  • 15
  • 34
MILAN MARWADI
  • 101
  • 1
  • 1
  • 7

3 Answers3

2

Add the following two properties in parent Layout of the Edit text

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

if your edit text is inside linear layout you can do like this

<LinearLayout 
  android:focusable="true"
  android:focusableInTouchMode="true" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />

<EditText
            android:id="@+id/et_HotelLocation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:drawableLeft="@drawable/ic_edit_location_black_24dp"
            android:drawablePadding="8dp"/>
</LinearLayout>
Fahad Rehman
  • 1,189
  • 11
  • 25
2

You can create dummy layout as below which you should keep exactly above your EditText and then use nextFocusUp and nextFocusLeft as shown.

<LinearLayout
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_width="0px"
            android:layout_height="0px"/>

        <EditText

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
             android:id="@+id/et"
            android:drawablePadding="8dp"
            android:nextFocusUp="@id/et"
            android:nextFocusLeft="@id/et"
            app:met_floatingLabel="normal" />
Mehul Kanzariya
  • 888
  • 3
  • 27
  • 58
1

This question has already been responded here

Try this also - this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Otherwise, declare in your manifest file's activity -

<application android:icon="@drawable/icon"
             android:label="@string/app_name">
<activity android:name=".Main"
     android:label="@string/app_name"
     android:windowSoftInputMode="stateHidden"
     >
Community
  • 1
  • 1
Lionel Briand
  • 1,732
  • 2
  • 13
  • 21