Count down timer and Text blinking in Android


Res/layout/main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent" >

    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Hello World, MyActivity" />

    <Button
            android:id="@+id/btnStartTime"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Start Timer"
            android:layout_margin="5dp"
            android:gravity="center" />

    <TextView
            android:id="@+id/tvTimeCount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="00:00"
            style="@style/normalText" />

</LinearLayout>

Res/values/style.xml


<resources>

    <style name="normalText" parent="@android:style/TextAppearance">
        <item name="android:textSize">19sp</item>
        <item name="android:typeface">sans</item>
        <item name="android:textColor">#FFFFFF</item>
    </style>

    <style name="blinkText" parent="normalText">
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">#FF0000</item>
    </style>

</resources>

src/com.example/MyActivity.java

package com.example;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MyActivity extends Activity
{

    Button buttonStartTime;                 // clicking this button will start time count down 
    TextView textViewShowTime;              // will show the time 
    CountDownTimer countDownTimer;          // built in android class CountDownTimer
    long totalTimeCountInMilliseconds;      // total count down time in milliseconds 
    long timeBlinkInMilliseconds;           // start time of start blinking 
    boolean blink;                          // controls the blinking .. on and off 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        getReferenceOfViews ();                         // get all views 
        setActionListeners ();                          // set action listerns 

        totalTimeCountInMilliseconds = 60 * 1000;      // time count for 3 minutes = 180 seconds
        timeBlinkInMilliseconds = 30 * 1000;            // blink starts at 1 minutes = 60 seconds
    }

    private void setActionListeners() {

        buttonStartTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                textViewShowTime.setTextAppearance(getApplicationContext(), R.style.normalText);

                countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
                    // 500 means, onTick function will be called at every 500 milliseconds 

                    @Override
                    public void onTick(long leftTimeInMilliseconds) {
                        long seconds = leftTimeInMilliseconds / 1000;

                        if ( leftTimeInMilliseconds < timeBlinkInMilliseconds ) {
                            textViewShowTime.setTextAppearance(getApplicationContext(), R.style.blinkText);
                            // change the style of the textview .. giving a red alert style 

                            if ( blink ) {
                                textViewShowTime.setVisibility(View.VISIBLE);
                                // if blink is true, textview will be visible
                            } else {
                                textViewShowTime.setVisibility(View.INVISIBLE);
                            }

                            blink = !blink;         // toggle the value of blink
                        }

                        textViewShowTime.setText(String.format("%02d", seconds / 60) + ":" + String.format("%02d", seconds % 60));
                        // format the textview to show the easily readable format
                    }

                    @Override
                    public void onFinish() {
                        // this function will be called when the timecount is finished
                        textViewShowTime.setText("Time up!");
                        textViewShowTime.setVisibility(View.VISIBLE);
                    }

                }.start();

            }
        });
    }

    private void getReferenceOfViews() {

        buttonStartTime = (Button) findViewById(R.id.btnStartTime);
        textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
    }
}

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example"
          android:versionCode="1"
          android:versionName="1.0">
    <application android:label="@string/app_name" >
        <activity android:name="MyActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest> 

Advertisements

5 thoughts on “Count down timer and Text blinking in Android

  1. I love Count down timer and Text blinking in Android | Solved Programming Problems
    Hello, i feel that i noticed you visited my site so i came to go back the choose?.I am attempting to in finding issues to enhance
    my site!I suppose its good enough to use some of your concepts!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s