How can I reuse an AlertDialog for Yes/No on Android? -


i'm trying find way reuse dialog shows customized titles, send yes/no click function has launched dialog.

i have 2 buttoms, save , dismiss, , both call yes/no dialog, 1 showing "do want save" , other "dismiss changes?".

i think procedure "dirty" guess can work, problem "view view" variable, don't know how pass activity dialog, can use recall function launched dialog.

thanks in advance, hernihdez

.java of activity (fragment of it)

public void open_hh_fragment_yesno(view view, string aux_title, string aux_function) {     bundle bundle=new bundle();     bundle.putstring("setmessage", aux_title);     bundle.putstring("callingfunction", aux_function);      dialogfragment newfragment = new hh_fragment_yesno();     newfragment.setarguments(bundle);     newfragment.show(getsupportfragmentmanager(), "hh_fragment_yesno"); }  public void savechanges(view view, string aux_yesno) {     if (aux_yesno == "")     {         toast.maketext(this, "save changes?", toast.length_short).show();         open_hh_fragment_yesno(view, "save changes?", "savechanges");     }     else if (aux_yesno == "yes")     {         toast.maketext(this, "saving changes", toast.length_short).show();     }     else if (aux_yesno == "no")     {         toast.maketext(this, "save cancelled", toast.length_short).show();     } }  public void dismisschanges(view view, string aux_yesno) {     if (aux_yesno == "")     {         toast.maketext(this, "dismiss changes?", toast.length_short).show();         open_hh_fragment_yesno(view, "dismiss changes?", "dismisschanges");     }     else if (aux_yesno == "yes")     {         toast.maketext(this, "dismiss ok", toast.length_short).show();         close(view);     }     else if (aux_yesno == "no")     {         toast.maketext(this, "dismiss cancelled", toast.length_short).show();     } }  // dialog fragment receives reference activity through // fragment.onattach() callback, uses call following methods // defined hh_fragment_yesno.yesnodialoglistener interface @override public void ondialogpositiveclick(dialogfragment dialog, view view, string aux_function) {     // user touched dialog's positive button     toast.maketext(this, "user clicked on yes", toast.length_short).show();      if (aux_function == "savechanges")     {         savechanges(view, "yes");     }     else if (aux_function == "dismisschanges")     {         dismisschanges(view, "yes");     } }  @override public void ondialognegativeclick(dialogfragment dialog, view view, string aux_function) {     toast.maketext(this, "user clicked on no", toast.length_short).show();      if (aux_function == "savechanges")     {         savechanges(view, "no");     }     else if (aux_function == "dismisschanges")     {         dismisschanges(view, "no");     } } 

.java of dialog (complete)

public class hh_fragment_yesno extends dialogfragment {     @override     public dialog oncreatedialog(bundle savedinstancestate)     {     // use builder class convenient dialog construction     alertdialog.builder builder = new alertdialog.builder(getactivity());     string setmessage = getarguments().getstring("setmessage");     final string callingfunction = getarguments().getstring("callingfuntion");      builder         .setmessage(setmessage)                                             // r.string.dialog_fire_missiles         .setpositivebutton("sí", new dialoginterface.onclicklistener()      // r.string.fire         {             public void onclick(dialoginterface dialog, int id)             {                 // exit without saving                 mlistener.ondialogpositiveclick(hh_fragment_yesno.this, view, callingfunction);             }         })         .setnegativebutton("no", new dialoginterface.onclicklistener()      // r.string.cancel         {             public void onclick(dialoginterface dialog, int id)             {                 // user cancelled dialog                 mlistener.ondialognegativeclick(hh_fragment_yesno.this, view, callingfunction);             }         });      // create alertdialog object , return     return builder.create(); }   /* activity creates instance of dialog fragment must  * implement interface in order receive event callbacks.  * each method passes dialogfragment in case host needs query it. */ public interface yesnodialoglistener {     public void ondialogpositiveclick(dialogfragment dialog, view view, string aux_function);     public void ondialognegativeclick(dialogfragment dialog, view view, string aux_function); }   // use instance of interface deliver action events yesnodialoglistener mlistener;   // override fragment.onattach() method instantiate noticedialoglistener @override public void onattach(activity activity) {     super.onattach(activity);     // verify host activity implements callback interface     try     {         // instantiate noticedialoglistener can send events host         mlistener = (yesnodialoglistener) activity;     }     catch (classcastexception e)     {         // activity doesn't implement interface, throw exception         throw new classcastexception(activity.tostring() + " must implement noticedialoglistener");     } } } 

complete solution try this

1) createa interface

import android.content.dialoginterface;  public interface alertmagnatic {      public abstract void positivemethod(dialoginterface dialog, int id);     public abstract void negativemethod(dialoginterface dialog, int id); } 

2) generalize method confirm dialog.

public static void getconfirmdialog(context mcontext,string title, string msg, string positivebtncaption, string negativebtncaption, boolean iscancelable, final alertmagnatic target) {         alertdialog.builder builder = new alertdialog.builder(mcontext);          int imageresource = android.r.drawable.ic_dialog_alert;         drawable image = mcontext.getresources().getdrawable(imageresource);          builder.settitle(title).setmessage(msg).seticon(image).setcancelable(false).setpositivebutton(positivebtncaption, new dialoginterface.onclicklistener() {             public void onclick(dialoginterface dialog, int id) {                 target.positivemethod(dialog, id);             }         }).setnegativebutton(negativebtncaption, new dialoginterface.onclicklistener() {             @override             public void onclick(dialoginterface dialog, int id) {                 target.negativemethod(dialog, id);             }         });          alertdialog alert = builder.create();         alert.setcancelable(iscancelable);         alert.show();         if (iscancelable) {             alert.setoncancellistener(new oncancellistener() {                  @override                 public void oncancel(dialoginterface arg0) {                     target.negativemethod(null, 0);                 }             });         }     } 

3) how use

getconfirmdialog(getstring(r.string.logout), getstring(r.string.logout_message), getstring(r.string.yes), getstring(r.string.no), false,                 new alertmagnatic() {                      @override                     public void positivemethod(final dialoginterface dialog, final int id) {}                      @override                     public void negativemethod(dialoginterface dialog, int id) {                      }                 }); 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -