anthony
anthony

Reputation: 7723

Android: how can change the text color in custom view?

I have this view called ButtonMultiline:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center">

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/ButtonMultilineCatalogTitle"
    android:text="title"/>

<TextView
    android:id="@+id/subtitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/ButtonMultilineCatalogSubtitle"
    android:text="subtitle"/>

I apply a background on this layout to change color on press:

<selector android:exitFadeDuration="200"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/btn_large_normal" />
<item android:state_pressed="true">
    <color android:color="@color/background_black_top" />
</item>
<item android:state_focused="true" android:drawable="@drawable/btn_large_pressed" />
<item android:drawable="@drawable/btn_large_pressed" android:state_hovered="true" />
<item android:state_checked="true" android:drawable="@drawable/btn_large_normal" />
<item android:drawable="@drawable/btn_large_normal" />

All works fine until here.

I want to change the both TextView color on press too, but when I add this line in both TextView, there is a crash on inflate problem on this button_text_catalog.xml:

<android:background="@drawable/button_text_catalog">

My button_text_catalog.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white" android:state_pressed="true" />
<item android:color="@color/white" android:state_focused="true" />
<item android:color="@color/theme_primary"/>

Thanks guys!

Upvotes: 0

Views: 1189

Answers (1)

Sagar Zala
Sagar Zala

Reputation: 5144

Use this code:

button_text_catalog.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- pressed -->
    <item android:state_pressed="true" android:color="@color/white"/>
    <!-- focused -->
    <item android:state_focused="true" android:color="@color/white"/>
    <!-- activated -->
    <item android:state_activated="true" android:color="@color/white"/>
    <!-- checked -->
    <item android:state_checked="true" android:color="@color/white"/>

    <!-- default -->
    <item android:color="@color/theme_primary"/>

</selector>

in Style.xml

add <item name="android:textColor">@drawable/button_text_catalog</item>

Upvotes: 2

Related Questions