Reputation: 39
I am new in android. I want to use the translate animation in android. I want that the red rounded image comes from center of the layout. It comes . But i want that the red rounded image comes back from the center image which color is green. Thanks in Advance.
Upvotes: 0
Views: 551
Reputation: 15702
final AlertDialog.Builder dialog = new AlertDialog.Builder(this)
.setTitle("Auto-closing Dialog")
.setMessage("After 10 second, this dialog will be closed");
dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
// tasks to do when Confirm clicked
}
});
final AlertDialog alert = dialog.create();
alert.show();
// Hide after 10 seconds
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
@Override
public void run() {
if (alert.isShowing()) {
alert.dismiss();
}
}
};
alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
handler.removeCallbacks(runnable);
}
});
handler.postDelayed(runnable, 10000);
Upvotes: 2
Reputation: 2303
I would recommend you Handler and it's postDelayed method. So the code should look like this
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
if(dialog != null && dialog.isShowing())
dialog.dissmiss();
}
}, 10000);
where handler should be created on the UI thread.
Upvotes: 2
Reputation: 6044
Create a handler, somewhere in activity oncreate method as follows:
handler = new Handler();
Now, show the dialog.
Later use this handler to dismiss dialog after 10 seconds as follows:
handler.postDelayed(new Runnable() {
public void run() {
if (dialog.isShowing())
dialog.dismiss(); // dismiss dialog
}
}, 10000);
Upvotes: 1