Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: change font color on click

i am trying to change the color of text inside a button.

So for example i have a button which is filled with white and the text is blue. When i click on the button i want these two colors to swap (text inside the button becomes white and the button blue).

I have tried something like this:

 <item style="@style/ButtonText_Menue_Clicked" android:drawable="@drawable/button_menue_default" android:state_focused="true"></item>
 <item style="@style/ButtonText_Menue" android:drawable="@drawable/button_menue_default" ></item>

but it actually does nothing. Is there any way to do what i want, or do i have to do some stuff in the onclik event (but then there is the problem how to set the colors back when the "click is gone" )

like image 633
user3466562 Avatar asked Mar 27 '14 00:03

user3466562


People also ask

How do I change the text color on my Android?

Open your device's Settings app . Text and display. Select Color correction. Turn on Use color correction.

How do I change text color in XML?

Android TextView – Text Color. TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.


1 Answers

Create a selector resource in res/color for the text color and a button selector in res/drawable as below...

text_color.xml

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true"
        android:color="#800000" />
    <item
        android:state_pressed="false"
        android:color="#4C5" />
</selector>

button_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#4C5"/>
        </shape>
    </item>
    <item
        android:state_pressed="false">
        <shape android:shape="rectangle"  >
            <solid android:color="#800000"/>
        </shape>
    </item>
</selector>

Add the button to the layout

 <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:textColor="@color/text_color"
    android:background="@drawable/button_color"/>
like image 142
Libin Avatar answered Dec 31 '22 02:12

Libin