Android Tutorial : Creating AlertDialog for Android Application

I believe you guys reach here after searching on how to create an AlertDialog for Android. There are few types of Dialog (or also can consider as a Pop Up) is available in Android SDK. The easiest to work with is AlertDialog.
This is the AlertDialog: 



To do this is very simple. Below are the code snippet to create a simple AlertDialog for Android :
(FYI: I'll skip all the import stuff as you can do it generally by Crtl + Shift + O / Organize Import - a function in eclipse to fill in all the import stuff for you )

// define a list of array
final CharSequence[] shareItems = {"Facebook", "Twitter", "Email"};

// initiate a alert dialog builder
AlertDialog.Builder builder = new AlertDialog.Builder(MediaNewsDetailActivity.this);
builder.setTitle("Share this");  // set title for the AlertDialog
builder.setItems(shareItems, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
    // TODO Auto-generated method stub
   Toast.makeText(MediaNewsDetailActivity.this, shareItems[item]+" clicked.", Toast.LENGTH_SHORT).show(); // show Toast message for each click on the list of item in the alert dialog
        }
});
 AlertDialog shareAlert = builder.create();
shareAlert.show(); // show the AlertDialog when it is triggered


Finally your AlertDialog is created. Next I'll post the tutorial on how to create an AlertDialog with images at each list items. Stay tuned and have a nice coding time. 

Comments

Popular posts from this blog

Django Form: MultipleChoiceField and How To Have Choices From Model Example

Jquery Validation fix for date format dd/mm/yyyy in Chrome and Safari

DJango Queryset comparing date time