Search code examples
androidbuttontext

How to change the textView text color on button click


I am having a button with a text under it. How can I change the color of the text in the textview on Button click? Does it need to be added in the selector? Or within the java code?

Here is the selector:

<?xml version="1.0" encoding="utf-8"?>

<item android:state_pressed="false">
    <shape android:shape="oval">
        <solid
            android:color="@color/blue_800"/>
    </shape>
</item>
<item android:state_pressed="true">
    <shape android:shape="oval">
        <solid android:color="@color/blue_300"/>
    </shape>
</item>

And the layout so far:

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<Button
    android:id="@+id/imageUploader1"
    android:background="@drawable/round_button"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginRight="2dp"
    android:layout_marginLeft="2dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Main"
    android:layout_gravity="center"/>
</LinearLayout>

Solution

  • Simply apply this in your java code

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