Cdacians

Cdacians
Cdacians

Saturday 18 August 2012

Android Timer Thread Example in Android Programming


This example prints alphabets from a name after an interval of 5 seconds each using timer threads.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it TimerThreadExample.
2.) Write following into main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:padding="@dimen/padding_medium"
        tools:context=".TimerThreadExample"
        android:textSize="40pt" />
</RelativeLayout>
3.) Run for output.
Steps:
1.) Create a project named TimerThreadExample and set the information as stated in the image.
Build Target: Android 4.0
Application Name: TimerThreadExample
Package Name: com. example. TimerThreadExample
Activity Name: TimerThreadExample
Min SDK Version: 8
2.) Open TimerThreadExample.java file and write following code there:
package com.example.timerthreadexample;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class TimerThreadExample extends Activity {
        protected int splashTime = 3000;
        TextView tv1;
        String[] name = {"A","N","D","R","O","I","D"};
        int timer =0;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                tv1 = (TextView) findViewById(R.id.textView1);
                Thread th=new Thread(){
                        @Override
                        public void run(){
                                try
                                {
                                        for (timer = 0; timer < 7; timer++)
                                        {
                                                int waited = 0;
                                                while(waited < splashTime)
                                                {
                                                        Thread.sleep(100);
                                                        runOnUiThread(newRunnable() {
                                                                @Override
                                                                public void run(){
                                                                        try {
                                                                                tv1.setText(name[timer]);
                                                                        }
                                                                       catch(Exception e)
                                                                        {
                                                                                e.printStackTrace();
                                                                        }
                                                                }
                                                        });
                                                        waited += 100;
                                                }
                                        }
                                }catch (InterruptedException e) {
                                }
                        }
                };
                th.start();
        }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }  
}
3.) Compile and build the project.
Output

Thanks
akm
www.cdacians.com

No comments:

Post a Comment