Closing A Custom Alert Dialog On Button Click
Solution 1:
AlertDialog.Builderdialog=newAlertDialog.Builder(AddData.this);
DialogInterfacedia=newDialogInterface();
//Create a custom layout for the dialog boxLayoutInflaterinflater= (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE);
Viewlayout= inflater.inflate(R.layout.add_rep_1_set, parent, false);
TextViewtitle= (TextView)layout.findViewById(R.id.rep_1_title);
Buttonadd_item= (Button)layout.findViewById(R.id.add_button);
title.setText(workout_items[position]);
dialog.setView(layout);
AlertDialogalertDialog= dialog.create();
add_item.setOnClickListener(newOnClickListener()
{
@OverridepublicvoidonClick(View v)
{
alertDialog.dismiss();
}
});
alertDialog.show();
Solution 2:
Try this:
AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
LayoutInflaterinflater= getLayoutInflater();
ViewdialogView= inflater.inflate(R.layout.brush_opts_dialog,null);
builder.setView(dialogView);
closeBtn = (Button)dialogView.findViewById(R.id.close_btn);
finalAlertDialogdialog= builder.create();
closeBtn .setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
dialog.dismiss();
}
});
dialog.show();
You should use the AlertDialog.Builder to "build" the Alert Dialog itself. Then, call the create() method on the AlertDialog.Builder object. This method returns an AlertDialog object, which allows you to call dismiss().
You should not have to create it multiple times, nor use any DialogInterface objects. This works for me. Let me know how it goes for you.
Solution 3:
do it like this,
add_item.setOnClickListener(newOnClickListener()
{
@OverridepublicvoidonClick(View v)
{
dialog.dismiss();
}
});
Solution 4:
i have modified your code plz check..
AlertDialog.Builderdialog=newAlertDialog.Builder(AddData.this);
DialogInterfacedia=newDialogInterface();
//Create a custom layout for the dialog boxLayoutInflaterinflater= (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE);
Viewlayout= inflater.inflate(R.layout.add_rep_1_set, parent, false);
TextViewtitle= (TextView)layout.findViewById(R.id.rep_1_title);
Buttonadd_item= (Button)layout.findViewById(R.id.add_button);
finalAlertDialogDial= alertDialog.create();
title.setText(workout_items[position]);
Dial.setView(layout);
AlertDialogalertDialog= dialog.create();
add_item.setOnClickListener(newOnClickListener()
{
@OverridepublicvoidonClick(View v)
{
Dial.dismiss();
}
});
Dial.show();
Just Added
final AlertDialog Dial = alertDialog.create(); and change dialog.setView(layout); to Dial.setView(layout);
now just call Dial.dismiss(); in onclick listener.. works fine for me.
Solution 5:
use this code in your class:
Dialogdialog=newDialog(context);
dialog.setContentView(R.layout.custom);
((Button) dialog.findViewById(R.id.button))
.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
dialog.dismiss();
}
});
dialog.show();
and create custom.xml, then paste this code inside it:
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=" Ok "/></RelativeLayout>
source: https://www.mkyong.com/android/android-custom-dialog-example/
and if you do not like above code, so see below link: http://thedeveloperworldisyours.com/android/prevent-alertdialog-closing-button-clicked/
Post a Comment for "Closing A Custom Alert Dialog On Button Click"