How to change TextView or EditText Text Color on Focus and on Press


To change the Text color for TextView when it is Pressed or brought on Focus you need to create a selector XML file under res folder. If you do not find a color folder just create a new folder named "color"

Now Right Click on the color folder and select Android XML file

Now select Selector as Root element

res->color->textview_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#878782"/>
    <item android:state_focused="true" android:color="#483728"/>
    <item android:color="#ff000000"/>
</selector>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textColor="@color/textview_selector"
        android:ems="10" >
        <requestFocus />
    </EditText>
</RelativeLayout>



Recent Posts:




Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.