Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove focus from TextInputLayout when page loads?

Tags:

android

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>
like image 695
Naveen Shriyan Avatar asked Feb 22 '16 16:02

Naveen Shriyan


3 Answers

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"/>
like image 162
Josh Avatar answered Nov 02 '22 22:11

Josh


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>
like image 31
Vinil Chandran Avatar answered Nov 03 '22 00:11

Vinil Chandran


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

android:focusableInTouchMode="true"
like image 6
Amit Vaghela Avatar answered Nov 03 '22 00:11

Amit Vaghela