Wednesday, 14 May 2014

How To Show toast after every 5 seconds

package com.irads5.toast;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {
WebView mWebView;
Timer t ;
TimerTask task ;
TextView mTextField;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);




Button b1=(Button)findViewById(R.id.btn1);
Button b2=(Button)findViewById(R.id.btn2);

mTextField=(TextView)findViewById(R.id.textview1);
b1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
t=new Timer();
task = new TimerTask() {
  @Override
  public void run() {
  runOnUiThread(new Runnable() {

    @Override
    public void run() {
    Toast.makeText(Main.this, "5 sec", Toast.LENGTH_LONG).show();
    }
   });
  }
 };

 t.scheduleAtFixedRate(task, 0, 5000);

}
});
b2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
task.cancel();
t.cancel();
}
});


}

}

No comments:

Post a Comment