I want to make a alert dialog close when the user choose one of the available options in the box only, and doesn't close when he click the faded area around the alert dialog.
So how can i prevent the alert dialog from close in that way ?
if (totalCount == 10){
AlertDialog.Builder rateDialog = new AlertDialog.Builder(MainActivity.this);
LayoutInflater layoutInflater = getLayoutInflater();
View view = layoutInflater.inflate(R.layout.rating_deign, null);
rateDialog.setView(view);
final AlertDialog alert = rateDialog.create();
alert.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alert.show();
btn_rate = view.findViewById(R.id.btn_rate);
close = view.findViewById(R.id.close);
btn_rate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
totalCount = 12;
editor.commit();
alert.cancel();
}
});
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
totalCount = 0;
editor.commit();
alert.cancel();
}
});
}
alert.setCancelable(false) you need to add.
Sets whether the dialog is cancelable or not. Default is true.
Related
This question already has answers here:
How to display Toast in Android?
(22 answers)
Closed 5 years ago.
public void showEditPassword() {
LayoutInflater li = LayoutInflater.from(this);
View promptsView = li.inflate(R.layout.dialog_editpassword, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText txtOldPass, txtNewPass, txtConfirmPass;
txtOldPass = (EditText) promptsView.findViewById(R.id.txtOldPassword);
txtNewPass = (EditText) promptsView.findViewById(R.id.txtNewPassword);
txtConfirmPass = (EditText) promptsView.findViewById(R.id.txtConfirmPassword);
// set and show dialog edit password
alertDialogBuilder.setCancelable(false)
.setPositiveButton("SAVE",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//do the saving here
saveNewPassword(currentPassword, txtOldPass.getText().toString(),
txtNewPass.getText().toString(), txtConfirmPass.getText().toString());
//recreate();
Toast.makeText(MainActivity.this, "Save Password clicked", Toast.LENGTH_LONG);
}
})
.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
I want to call a input dialog and capture the save button action when click. I have tried putting a Toast message when save button was clicked in the dialog but nothing' happened. Thanks.
You do not call show() on your Toast. That's why it looks like nothing is happening
I have a dialog in which I'm supposed to fill in some details in an EditText. If the positive button is clicked when the edit text is empty, a Snackbar with a message is shown and the dialog gets closed. But, when I open the dialog again, the app gets crahed giving: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first error.
Here's how I have inflated the view:
LayoutInflater inflater = this.getLayoutInflater();
addVenueDialog = inflater.inflate(R.layout.add_venue_dialog, null);
and here's the java code behind opening dialog and checking if edit text is empty or not:
case R.id.nav_add_venue:
if (dialog == null) {
LayoutInflater inflater = this.getLayoutInflater();
View addVenueDialog = inflater.inflate(R.layout.add_venue_dialog, null);
vName = (EditText) addVenueDialog.findViewById(R.id.vName);
vAddress = (EditText) addVenueDialog.findViewById(R.id.vAddress);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Title");
builder.setView(addVenueDialog);
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if (isNetworkAvailable()) {
if (vName.getText().toString().isEmpty()) {
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "V name cannot be empty", Snackbar.LENGTH_SHORT);
snackbar.show();
} else if (vAddress.getText().toString().isEmpty()) {
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "V address cannot be empty", Snackbar.LENGTH_SHORT);
snackbar.show();
} else {
mDatabase.child("vs").child(user.getUid()).child("V name").setValue(vName.getText().toString());
mDatabase.child("vs").child(user.getUid()).child("V address").setValue(vAddress.getText().toString());
}
} else {
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "No internet connection", Snackbar.LENGTH_SHORT);
snackbar.show();
}
}
});
dialog = builder.create();
}
dialog.show();
break;
I don't know why the app is crashing when I'm opening the dialog once again after closing it.
Please let me know.
One simple solution would be to keep a global instance of AlertDialog and re-using it:
//global
private AlertDialog dialog;
now in the switch case:
case R.id.nav_add_venue:
if(dialog == null) {
LayoutInflater inflater = this.getLayoutInflater();
View addVenueDialog = inflater.inflate(R.layout.add_venue_dialog, null);
builder.setView(addVenueDialog);
final EditText vName = (EditText) addVenueDialog.findViewById(R.id.vName);
final EditText vAddress = (EditText) addVenueDialog.findViewById(R.id.vAddress);
// Other code //
dialog = builder.create();
}
dialog.show();
break;
Remember to dismiss the dialog onDestroy method to avoid memory leak:
public void onDestroy() {
super.onDestroy();
if(dialog != null) {
dialog.dismiss();
}
}
I am trying to build a alert box in android studio, i have 2 xml and 1 activity class where on click of a button i want to show the other layout.
I am getting a error, here is my code of MainActivity
cancelBookingButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = getLayoutInflater();
View alertLayout = inflater.inflate(R.layout.alert_cancel_confirm, null);
final TextView disAgree = (TextView) alertLayout.findViewById(R.id.TextDisagree);
final TextView Agree = (TextView) alertLayout.findViewById(R.id.TextAgree);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Current Booking");
// This will set the view from XML inside ALertDialog
alert.setView(alertLayout);
// disabling cancel of AlertDialog on click of back button and outside touch
alert.setCancelable(false);
AlertDialog dialog = alert.create();
dialog.show();
}
});
In this line i am getting error
AlertDialog.Builder alert = new AlertDialog.Builder(this);
In Builder cannot be applied
Any idea why i am getting this.
AlertDialog.Builder alert = new AlertDialog.Builder(YourActivity.this);
alert.setTitle("Current Booking");
// This will set the view from XML inside ALertDialog
alert.setView(alertLayout);
// disabling cancel of AlertDialog on click of back button and outside touch
alert.setCancelable(false);
AlertDialog dialog = alert.create();
dialog.show();
i am having an custom alert view which pop ups on a button click event.all the things are going fine.but the problem is:
if user clicks outside alert dialog it disappear.i want to restrict user for clicking out side.I am giving him the choice of cancel/cross button to close alert dialog.
so how to restrict user clicking outside the alert box?
code:
the code in onCreate for button click where i am calling show dialog:
final Button cdButton = (Button) findViewById(R.id.denonCdImage);
cdButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
showDialog(CD_CATG_ID);
}
});
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder;
Context mContext = this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.categorydialog,(ViewGroup) findViewById(R.id.layout_root));
GridView gridview = (GridView)layout.findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
/** Check the id for the device type for image tobe change */
switch(id) {
case 1 : // for the cd image
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,final int position, long id) {
Toast.makeText(view.getContext(), "Image selected for CD", 3000).show();
cdImageId = getImageId(position);
int elementId = getApplicationContext().getResources().getIdentifier(cdImageId, "drawable", getPackageName());
cdImageView.setImageResource(elementId);
Log.d("CdImageid", ""+cdImageId);
closeDialog(view);
}
});
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
dialog = builder.create();
break;
default:
dialog = null;
}
/** onclick listner for the close button */
ImageView close = (ImageView) layout.findViewById(R.id.close);
close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
dialog.dismiss();
}
});
return dialog;
}
any suggestions?
thanks!
There are two methods concerning this behaviour: setCancelable() and setCanceledOnTouchOutside() as you can see in the reference.
I've come across an issue which I've never had before. I have a Fragment containing a button. This button (ViewBookingButton) shows a popup dialog. When the dialog opens I would like to set a string in an EditText (AllBooking). Unfortunately it crashes and shows NullPointerExecption. What is going wrong? Here is my code:
ViewBookingButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
AlertDialog.Builder ViewBookingHelpBuilder = new AlertDialog.Builder(getActivity());
ViewBookingHelpBuilder.setTitle("view booking:");
LayoutInflater inflater = getActivity().getLayoutInflater();
View DialogLayout = inflater.inflate(R.layout.view_booking, null);
ViewBookingHelpBuilder.setView(DialogLayout);
TextView AllBooking = (TextView)view.findViewById(R.id.AllBooking);
AllBooking.setText("Hello World");//WHEN I REMOVE THIS THE DIALOG WORKS
ViewBookingHelpBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
}
});
AlertDialog helpDialog = ViewBookingHelpBuilder.create();
helpDialog.show();
}
});
Change this
TextView AllBooking = (TextView)view.findViewById(R.id.AllBooking);
to
TextView AllBooking = (TextView)DialogLayout.findViewById(R.id.AllBooking);
TextView belongs to the inflated layout and `findViewById looks for the view in the current view hierarchy.