package com.example.timerbutton;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv1 = (TextView) findViewById(R.id.textView1);
Button bt1 = (Button) findViewById(R.id.button1);
// Timer object creation and Staring;
final Timer timer = new Timer();
// Handler object declaration
final Handler h = new Handler();
bt1.setOnClickListener(new OnClickListener() {
int counter = 0;
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// Timer task creation which will be executed on timer schedule
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
h.post(new Runnable() {
public void run() {
// Do something
if (counter <= 5) {
counter++;
// Android UI get Updated continually
tv1.setText("The counter is " + counter);
} else {
// If condition full filled the timer will
// stop here
//timer.cancel();
//timer.purge();
}
}
});
}
};
// Timer will schedule with task and periodical time interval.
timer.schedule(timerTask, 5000, 5000);
}
});
}
}
Layout
<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/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="TextView" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="45dp"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_alignParentLeft="true"
android:text="start" />
</RelativeLayout>
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv1 = (TextView) findViewById(R.id.textView1);
Button bt1 = (Button) findViewById(R.id.button1);
// Timer object creation and Staring;
final Timer timer = new Timer();
// Handler object declaration
final Handler h = new Handler();
bt1.setOnClickListener(new OnClickListener() {
int counter = 0;
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// Timer task creation which will be executed on timer schedule
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
h.post(new Runnable() {
public void run() {
// Do something
if (counter <= 5) {
counter++;
// Android UI get Updated continually
tv1.setText("The counter is " + counter);
} else {
// If condition full filled the timer will
// stop here
//timer.cancel();
//timer.purge();
}
}
});
}
};
// Timer will schedule with task and periodical time interval.
timer.schedule(timerTask, 5000, 5000);
}
});
}
}
Layout
<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/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="TextView" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="45dp"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_alignParentLeft="true"
android:text="start" />
</RelativeLayout>
No comments:
Post a Comment