Reputation: 380
I want to add a toast say after 30 seconds when a button is clicked. Can you please help me out.
Upvotes: 4
Views: 2508
Reputation: 3140
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// Toast message here
}
}, 500);
Upvotes: 1
Reputation: 18923
you can implement this on button click event.
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(Activity.this, "Button Clicked", Toast.LENGTH_LONG).show();
}, 3000);
}
});
Upvotes: 1
Reputation: 1283
You can start a timer for 30 second after the button is clicked and then in onFinish() method you can display the toast message.
public class TestActivity extends Activity{
private MyCounter mCounter;
private Button mBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
mBtn=(Button)findViewById(R.id.btn);
mBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mCounter=new MyCounter(40000, 10000);
mCounter.start();
}
});
}
private class MyCounter extends CountDownTimer{
public MyCounter(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Display your text here", Toast.LENGTH_SHORT).show();
}
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
Log.i("on tick>>>>>>",millisUntilFinished+">>>>>");
}
}
}
Upvotes: 0
Reputation: 3188
You can use a Handler
with postDelayed()
. You can find the documentation here
For example:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// Put your Toast here
}
}, 30 * 1000);
You have to watch out which Thread your Handler
is running on. If you want to make UI modifications (like the Toast
), you have to attach the Handler
on your UI-Thread.
Upvotes: 5
Reputation: 11254
Something like that:
Button button = new Button(this);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(context, "Hello!", Toast.LENGTH_LONG).show();
}
}, 30000);
}
});
Upvotes: 2
Reputation: 9700
You can use postDelayed()
method of Handler
...pass a Thread
and specific time after which time the Thread
will be executed as below...
private Handler mTimerHandler = new Handler();
private Runnable mTimerExecutor = new Runnable() {
@Override
public void run() {
Toast.makeText(Activity.this, "Button Clicked", Toast.LENGTH_LONG).show().
}
};
Then call as below inside the onClick()
method...
public void onClick(View view) {
mTimerHandler.postDelayed(mTimerExecutor, 30000);
}
Upvotes: 2