dialog - AlertDialog for getting password android -


i'm programming in java using android studio. want get password user using alertdialog , show using alertdialog. problem without prompt entering password see second dialog. in other words message "your pass null". wrong in codes?

i run dialog codes in button onclick event:

string s = messagebox.showpasswordbox(this, "", "enter password" , "ok" , "cancel"); messagebox.show(this, "showing pass", "your pass " + s , "ok" , "cancel"); 

where show , showpasswordbox codes are:

public static messageboxresult show(context context, string title, string message, string positivemessage, string negativemessage) {     result = messageboxresult.closed;      alertdialog.builder builder = new alertdialog.builder(context);      builder.settitle(title);     builder.setmessage(message);      builder.setpositivebutton(positivemessage, new dialoginterface.onclicklistener() {          public void onclick(dialoginterface dialog, int which) {             // nothing close dialog             result = messageboxresult.positive;             dialog.dismiss();          }      });      builder.setnegativebutton(negativemessage, new dialoginterface.onclicklistener() {          @override         public void onclick(dialoginterface dialog, int which) {             result = messageboxresult.negative;              dialog.dismiss();         }     });      alertdialog alert = builder.create();     alert.show();      return result; }   // return password public static string showpasswordbox(         context context, string title, string message, string positivemessage, string negativemessage) {     stringresult = null;     alertdialog.builder builder = new alertdialog.builder(context);     builder.settitle(title);     builder.setmessage(message);      // set input     final edittext input = new edittext(context);      // specify type of input expected; this, example, sets input password, , mask text     input.setinputtype(inputtype.type_class_text | inputtype.type_text_variation_password);     builder.setview(input);      // set buttons     builder.setpositivebutton(positivemessage, new dialoginterface.onclicklistener() {         @override         public void onclick(dialoginterface dialog, int which) {             stringresult = input.gettext().tostring();              dialog.dismiss();         }     });     builder.setnegativebutton(negativemessage, new dialoginterface.onclicklistener() {         @override         public void onclick(dialoginterface dialog, int which) {             dialog.cancel();             dialog.dismiss();         }     });       builder.create().show();      return stringresult; } 

problem in showpasswordbox method. returns stringresult in whe same moment when first dialog showed. , second dialog showed not initailized string object. (you can check if close second dialog see first dialog). , stringresult setted when user entered field , tap on "ok" button.

1.try call just

messagebox.showpasswordbox(this, "", "enter password", "ok", "cancel"); 

and modify part of showpasswordbox method below.

 builder.setpositivebutton(positivemessage, new dialoginterface.onclicklistener() {             @override             public void onclick(dialoginterface dialog, int which) {                 stringresult = input.gettext().tostring();                 messagebox.show(context, "showing pass", "your pass " + stringresult, "ok", "cancel");                 dialog.dismiss();             }         }); 

2.or if wanna use showpasswordbox in general way should create own listener , add in onclick method positive button.

public class messagebox {     private static positivebuttonlistener listener;     public static void setlistener(positivebuttonlistener newlistener) {         listener = newlistener;     }      public interface positivebuttonlistener {         void onpositivebuttonclick(string result);     } 

and modify onclick method

builder.setpositivebutton(positivemessage, new dialoginterface.onclicklistener() {     @override     public void onclick(dialoginterface dialog, int which) {         stringresult = input.gettext().tostring();         if (listener != null) {             listener.onpositivebuttonclick(stringresult);         }         dialog.dismiss();     } }); 

and call methods below.

messagebox.setlistener(new messagebox.positivebuttonlistener() {     @override     public void onpositivebuttonclick(string result) {         messagebox.show(mainactivity.this, "showing pass", "your pass " + result, "ok", "cancel");     } });  messagebox.showpasswordbox(mainactivity.this, "", "enter password", "ok", "cancel"); 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -