Package com.example.dialog2;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.app.Dialog;
Import Android.content.DialogInterface;
Import Android.view.Menu;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.Toast;
public class Mainactivity extends Activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Final string[] Arrayfruit = new string[] {"Apple", "orange", "Strawberry", "banana"};
Dialog Alertdialog = new Alertdialog.builder (this).
Settitle ("What kind of fruit do you like to eat?") ")
. SetIcon (R.drawable.ic_launcher)
Items and Setmessage cannot be used at the same time
. Setitems (Arrayfruit, New Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Toast.maketext (Mainactivity.this, Arrayfruit[which], Toast.length_short). Show ();
}
}).
Setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
}
}).
Create ();
Alertdialog.show ();
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
}
Android with ListView dialog box