Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mobile Computing With Android ACST 4550 Alerts

Similar presentations


Presentation on theme: "Mobile Computing With Android ACST 4550 Alerts"— Presentation transcript:

1 Mobile Computing With Android ACST 4550 Alerts

2 AlertDialog Builder AlertDialog is a class that gives you a fast way to build dialogs with or without your own custom layout. You can use the AlertDialog standard layout, which includes a Title, a Message and up to three Buttons: one Positive, one Negative and one Neutral. When you use this approach you just set the Title, and then set the Message, and give the buttons an onClick method to tell them what to do. Although three buttons are available, you don’t need to use all three of the buttons. Only the buttons you set up will be seen. See AlertDemoView.java and AlertDemoActivity.java For this demo, you will need to create a new project and create an Activity for it named “AlertDemoActivity”, and then place the AlertDemoView.java file in the package, and copy the code from AlertDemoActivity.java into the Activity.

3 Standard AlertDialog Builder Layout
AlertDialog.Builder qBuilder = new AlertDialog.Builder(this); qBuilder.setTitle(titlestring); qBuilder.setMessage(messagestring); qBuilder.setPositiveButton(yesstring, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // positive actions } }); qBuilder.setNegativeButton(nostring, new DialogInterface.OnClickListener() { // negative actions qBuilder.setNeutralButton(cancelstring, new DialogInterface.OnClickListener() { // neutral actions AlertDialog alertDialog = qBuilder.create(); alertDialog.show();

4 AlertDialog Builder By default, the user can click out of the AlertDialog without selecting any of the buttons you have placed in it. To stop a user from doing this, and thereby making the alert act as a “Modal” dialog, you can call either of the following AlertDialog methods, as shown in the demo: setCancelable(false) setCanceledOnTouchOutside(false)

5 AlertDialog Builder Custom Layouts
You can also use the AlertDialog with a custom layout of your own making. To do this you can load it in as an XML layout file and then “inflate” it to a View using the LayoutInflater. To inflate a View means to render it from XML into a native bytecode View: LayoutInflater li = LayoutInflater.from(this); View view = li.inflate(R.layout.xmlfile,null); // instantiate the AlertDialog AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setView(view);


Download ppt "Mobile Computing With Android ACST 4550 Alerts"

Similar presentations


Ads by Google