Change the color of textview using switch

Asked

Viewed 426 times

0

I’d like to know how to change the color of a texview using a switch. When selecting the Switch, the word on the side stands out with another color on Android Studio.

XML code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:checked="false"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:checked="false" />

</android.support.constraint.ConstraintLayout>

Java code:

package com.example.apoll.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

The top code would be to change the color of the Hello world, and when you click back, it returns to the "original".

1 answer

2

First to receive the check/uncheck event from Switch you need to implement the interface OnCheckedChangeListener through the method setOnCheckedChangeListener, within that callback you have access to a boolean indicating whether the Switch is 'checked' or 'unchecked', from there you can change the TextView for the color you desire

final TextView textView = findViewById(R.id.textView);
Switch switch1 = findViewById(R.id.switch1);

// Uma forma de salvar a cor 'original' do TextView
final ColorStateList oldColors =  textView.getTextColors();

switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
                // Verificando se o switch está marcado, caso esteja, alterar a color para 'Color.RED', caso não, restaurar para cor 'original' do TextView
                if (checked) {
                    textView.setTextColor(Color.RED);
                } else {
                    textView.setTextColor(oldColors);
                }
            }
        });

Reference: https://stackoverflow.com/a/6468764/4181975

Browser other questions tagged

You are not signed in. Login or sign up in order to post.