Tuesday, March 15, 2016

android - How to blink TextView text

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="#f7fffa"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:fontFamily="sans-serif-condensed"
        android:textColor="@android:color/black"
        android:text="This text will be blink\nafter clicking the button."
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Blink The Text"
        android:layout_below="@id/tv"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.os.Handler;



public class MainActivity extends AppCompatActivity {

    private Context mContext;
    private Activity mActivity;

    private RelativeLayout mRelativeLayout;
    private TextView mTextView;
    private Button mButton;

    private Handler mHandler;
    private Runnable mRunnable;
    private int mInterval = 300; // milliseconds
    private boolean initialState = true;


    @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);
        mButton = (Button) findViewById(R.id.btn);

        // Initialize the Handler
        mHandler = new Handler();

        // Set a click listener for button
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                    Runnable
                        Represents a command that can be executed. Often used to run code in a
                        different Thread.

                    Thread
                        A Thread is a concurrent unit of execution. It has its own call stack for
                        methods being invoked, their arguments and local variables. Each application
                        has at least one thread running when it is started, the main thread, in the
                        main ThreadGroup. The runtime keeps its own threads in the system thread group.

                        There are two ways to execute code in a new thread. You can either subclass
                        Thread and overriding its run() method, or construct a new Thread and pass a
                        Runnable to the constructor. In either case, the start() method must be
                        called to actually execute the new Thread.

                */
                // Initialize the Runnable
                mRunnable = new Runnable() {
                    /*
                        public abstract void run ()
                            Starts executing the active part of the class' code. This method is
                            called when a thread is started that has been created with a class which
                            implements Runnable.
                    */
                    @Override
                    public void run() {
                        // Do the task
                        doTask();
                    }
                };

                /*
                    public final boolean postDelayed (Runnable r, long delayMillis)
                        Causes the Runnable r to be added to the message queue, to be run after the
                        specified amount of time elapses. The runnable will be run on the thread to
                        which this handler is attached. The time-base is uptimeMillis(). Time spent
                        in deep sleep will add an additional delay to execution.
                */
                // Schedule the task
                mHandler.postDelayed(mRunnable,mInterval);
            }
        });
    }

    // Custom method to do a task
    protected void doTask(){
        if(initialState){
            // Reverse the boolean
            initialState = false;
            // Set the TextView color to red
            mTextView.setTextColor(Color.RED);
        }else {
            // Reverse the boolean
            initialState = true;
            // Change the TextView color to initial State
            mTextView.setTextColor(Color.BLACK);
        }

        // Schedule the task
        mHandler.postDelayed(mRunnable,mInterval);
    }
}