java - Confirm dialog before exit android studio -
hi guys have exit button on main activity xml need when user clicks it, opens dialogue confirm exit, if yes exit. if no take them back.
here's code far, don't have of part don't know start.
package com.tradingsoftwarelimited.labelprinter10; import android.app.activity; import android.app.alertdialog; import android.content.dialoginterface; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.view; import android.widget.button; import android.widget.toast; public class mainactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //sets edit , data button hidden button hiddenbutton = (button) findviewbyid(r.id.editbutton); hiddenbutton.setvisibility(view.invisible); button hiddenbutton1 = (button) findviewbyid(r.id.databutton); hiddenbutton1.setvisibility(view.invisible); //acknolages settings button button showbutton = (button) findviewbyid(r.id.settingsbutton); //if clicked showbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //if clicked show edit , data button button showbutton = (button) (findviewbyid(r.id.editbutton)); showbutton.setvisibility(view.visible); button showbutton1 = (button) (findviewbyid(r.id.databutton)); showbutton1.setvisibility(view.visible); } }); } public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getsupportactionbar().hide(); return true; } }; ;
private void confirmdialog(context context){ final alertdialog alert = new alertdialog.builder( new contextthemewrapper(context,android.r.style.theme_dialog)) .create(); alert.settitle("alert"); alert.setmessage("do want exit ?"); alert.seticon(r.drawable.warning_icon); alert.setcancelable(false); alert.setcanceledontouchoutside(false); alert.setbutton(dialoginterface.button_positive, "yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { alert.dismiss(); finish(); } }); alert.setbutton(dialoginterface.button_negative, "no", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { alert.dismiss(); } }); alert.show(); }
call method in
@override public void onbackpressed() { super.onbackpressed(); confirmdialog(getapplicationcontext()); }
of activity
Comments
Post a Comment