Programming Alert Dialog in Android Studio.

1 22
Avatar for Ideafix
3 years ago

We can program an alert dialog and configure it to show us more finished our work, example changing the Title, the body of the message, the icon, the color of the letter and even make it self-hidden when a certain time passes, this is the code that I leave you to those who can be useful

public void AlertDialogSelfHidden() {

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

builder.setTitle("Title")

.setMessage("Message")

.setCancelable(false).setCancelable(false)

.setIcon(R.drawable.ic_menu_add)

.setPositiveButton("CANCEL", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {

//this is for skip dialog

dialog.cancel();

}

});

final AlertDialog alertDialog = builder.create();

alertDialog.show();

new Handler().postDelayed(new Runnable() {

@Override

public void run() {

if (alertDialog.isShowing()){

alertDialog.dismiss();

}

}

}, 3000); //change 3000 with a specific time you want to state showing the alert dialog.
}

setTitle("Title") it set the Title to Alert dialog.

setMessage("Message") set the body message of Alert.

We need to can't cancel Alert with setCancelable(false).setCancelable(false).

setIcon setup an icon to dialog.

Last things to do is create a Handler to manage time the Alert Dialog is showing.

I hope can help some body.

Thanks.

0
$ 0.00

Comments

Thankyou, but this is too short, not a real article, i will not put this to our community. Maybe you should make a tutorial-series about these, and post it as one article.

$ 0.00
3 years ago