Home > Android, Resources > Creating a multiple options dialog in Android

Creating a multiple options dialog in Android

Below is the code for creating a multiple options dialog in Android. This dialog is triggered by clicking an option from a context menu in my application which determines what Activity class my app should direct to.


final String items[] = {“Movie”,”Music”,”Book”};
AlertDialog.Builder ab=new AlertDialog.Builder(SmugActivity.this);
ab.setTitle(“Dialog Title”);
ab.setItems(items, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface d, int choice) {
if(choice == 0) {
Intent mainIntent = new Intent(SampleActivity.this, MovieActivity.class);
SampleActivity.this.startActivity(mainIntent);
SampleActivity.this.finish();
}
else if(choice == 1) {
Intent mainIntent = new Intent(SampleActivity.this, MusicActivity.class);
SampleActivity.this.startActivity(mainIntent);
SampleActivity.this.finish();
}
else if(choice == 2) {
Intent mainIntent = new Intent(SampleActivity.this, BookActivity.class);
SampleActivity.this.startActivity(mainIntent);
SampleActivity.this.finish();
}
}
});
ab.show();

The code above should look like this:

Advertisements
Categories: Android, Resources
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: