Change the Textview Text Color When Button Is Clicked in Android

How to change the textView text color on button click

Simply apply this in your java code

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setTextColor(Color.BLUE);
}
});

change the color of TextView when clicking on Button

In the onClick method of the button you are using you can something like this:

TextView title1 = findViewById(R.id.myTextView);
//Your "onclick" method
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

title1.setTextColor(Color.MyColor); //changing text color
}
});

Here instead of myTextView, use your TextView id. Instead of MyColor choose your color, you either have to use the built in colors(i.e. WHITE, BLACK) or add a color of your own in values\colors.xml file with your desired name and use that here.

How to change text color when Button/TextView is pressed in android

Try this:

TextView

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/btn_add"
android:background="@drawable/background"
android:gravity="left"
android:paddingBottom="10dp"
android:state_selected="true"
android:paddingLeft="10dp"
android:paddingRight="5dp"
android:paddingTop="10dp"
android:text="@string/student_name"
android:textColor="@drawable/my_text_color"
android:textSize="18dp" />

drawable/my_text_color.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/black"/>

</selector>

In your class file add the following code,

textView.setselected(true);

Android: how can I change the color of a textview when a button is pressed with selector XML

You mentioned you wish to change the color of textview but never mentioned color of text in textview. this can be done very easily using the same selector file. Implement this way for proper implementation, so you can just get things done inside onclicklistener no need to handle button states. Modify your selector file as

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/default_blue" android:color="@color/default_blue"/>
<item android:state_pressed="false" android:drawable="@color/default_color" android:color="@color/default_color"/>
</selector>

in your layout xml for textview set

android:textColor = "@drawable/buttons_drawable_resource"

Note: Do not skip to add android:state_pressed="false" in selector as i gave above with default selections for button as well as textview text color else it will crash with NPE.



Related Topics



Leave a reply



Submit