Saturday, March 12, 2016

android - How to set TextView disabled color

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:background="#f1fbfc"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:layout_margin="10dp"
        android:padding="10dp"
        android:textStyle="bold"
        android:fontFamily="sans-serif-condensed"
        android:textColor="@color/textview_colors"
        />
    <ToggleButton
        android:id="@+id/tb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="Disable TextView"
        android:textOff="Enable TextView"
        android:layout_below="@id/tv"
        />
</RelativeLayout>
res/color/textview_colors.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="false"
        android:color="#9e9e9e"
        />
    <item
        android:state_enabled="true"
        android:color="#56934d"
        />
</selector>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;


public class MainActivity extends AppCompatActivity {

    private Context mContext;
    private Activity mActivity;

    private RelativeLayout mRelativeLayout;
    private TextView mTextView;
    private ToggleButton mToggle;


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

        // Get the application context
        mContext = getApplicationContext();

        // Get the activity
        mActivity = MainActivity.this;

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mTextView = (TextView) findViewById(R.id.tv);
        mToggle = (ToggleButton) findViewById(R.id.tb);

        // Change the TextView text depending on enable disable
        if(mTextView.isEnabled()){
            mTextView.setText("TextView is now Enabled.");
            // On the toggle button
            mToggle.setChecked(true);
        }else {
            mTextView.setText("TextView is now Disabled.");
            // Off the toggle button
            mToggle.setChecked(false);
        }

        // Set a check change listener for toggle button
        mToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){ // If the toggle is on
                    // Enable the TextView
                    mTextView.setEnabled(true);
                    mTextView.setText("TextView is now Enabled.");
                } else { // If the toggle is off
                    // Disable the TextView
                    mTextView.setEnabled(false);
                    mTextView.setText("TextView is now Disabled.");
                }
            }
        });
    }
}