Summary Notes for various android dialogs
Confirm cancel dialog box (with ICON)
// (Context, topic) new AlertDialog. builder (this, AlertDialog. THEME_DEVICE_DEFAULT_LIGHT ). setTitle ). setMessage (content ). setIcon (R. drawable. ic_launcher) // responds to the click event. setPositiveButton (OK, new OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stub Toast. makeText (MainActivity. this, OK, 0 ). show ();}}). setNegativeButton (cancel, null ). show (); // do not forget to showEffect
Simple radio dialog box
Final String [] strs = new String [] {male, female, don't tell you}; new AlertDialog. builder (this, AlertDialog. THEME_DEVICE_DEFAULT_LIGHT ). setTitle ). setIcon (R. drawable. ic_launcher) // (String array, default option, RESPONSE event ). setSingleChoiceItems (strs, 2, new OnClickListener () {// swich: array id of the selected option @ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stubToast. makeText (MainActivity. this, strs [which], 0 ). show ();}}). show ();Effect
Multiple choice dialog box
Final String [] strs = new String [] {bananas, apples, pears}; AlertDialog. builder dialog = new AlertDialog. builder (this, AlertDialog. THEME_DEVICE_DEFAULT_LIGHT ). setTitle ). setIcon (R. drawable. ic_launcher ). setMultiChoiceItems (strs, new boolean [] {true, true, false}, new OnMultiChoiceClickListener () {// which: The selected array id // isChecked: selected status @ Override public void onClick (DialogInterface dialog, int which, boolean isChecked) {// TODO Auto-generated method stub Log. d (test, which = + which +: isChecked = + isChecked) ;}}); dialog. create (). show ();Effect
Progress dialog box
ProgressDialog pd = new ProgressDialog (this); pd. setTitle (title); pd. setMessage (please bring it to...); pd. show ();
Progress bar dialog box
Final ProgressDialog pd = new ProgressDialog (this); // there are only two types of topic pd. setProgressStyle (ProgressDialog. STYLE_HORIZONTAL); // sets the maximum progress value of pd. setMax (1, 100); pd. setTitle (title); pd. setMessage (please include .....); pd. show (); new Thread () {public void run () {for (int I = 1; I <100; I ++) {try {Thread. sleep (100);} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace ();} pd. setProgress (I);} // can be disabled in any thread, pd. dismiss ();};}. start ();Effect
Custom dialog box
Final EditText et_text = new EditText (this); new AlertDialog. builder (this ). setTitle (enter) // enter a TextView. setIcon (android. r. drawable. ic_dialog_info ). setView (et_text ). setPositiveButton (OK, new OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stub Toast. makeText (MainActivity. this, et_text.getText (). toString (), 0 ). show ();}}). setNegativeButton (cancel, null ). show ();
Effect