AlertDialog with EditText and Three buttons - java

So i have tis code and i'm trying to create a AlertDialog with an EditTet and Three buttons the positive one, the négative one and the neutral one , but it doesn't work and the application stops
b5.setOnClickListener(new View.OnClickListener() {
#SuppressLint("UseCompatLoadingForDrawables")
#Override
public void onClick(View view) {
AlertDialog.Builder boite;
boite = new AlertDialog.Builder(MainActivity.this);
boite.setTitle("boite de dialogue");
boite.setIcon(getDrawable(R.drawable.warning_shield_96px));
final EditText input = new EditText(MainActivity.this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
boite.setView(input);
boite.setPositiveButton("OUI", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//whatever action
}
});
boite.show();
boite.setNegativeButton("NON", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//whatever action
}
});
boite.show();
boite.setNeutralButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//whatever action
}
});
boite.show();
}
});

There is no need to call boite.show() several times, just call it once like below :
b5.setOnClickListener(new View.OnClickListener() {
#SuppressLint("UseCompatLoadingForDrawables")
#Override
public void onClick(View view) {
AlertDialog.Builder boite;
boite = new AlertDialog.Builder(MainActivity.this);
boite.setTitle("boite de dialogue");
boite.setIcon(getDrawable(R.drawable.warning_shield_96px));
final EditText input = new EditText(MainActivity.this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
boite.setView(input);
boite.setPositiveButton("OUI", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//whatever action
}
});
boite.setNegativeButton("NON", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//whatever action
}
});
boite.setNeutralButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//whatever action
}
});
boite.show();
}
});
AlertDialog uses Builder Pattern to initialize, so you can set different methods and buttons and anything you like, then when you call alertDialog.show() it builds the object with any configs you set before that call.

Related

How to know the pressed button while opening AlertDialog?

i have this:
public void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(updatedActivity.this);
builder.setTitle("Choose the currency you would like to exchange.");
//list of items
String[] items = getResources().getStringArray(R.array.countries_array);
builder.setSingleChoiceItems(items, 0,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
String positiveText = getString(android.R.string.ok);
builder.setPositiveButton(positiveText,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//So here i wanna a code like : If pressbutton = buttonto, then buttonto.setText(selecteditem..)
}
});
String negativeText = getString(android.R.string.cancel);
builder.setNegativeButton(negativeText,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// negative button logic
}
});
AlertDialog dialog = builder.create();
// display dialog
dialog.show();
}
In the on click event, i wanna know which the button was pressed, so for example if buttonto.ispressed then buttonto.settext(the selected item)
So how to do it
Thanks

AlertDialog setOnShowListener never called

I am using a slightly different approach in order to keep the dialog open when a button is pressed:
AlertDialog.Builder builder = new AlertDialog.Builder(NewTableActivity.this);
builder.setTitle(R.string.addComponent);
final EditText titleText = new EditText(NewTableActivity.this);
titleText.setHint(R.string.title);
builder.setView(titleText);
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialogInterface, int i){
dialogInterface.cancel();
}
});
builder.setPositiveButton(R.string.ok, null);
final AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener(){
#Override
public void onShow(DialogInterface dialogInterface){
Log.i("TEst", "Doung");
Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
}
}
The dialog is opened a little lower (this works too), but the Log is never called
AlertDialog.Builder builder = new AlertDialog.Builder(NewTableActivity.this);
builder.setTitle(R.string.addComponent);
final EditText titleText = new EditText(NewTableActivity.this);
titleText.setHint(R.string.title);
builder.setView(titleText);
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
builder.setPositiveButton(R.string.ok, null);
final AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialogInterface) {
Log.e("TEst", "Doung");
Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
}
});
alertDialog.show();

Issues in onBackpressed()

Actually my issue is when i press the back button it shows a dialog and also at the same time the application finished without any operation on key? Can any one solve this?
#Override
public void onBackPressed() {
super.onBackPressed();
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setMessage("Really want to exit");
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Main.super.onBackPressed();
}
});
ab.create();
ab.show();
}
remove super.onBackPressed(); from method, keep it in onClick() only
#Override
public void onBackPressed() {
super.onBackPressed(); //remove it
}
seems you are performing same operation in both buttons, you should remove Main.super.onBackPressed() from negativeButton
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
super.onBackPressed();
}
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
ab.dismiss();
}
});
Don't call super method of onBackPressed.
#Override
public void onBackPressed() {
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setMessage("Really want to exit");
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
ab.create();
ab.show();
}
try this
#Override
public void onBackPressed() {
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setMessage("Really want to exit");
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Main.super.onBackPressed();
}
});
ab.create();
ab.show();
}
You need to Remove, super.onBackPressed(); from onBackPressed(). then your function should as,
#Override
public void onBackPressed() {
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setMessage("Really want to exit");
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Main.super.onBackPressed();
}
});
ab.create();
ab.show();
}

Error when removing item in listView

I got error at this line:
alert.setPositiveButton("YES", new DialogInterface.OnClickListener()
Logcat gives this error
java.lang.NullPointerException
Anyone can help me?
//method to remove list item
private void removeItemFromList(int position) {
final int deletePosition = position;
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setTitle("Delete");
alert.setMessage("Do you want delete this item?");
alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TOD O Auto-generated method stub
// main code on after clicking yes
arr.remove(deletePosition);
adapter.notifyDataSetChanged();
adapter.notifyDataSetInvalidated();
}
});
alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
}
Call removeAt(deletePosition) onClick of Yes
public void removeAt(int deletePosition) {
arr.remove(deletePosition);
notifyItemRemoved(deletePosition);
notifyItemRangeChanged(deletePosition, arr.size());
}

How do I reduce alert-dialogue code from main-activity in android?

I have a class where I am writing three different alert dialogue. These three alert dialogue extends the line of code of this class drastically. So for re-factoring purpose, I want to make base class for three alert dialogue and use this class in the main activity. Can anyone suggest me how to achieve this? My three alert dialogue are given below:
public boolean onJsConfirm(WebView view, String url, String message,
final android.webkit.JsResult result) {
new AlertDialog.Builder(currentActivity)
.setTitle("Confirmation")
.setMessage(message)
.setPositiveButton(android.R.string.yes,
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
result.confirm();
}
})
.setNegativeButton(android.R.string.no,
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
result.cancel();
}
}).setCancelable(false).create().show();
return true;
}
public boolean onJsAlert(WebView view, String url, String message,
final android.webkit.JsResult result) {
new AlertDialog.Builder(currentActivity)
.setTitle("Alert !")
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
result.confirm();
}
}).setCancelable(false).create().show();
return true;
}
public void onGeolocationPermissionsShowPrompt(final String origin,
final GeolocationPermissions.Callback callback) {
final boolean remember = true;
AlertDialog.Builder builder = new AlertDialog.Builder(
WebviewActivity.this);
builder.setTitle("Locations");
builder.setMessage(" Would like to use your Current Location")
.setCancelable(true)
.setPositiveButton("Allow",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int id) {
callback.invoke(origin, true, remember);
SharedPreferences pref = currentActivity
.getPreferences(currentActivity.MODE_PRIVATE);
SharedPreferences.Editor editor = pref
.edit();
editor.putBoolean("isLocationAvailable",
true);
editor.commit();
webview.loadUrl(getUrl(gps.getLatitude(),
gps.getLongitude(), "true"));
}
})
.setNegativeButton("Don't Allow",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int id) {
callback.invoke(origin, false, remember);
webview.loadUrl(getUrl("", "", "false"));
}
});
AlertDialog alert = builder.create();
alert.show();
Complete solution Try this is a Generic Alert dialog with cutom tilte,message,yes,no button caption
1) Createa Interface
import android.content.DialogInterface;
public interface AlertMagnatic {
public abstract void PositiveMethod(DialogInterface dialog, int id);
public abstract void NegativeMethod(DialogInterface dialog, int id);
}
2) Generalize method for confirm dialog.
public static void getConfirmDialog(Context mContext,String title, String msg, String positiveBtnCaption, String negativeBtnCaption, boolean isCancelable, final AlertMagnatic target) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
int imageResource = android.R.drawable.ic_dialog_alert;
Drawable image = mContext.getResources().getDrawable(imageResource);
builder.setTitle(title).setMessage(msg).setIcon(image).setCancelable(false).setPositiveButton(positiveBtnCaption, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
target.PositiveMethod(dialog, id);
}
}).setNegativeButton(negativeBtnCaption, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
target.NegativeMethod(dialog, id);
}
});
AlertDialog alert = builder.create();
alert.setCancelable(isCancelable);
alert.show();
if (isCancelable) {
alert.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface arg0) {
target.NegativeMethod(null, 0);
}
});
}
}
3) How to use
getConfirmDialog(getString(R.string.logout), getString(R.string.logout_message), getString(R.string.yes), getString(R.string.no), false,
new AlertMagnatic() {
#Override
public void PositiveMethod(final DialogInterface dialog, final int id) {}
#Override
public void NegativeMethod(DialogInterface dialog, int id) {
}
});

Categories

Resources