Tuesday, August 18, 2015

How to remove auto focus from EditText in Android

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#d4c5b8"
    android:focusable="true"
    android:focusableInTouchMode="true"
    >
    <!--
        RelativeLayout
            android:focusable="true"
            android:focusableInTouchMode="true"

            This two attributes remove focus from both EditText
            widgets and set focus to layout container widget.
    -->
    <EditText
        android:id="@+id/et"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:hint="Input your country"
        android:padding="10dp"
        />
    <EditText
        android:id="@+id/et_city"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:hint="Input your City"
        android:padding="10dp"
        android:layout_below="@+id/et"
        />
</RelativeLayout>


More android examples