Creating listener of a CustomDialog out of the class in Android -
i trying create listener of button of customdialog extends dialogfragment class , want locate listener of custom buttons out of customdialog fragment class .however when try call view of customdialog fragment getting null exception. create new instance of customdialog fragment in somewhere else , say
customdialog.getview().findviewbyid(r.id.custombutton);
but getting null.
public class customdialog extends dialogfragment { public final int res_none = -1; private textviewcustomfont dialogtitle, view2, dialogbodybottom, dialogbodytop; private edittextcustomfont dialogedittext; private buttoncustomfont dialogleftbutton; private buttoncustomfont dialogrightbutton; private typeface gothambold, gothammedium, gothamultra; private static int title1, bodytop1, bodybottom1, edittexthint1, leftbutton1, rightbutton1; onsubmitlistener mlistener; private dialog dialog; interface onsubmitlistener { void setonsubmitlistener(string arg); } public static customdialog newinstance(int title, int bodytop, int bodybottom, int edittexthint, int leftbutton, int rightbutton) { title1 = title; bodytop1 = bodytop; bodybottom1 = bodybottom; edittexthint1 = edittexthint; leftbutton1 = leftbutton; rightbutton1 = rightbutton; customdialog frag = new customdialog(); return frag; } public buttoncustomfont getdialogleftbutton() { return dialogleftbutton; } @override public dialog oncreatedialog(bundle savedinstancestate) { dialog = new dialog(getactivity()); dialog.getwindow().requestfeature(window.feature_no_title); dialog.getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); //dialog.setcontentview(r.layout.dialog_layout); //dialog.show(); initlayout(); return dialog; } private void initlayout(){ dialog.setcontentview(r.layout.dialog_layout); setdialogview(); setcustomdialog(); } public void setdialogview(){ //create java object of each dialog view item dialogtitle = (textviewcustomfont) dialog.findviewbyid(r.id.custom_dialog_title); dialogbodytop = (textviewcustomfont) dialog.findviewbyid(r.id.custom_dialog_body_top); dialogbodybottom = (textviewcustomfont) dialog.findviewbyid(r.id.custom_dialog_body_bottom); dialogedittext = (edittextcustomfont) dialog.findviewbyid(r.id.custom_dialog_body_et); dialogleftbutton = (buttoncustomfont) dialog.findviewbyid(r.id.custom_dialog_body_btn_left); dialogrightbutton = (buttoncustomfont) }
public class loginselectionfragment extends fragment { public static loginselectionfragment newinstance() { loginselectionfragment fragment = new loginselectionfragment(); return fragment; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_loginselection, container, false); }
i trying pull dialogleftbutton of customdialog fragment , assing listener on inside loginselectionfragment.
here how looks after added method 2. part of loginselectionfragment
private void twobuttontextedittextdialog(){ string title = getresources().getstring(r.string.invalid_info_header); string body = getresources().getstring(r.string.invalid_info_body); string body2 = getresources().getstring(r.string.hint_newemail); string btn1 = getresources().getstring(r.string.cancel_uppercase); string btn2 = getresources().getstring(r.string.ok_alert); fragmentdialog = customdialog.newinstance(title, body, body2, res_none, btn1, btn2); fragmentdialog.setcustomdialogfragmentlistener(mdialogclicklistener); fragmentdialog.show(getfragmentmanager(), ""); } private customdialog.customdialogfragmentlistener mdialogclicklistener = new customdialog.customdialogfragmentlistener(){ @override public void onnegativeclick() { // todo auto-generated method stub fragmentdialog.dismiss(); } @override public void onpositiveclick() { // todo auto-generated method stub fragmentdialog.dismiss(); } }; @override public void onnegativeclick() { // todo auto-generated method stub } @override public void onpositiveclick() { // todo auto-generated method stub }
you create method setdialogbuttonclicklistener(customdialog.onbuttonclicklistener clicklistener);
customdialog.onbuttonclicklistener
inner static interface , way listen click events of buttons anywhere.
an example of below,
public class customdialog extends dialogfragment { ..... public static customdialog newinstance(int title, int bodytop, int bodybottom, int edittexthint, int leftbutton, int rightbutton) { customdialog frag = new customdialog(); bundle args = new bundle(); args.putint("title", title); ...... frag.setarguments(args); return frag; } ... @override public dialog oncreatedialog(bundle savedinstancestate) { int title = getarguments().getint("title"); return new alertdialog.builder(getactivity()) .seticon(android.r.drawable.ic_dialog_alert) .settitle(title) .setpositivebutton(android.r.string.ok, this) .setnegativebutton(android.r.string.cancel, this) .... .create(); } @override public void onclick(dialoginterface dialog, int which) { if (listener != null) { switch (which) { case dialoginterface.button_positive: listener.onrightbuttonclick(); default: listener.onleftbuttonclick(); } } } ... private customdialog.onbuttonclicklistener mclicklistener; .... public void setdialogbuttonclicklistener(customdialog.onbuttonclicklistener clicklistener){ mclicklistener = clicklistener; } ... public static interface onbuttonclicklistener { public void onleftbuttonclick(); public void onrightbuttonclick(); } }
if notice above sample posted , have besides solving problem of setting click listener on buttons have introduced factory design pattern on android , can see instead of creating static
fields button title , dialog title i've set them in bundle argument , retrieve them in dialogs oncreate() method.
for more best practices of fragment can take here
edit
ok , providing glimpse of loginselectionfragment should like.
public class loginselectionfragment extends fragment implements customdialog.onbuttonclicklistener { ......// method 1 public void showdialog(string title , string message .....) { customdialog dialog = customdialog.getinstance(title , message...); dialog.setdialogbuttonclicklistener(this); dialog.show(getsupportfragmentmanager(), null); } public void onleftbuttonclick(){ ...// on left button click of dialog } public void onrightbuttonclick(){ // on right button click of dialog .. } // method 2 public void showdialog2(string title , string message .....) { customdialog dialog = customdialog.getinstance(title , message...); dialog.setdialogbuttonclicklistener(mdialogclicklistener); dialog.show(getsupportfragmentmanager(), null); } private final customdialog.onbuttonclicklistener mdialogclicklistener = new customdialog.onbuttonclicklistener() { public void onleftbuttonclick(){ ...// on left button click of dialog } public void onrightbuttonclick(){ // on right button click of dialog .. } } }
now if @ method 1 , have given parameters showdialog() method reuse showing multiple times different arguments ie., use approach when want show same dialog different title , message etc
and in method 2 have provided anonymous inner class handling click events many anonymous inner classes have different varieties of dialog ie dialog different ui , different event listeners in same activity/fragment.
enjoy!
Comments
Post a Comment