Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove focus from EditText and also keep it editable

Tags:

android

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"/>
like image 226
MILAN MARWADI Avatar asked Dec 29 '25 07:12

MILAN MARWADI


2 Answers

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" />
like image 136
Mehul Kanzariya Avatar answered Dec 30 '25 21:12

Mehul Kanzariya


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>
like image 29
Fahad Rehman Avatar answered Dec 30 '25 19:12

Fahad Rehman