Wednesday, February 1, 2017

Textview selector text color

Creating text selector for TextView to change text color when users presse or click on TextView.


1.text_selector.xml

Create and XML named text_selector.xml in drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#00598f"/>
    <item android:state_activated="true" android:color="#00598f"/>
    <item android:color="#b35411" />
</selector>

2.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="50dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="49.5dp"
        android:text="Text Selector"
        android:textSize="35sp"
        android:textStyle="bold"
        android:clickable="true"
        android:gravity="center"
        android:textColor="@drawable/text_selector"/>
</LinearLayout>


EmoticonEmoticon