Naveen Shriyan
Naveen Shriyan

Reputation: 83

How to remove focus from TextInputLayout when page loads?

when this xml loads in a fragment, the focus directly goes to second edit text, i want that edit text should get focus on touching it. and i want to use my own color to both hint and for text which m typing on it.Any help highly appreciated.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="23dp">

    <TextView
        android:id="@+id/basic_details_heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/basic_details"
        android:textSize="16sp"
        android:paddingLeft="0dp"
        android:textColor="@color/basic_details_gray"
        android:textStyle="normal"
        android:typeface="monospace"
        android:includeFontPadding="false"
        android:layout_alignParentLeft="true"
        />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/container_first"
        android:layout_below="@+id/basic_details_heading"
        android:paddingLeft="0dp"
        >

      <!--  <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/profile_created_for"
            android:id="@+id/sub_head_one"
            android:textStyle="normal"
            android:textColor="@color/login_background"
            android:textSize="12sp"
            android:paddingLeft="0dp"
            android:paddingTop="0dp"
            android:visibility="gone"
            android:includeFontPadding="false"
            android:layout_alignParentLeft="true"
            />-->
        <android.support.design.widget.TextInputLayout
            android:id="@+id/profile_created_for"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >

            <EditText
                android:id="@+id/edit_text_profile_created_for"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="-4dp"
                android:textSize="15sp"
                android:textColor="@color/black"
                android:textStyle="normal"
                android:hint="Profile Created For"
                android:focusable="false"/>

        </android.support.design.widget.TextInputLayout>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/nav_next"
            android:layout_alignParentRight="true"
            android:paddingTop="20dp"
            android:id="@+id/profile_created_for_arrow"
            />


        <android.support.design.widget.TextInputLayout
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/profile_created_for"
            android:focusableInTouchMode="true"
            >

            <EditText
                android:id="@+id/edit_text_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="-4dp"
                android:textSize="15sp"
                android:textColor="@color/black"
                android:textStyle="normal"
                android:hint="Name"
                />

        </android.support.design.widget.TextInputLayout>


</RelativeLayout>


</RelativeLayout>

Upvotes: 8

Views: 12798

Answers (4)

Linh Nguyễn
Linh Nguyễn

Reputation: 11

I tried add another EditText before the Edittext you don't want to autofocus and it works

<EditText
      android:layout_width="0dp"
      android:layout_height="0dp" />
    <android.support.design.widget.TextInputLayout
        android:id="@+id/NghiPhep_TextInputLayout_GhiChu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:hintTextAppearance="@style/TextHintLabel"
        android:layout_marginTop="5dp"
        android:focusableInTouchMode="true"
        >
        <EditText
            android:id="@+id/NghiPhep_txt_GhiChu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/colorPrimaryDark"
            android:hint="Ghi chú"
            android:fontFamily="@font/arial_rounded"
            android:inputType="textMultiLine"
            android:singleLine="false"
            android:textSize="18sp"/>
    </android.support.design.widget.TextInputLayout>

Upvotes: 0

Vinil Chandran
Vinil Chandran

Reputation: 1561

Add the attribute android:focusableInTouchMode="true" in the parent view of TextInputLayout as follows

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusableInTouchMode="true">
    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ............>
        <android.support.v7.widget.AppCompatEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ............/>
    </android.support.design.widget.TextInputLayout>
</RelativeLayout>

Upvotes: 16

Amit Vaghela
Amit Vaghela

Reputation: 22955

In your root view (TextInputLayout) use this attribute: (which you want to disable use)

android:focusableInTouchMode="true"

Upvotes: 6

Josh
Josh

Reputation: 6383

Try this: add a dummy object in your xml, right before your Text Layout, so that it can consume the focus.

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

Upvotes: 26

Related Questions