Guide to Creating User Alerts in the Android Programming Environment

Written by:  • Edited by: Simon Hill
Updated May 3, 2011
• Related Guides: Android | Google

Sometimes when programming our application we may need to alert the user of some change in the status of the application such as an error. This section explains the basics of the Google Android AlertDialog and Toast programming messages.

Lets introduce two basic (but very useful!) elements to use in our application: The Toast and the AlertDialog.

The Toast is just a message that will appear in front of the activity you are focused on. This message will appear for a short time, and it will just display a text message.

An AlertDialog its the more classic “Alert” style. This widget will show a message and will not disappear until the user interact with it. With this widget you can construct more complex pop-ups, but this will be explained in other article.

Toasts

Creating a Toast is so simple.

Toast.makeText(ActivityName.this,”Hi! Bright Hub”, Toast.LENGTH_SHORT/LENGTH_LONG).show();

There are 3 parameters in the makeText call. The first one is the Activity class we are calling the Toast, the second parameter is the text you want to show in the toast and the last parameter is the duration of the message, it has 2 values: LENGTH_SHORT or LENGTH_LONG.

Toast
click to enlarge

The LENGTH_SHORT constant will make the message appear for no more than 1-2 seconds the LENGTH_LONG will appear a bit more, 4-5 seconds. Then the message will disappear and the focus of the application will be again in the Activity that called the Toast.

AlertDialog

An AlertDialog is a more complex “Alert-system”, just for now, we will see how to create the simplest ones: A pop-up, with a message and a button. This time, we will have the control of the focus: Clicking on the button we will give the focus back to the application. With this AlertDialogs we can show critical messages to the user, or info about our application. The only limit is your imagination.

alertDialog
click to enlarge

Now Let's have a look at how to create a simple Alert Dialog, like we can see in the image above.

new AlertDialog.Builder(BrightHub.this)

.setTitle("BrightHub Alert!")

.setMessage("Google Android How-to guides in the Bright Hub")

.setNeutralButton("Ok",

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,

int which) {

}

}).show();

To the AlertDialog Builder we can add methods to define the style, format and content of the Dialog.

.setTitle → Sets the title of the pop-up. Just a String

.setMessage → We can add a message. A String

.setNeutralButton → Here we add a simple button. Parameters: The first one is the text that its going to appear in the button, the second one is a Listener. Do you want something happen when you click the button? Here is the place.

Other methods I have no show are available. For example, if we want to add an image to the Dialog we have to use the .setIcon method (passing a Drawable as parameter).

Want more?

In our next article we will see examine more complex Dialogs and how we can add a common view to them.


Comments

Showing all 4 comments
 
Swapnil Bhavekar Nov 14, 2011 5:51 AM
RE: Guide to Creating User Alerts in the Android Programming Environment
Hi, i also want to show alert box on canvas but is not working pls help<br><br> package com.example.circleclick;<br><br>import android.app.Activity;<br>import android.content.Context;<br>import android.graphics.Canvas;<br>import android.graphics.Color;<br>import android.graphics.Paint;<br>import android.app.AlertDialog;<br>import android.content.DialogInterface;<br>import android.os.Bundle;<br>import android.view.View;<br><br>public class circleclick extends Activity<br>{<br>    DemoView demoview;<br>    /** Called when the activity is first created. */<br>    @Override<br>    public void onCreate(Bundle savedInstanceState) {<br>        super.onCreate(savedInstanceState);<br>        demoview = new DemoView(this);<br>        setContentView(demoview);<br>    }<br><br>    private class DemoView extends View<br>    {<br>        public DemoView(Context context)<br>        {<br>            super(context);<br>        }<br>        @Override protected void onDraw(Canvas canvas) <br>        {<br>            super.onDraw(canvas);<br><br>            Paint paint = new Paint();<br>            paint.setStyle(Paint.Style.FILL);<br>            //declare array of nodes<br>            int[]nodes=new int[15];<br>           <br>            //calculate the length of the array<br>            int length=nodes.length;<br>            // make the entire canvas white<br>            paint.setColor(Color.WHITE);<br>            canvas.drawPaint(paint);<br>           <br>            paint.setColor(Color.BLACK);<br>            paint.setStyle(Paint.Style.STROKE);<br>            canvas.drawCircle(200, 200, 150, paint);<br>           <br>            int x=200;<br>            int y=200;<br>            int radius=150;<br>            double anglerad =0;<br>            anglerad =2*Math.PI/length;<br>           <br>            for(int i=0;i&lt;length;i++)&lt;br&gt;            {<br>                double rx1=radius*Math.sin(90);<br>                double ry1=radius*Math.cos(90);<br>                float cxx
xyz Mar 29, 2011 8:37 AM
Alert Dialog
I want to call a screen from ok button of alerdialog box , it is in a class which extends AsyncTask.
Jbeerdev Mar 28, 2010 1:29 PM
RE: Guide to Creating User Alerts in the Android Programming Environment
Hi Usama

You can use Timers in Android. Check this

http://developer.android.com/resources/articles/timed-ui-updates.html

Hope it helps!
Usama Mar 22, 2010 4:40 AM
Alert Dialog
I want to create an alert dialog with count down timer.
timer starts from 60 sec, when timer is expired, i should b able to go to next activity.
can you help me out?
 
blog comments powered by Disqus
Email to a friend