user6456773
user6456773

Reputation: 381

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>

Upvotes: 0

Views: 4653

Answers (2)

sumanth.js
sumanth.js

Reputation: 414

    //here is a TextView and two Buttons with different color
<TextView
    android:id="@+id/txt"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:text="Hello World"
    android:gravity="center"
    android:textSize="24sp"
    android:fontFamily="sans-serif-black"
    android:letterSpacing="0.03"
    android:layout_marginTop="30dp"
    />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

    <Button
        android:id="@+id/red"
        android:layout_width="150dp"
        android:layout_height="80dp"
        android:text="Red"
        android:fontFamily="sans-serif-black"
        android:textSize="22sp"
        android:backgroundTint="@color/Red"
        android:textColor="@color/white"
        />

    <Button
        android:id="@+id/green"
        android:layout_width="150dp"
        android:layout_height="80dp"
        android:text="Red"
        android:fontFamily="sans-serif-black"
        android:textSize="22sp"
        android:backgroundTint="@color/Green"
        android:textColor="@color/white"
        />

</LinearLayout>
    //here is a java code
    public class MainActivity extends AppCompatActivity {
    TextView text;
    Button red, green;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (TextView)findViewById(R.id.txt);
        red = (Button)findViewById(R.id.red);
        green = (Button)findViewById(R.id.green);

        red.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text.setTextColor(Color.parseColor("#FF0000"));
            }
        });
        green.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text.setTextColor(Color.parseColor("#04AF0A"));
            }
        });
    }

Upvotes: 0

Sudip Podder
Sudip Podder

Reputation: 838

Simply apply this in your java code

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

Upvotes: 4

Related Questions