Change textview color using switch

0

I'd like to know how to change the color of a texview using a switch . When selecting Switch , the word on the side stands out with another color in 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 code above would be to change the color of Hello world , and when you click back, it returns to the "original" color.

    
asked by anonymous 25.08.2018 / 14:39

1 answer

2

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

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: link

    
27.08.2018 / 14:24