57567

Why function execution is completed before I click on OK button in an Alert android?

Question:

When I want to retrieve a value from an Alert box in android, it returns before I click on OK button.

here is the function I made:

public String enterSecretCode(){ AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle("Secret Code"); alert.setMessage("Enter your 5 digit Secret Code to Encode!"); // Set an EditText view to get user input final EditText input = new EditText(this); input.setFilters(new InputFilter[] {new InputFilter.LengthFilter(5)}); input.setHint("XXXXX"); input.setInputType(InputType.TYPE_CLASS_NUMBER); alert.setView(input); alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { code = input.getText().toString(); // Do something with value! } }); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Canceled. } }); alert.show(); return code; }

and here is the function call which I am trying to excuse on a button click:

String secretCode = enterSecretCode();

Answer1:

show() is not a blocking call. You need to rewrite your code to process your results as part of the positive button onClick() method, rather than having this method return some value.

Answer2:

If you want you can use an Interface:

Create a new Class called AlertInterface

public class AlertInterface { Context context; Receiver receiver; public AlertInterface(Context context, Receiver receiver) { this.context=context; this.receiver=receiver; } public void enterSecretCode() { final AlertDialog.Builder alert = new AlertDialog.Builder(this.context); alert.setTitle("Secret Code"); alert.setMessage("Enter your 5 digit Secret Code to Encode!"); // Set an EditText view to get user input final EditText input = new EditText(this.context); input.setFilters(new InputFilter[] {new InputFilter.LengthFilter(5)}); input.setHint("XXXXX"); input.setInputType(InputType.TYPE_CLASS_NUMBER); alert.setView(input); alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String code = input.getText().toString(); receiver.onLoad(code); } }); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }); alert.show(); } public interface Receiver{ void onLoad(String code); }

}

For calling this Class add this in your Activity:

AlertInterface aux=new AlertInterface(MainActivity.this, new AlertInterface.Receiver() { public void onLoad(String code) { Toast.makeText(MainActivity.this,"Secret code entered: "+code,Toast.LENGTH_LONG).show(); } }); aux.enterSecretCode();

The result will receive in onLoad implement method in the Activity.

Recommend

  • calling adapter in model
  • notifyItemInserted() in RecyclerView and animation
  • How to apply InputFilter to EditTextPreferences?
  • How to limit a length of a text in android ListView?
  • Using ZF2 components without entire MVC process
  • Android EditText/TextView how to make each word start with uppercase and all remaining characters of
  • In TextInputLayout change the color of hint for single letter or alphabet
  • guidance with input filter for TBB Pipeline library
  • Alert Dialog was disappearing when user clicks out side
  • EditText soft numeric keyboard sometimes does not allow digits
  • Distcp Mismatch in length of source
  • how to send the response to browser fromm http.request in node.js?
  • FQL returning empty set?
  • Argument of '#selector' does not refer to an '@objc' method (swift 3)
  • Update Search Results to Lazy Adapter in android
  • Android onKey w/ virtual keyboard
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • How Lists (specifically, RecyclerView with CardViews) in Android work
  • How to handle images sent by a mobile device?
  • java inputstream
  • chrome.tabs.executeScript only fires when the Developer Console is open
  • How to render a blob on a canvas element?
  • How to use an array of arrays with array_map(…) in PHP?
  • how to adjust image in a panel in Java swing?
  • FFmpeg Conversion Error
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Possible to stop flickering java tooltip in heavyweight mode?
  • How to set my toolbar fixed while scrolling android
  • what is the difference between the asp.net mvc application and asp.net web application
  • Cannot Parse HTML Data Using Android / JSOUP
  • Matrix multiplication with MKL
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • JTable with a ScrollPane misbehaving
  • Getting Messege Twice Using IMvxMessenger
  • Authorize attributes not working in MVC 4
  • Busy indicator not showing up in wpf window [duplicate]
  • Binding checkboxes to object values in AngularJs
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF
  • How to load view controller without button in storyboard?