Saturday, June 14, 2014

create timer control in android code

Here i share How to create timer control in android

1 First Create XML File "activity_main.xml" activity_main.xml
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Timer Control Example :" />
    
                  android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginTop="50dp"
           android:id="@+id/mClock">



2 Create Java Class file "MainActivity.java" for create timer control in android MainActivity.java
package com.bhansalisoft.testapp;

import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView mClock;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mClock = (TextView)findViewById(R.id.mClock);
    }
    
    private Handler mHandler = new Handler();
    private Runnable timerTask = new Runnable() {
        @Override
        public void run() {
            Calendar now = Calendar.getInstance();
            mClock.setText(String.format("%02d:%02d:%02d",
                    now.get(Calendar.HOUR),
                    now.get(Calendar.MINUTE),
                    now.get(Calendar.SECOND)) );
            mHandler.postDelayed(timerTask,1000);
        }
    };
    
    @Override
    public void onResume() {
        super.onResume();
        mHandler.post(timerTask);
    }
    
    @Override
    public void onPause() {
        super.onPause();
        mHandler.removeCallbacks(timerTask);
    }
    
    
    
}




If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website