Getting return value from onClickListener of Android
android, android-widget, java
Solution
Android dialogs are asynchronous, therefore you need to refactor your code to deal with this. I'm guessing you were planning to do something like this:
boolean result = showConfirmation(...);
if(result) {
//do something
}
else {
//do something else
}
You can achieve the same result with something like this:
public class MainActivity extends Activity {
private boolean result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showConfirmationBox("Are you sure you want to do this", this);
}
private doOnTrueResult() {
result = true;
//do something
}
private doOnFalseResult() {
result = false;
//do something else
}
public void showConfirmationBox(String messageToShow, final Context context) {
// prepare the alert box
AlertDialog.Builder alertbox = new AlertDialog.Builder(context);
// set the message to display
alertbox.setMessage(messageToShow);
// set a positive/yes button and create a listener
alertbox.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(context,
"'Yes' button clicked", Toast.LENGTH_SHORT)
.show();
doOnTrueResult();
}
});
// set a negative/no button and create a listener
alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(context, "'No' button clicked",
Toast.LENGTH_SHORT).show();
doOnFalseResult();
}
});
// display box
alertbox.show();
}
}
Problem
Can I set variable into context like session in web development? Here is my code to in which I am developing an confirmation box as soon as the Android application get started: ``` package com.example.alertboxandloadingwidgets; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.view.Menu; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Boolean result = showConfirmationBox("Are you sure you want to do this", this); } public Boolean showConfirmationBox(String messageToShow, final Context context) { // prepare the alert box AlertDialog.Builder alertbox = new AlertDialog.Builder(context); // set the message to display alertbox.setMessage(messageToShow); // set a positive/yes button and create a listener alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() { // do something when the button is clicked public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(context, "'Yes' button clicked", Toast.LENGTH_SHORT) .show(); } }); // set a negative/no button and create a listener alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() { // do something when the button is clicked public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(context, "'No' button clicked", Toast.LENGTH_SHORT).show(); } }); // display box alertbox.show(); } } ``` But I want that if the `yes` button is clicked then it has to return `true` and if `no` button is clicked then it has to return `false`. But I am not able to do so because return type of `onClickListener` is void. Update But the problem is that I have make it generic means This method I have to write in a CommonUtilities Class From where any of the activity can use this method. So I have to set or reset the value the result parameter from where I am calling this method.