Change layout of EditText on state "selected"

3.1k Views Asked by At

I am trying to obtain a EditText changing it's colors and layout when the user clicks on it to type something, and I do that with a custom drawable, but the attributes android:state_enabled="true" android:state_focused="true"> don't work at all, meaning that they don't get called.

EditText:

 <EditText android:id="@+id/field_user_info" android:hint="Nome e Cognome"
        android:layout_marginTop="8dp" android:layout_marginBottom="8dp"
        android:padding="8dp" android:maxLines="1"
        android:background="@drawable/custom_account_edittext"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:textColorHint="@color/textHint"/>

custom_account_edittext drawable:

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle">
            <!--<solid android:background="?android:attr/selectableItemBackground"/>-->
            <!--android:color="#ffffff" -->
            <solid android:color="@color/account_grey_300" />
            <stroke android:width="0.8dip"
                android:color="@color/account_grey_400"/>
            <corners android:radius="2dip" /> <!--android:bottomRightRadius="7dp"--><!--android:bottomLeftRadius="7dp"--><!--android:topLeftRadius="7dp"--><!--android:topRightRadius="7dp"/>-->
        </shape>
    </item>
    <item android:state_enabled="true"  android:state_focused="true">
        <shape android:shape="rectangle">
            <!--<solid android:background="?android:attr/selectableItemBackground"/>-->
            <!--android:color="#ffffff" -->
            <solid android:color="@color/white" />
            <stroke android:width="1dip"
                android:color="@color/cardview_blu_color600"/>
            <corners android:radius="2dip" />
        </shape>
    </item>
</selector>

My problem is that I always visualize the default layout (the first item). Why do the items <item android:state_enabled="true" android:state_focused="true"> don't get activated at all?

How can I customize my TextView states?

1

There are 1 best solutions below

4
On BEST ANSWER

Try this

<item android:state_enabled="true" android:state_focused="true">

EDIT:

Try this and check if the background color is changed when clicked on edittext

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item
        android:state_pressed="true"
        android:drawable="#f07c01"
        /> <!-- pressed -->
    <item
    android:state_enabled="true" android:state_focused="true"
        android:drawable="#c099cc00"
        /> <!-- focused -->
    <item
        android:drawable="#0d91a8"
        /> <!-- default -->
</selector>