How to dismiss AlertDialog in android
Actually there is no any cancel()
or dismiss()
method from AlertDialog.Builder Class.
So Instead of AlertDialog.Builder optionDialog
use AlertDialog
instance.
Like,
AlertDialog optionDialog = new AlertDialog.Builder(this).create();
Now, Just call optionDialog.dismiss();
background.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SetBackground();
// here I want to dismiss it after SetBackground() method
optionDialog.dismiss();
}
});
How can dismiss this alert dialog?
I don't know where you have written your code to launch the dialog. I have replicated your code. And this works as expected.
public class MainActivity extends AppCompatActivity {
private AlertDialog b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// custom dialog
AlertDialog.Builder dialogBuilder = new
AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
dialogBuilder.setView(dialogView);
Button reject = (Button) dialogView.findViewById(R.id.reject_btn);
Button accept = (Button) dialogView.findViewById(R.id.accept_btn);
b = dialogBuilder.create();
b.show();
accept.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
b.dismiss();
}
});
reject.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
b.dismiss();
}
});
}
}
And below is the XML. Again a very basic one to match your code
custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:id="@+id/reject_btn"
android:text="Reject"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:id="@+id/accept_btn"
android:text="Accept"
android:layout_height="wrap_content" />
</LinearLayout>
Dismiss AlertDialog.Builder
Your function should look like that:
public AlertDialog createSigninDialog (){
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_signin, null);
builder.setView(view);
final AlertDialog dialog = builder.create();
signin = (Button)view.findViewById(R.id.singing);
user = (EditText)view.findViewById(R.id.user_input);
password = (EditText)view.findViewById(R.id.password_input);
signin.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if (user.getText().toString().equals("1234567890") && password.getText().toString().equals("1234")){
Toast.makeText(MainActivity.this, "Login", Toast.LENGTH_SHORT).show();
//Dismiss here
dialog.dismiss();
} else {
Toast.makeText(MainActivity.this, "Datos incorrectos", Toast.LENGTH_SHORT).show();
//Dismiss here
dialog.dismiss();
}
}
}
);
return dialog;
}
And then you can invoke it like that:
createSigninDialog().show();
Dismiss alertDialog from other function
Save AlertDialog in a private variable. So you can use it later and call dialog.dismiss().
private AlertDialog dialog;
And in your function onReceivedError:
dialog = new AlertDialog.Builder.....
Dismiss AlertDialog from button in custom view
You need to set onClick
listener on your custom button.
Try this :
AlertDialog.Builder builder = new AlertDialog.Builder(contextRef);
builder.setView(dialogContentView);
Button btnOk= (Button) dialogContentView.findViewById(R.id.btn_ok);
builder.setNegativeButton(contextRef.getString(R.string.std_cancel), null);
AlertDialog dialog = builder.create();
dialog.show();
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
That's it !!
How to dismiss AlertDialog.Builder with custom button
Not exactly the answer to the question but i fix the problem using setPositiveButton and custom with SetTextColor and setBackgroundColor.
This is my new code:
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.custom_alert_dialog_horarios, null);
final TextView tv_texto = (TextView) dialoglayout.findViewById(R.id.custom_alert_dialog_horarios_texto);
final TextView tv_titulo = (TextView) dialoglayout.findViewById(R.id.custom_alert_dialog_horarios_titulo);
//Preparamos las fuentes personalizadas
Typeface fontalertaTitulo = Typeface.createFromAsset(getActivity().getAssets(),"fonts/OpenSans-Semibold.ttf");
Typeface fontalertaMensaje = Typeface.createFromAsset(getActivity().getAssets(),"fonts/OpenSans-Light.ttf");
tv_titulo.setTypeface(fontalertaTitulo);
tv_titulo.setText(getResources().getString(R.string.dias_de_cierre_alert_titulo));
tv_texto.setTypeface(fontalertaMensaje);
tv_texto.setText(getResources().getString(R.string.dias_de_cierre_texto));
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setPositiveButton(getResources().getString(R.string.aceptar), null);
builder.setView(dialoglayout);
//builder.show();
AlertDialog dialog = builder.create();
dialog.show();
// Customize the button
Button button = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
button.setTextColor(getResources().getColor(color.donostiakirola_texto_general));
button.setBackgroundColor(getResources().getColor(color.donostiakirola_fondo_pantalla));
//Preparamos las fuentes personalizadas
Typeface fontTextoBoton = Typeface.createFromAsset(getActivity().getAssets(),"fonts/OpenSans-Semibold.ttf");
button.setTypeface(fontTextoBoton);
How to dismiss AlertDialog.Builder?
What didn't work about dismiss()?
You should be able to use either Dialog.dismiss(), or Dialog.cancel()
alertDialog.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { // define the 'Cancel' button
public void onClick(DialogInterface dialog, int which) {
//Either of the following two lines should work.
dialog.cancel();
//dialog.dismiss();
}
});
Dismiss AlertDialog.Builder from OnClick
AlertDialog.Builder
is best suited for small simple dialog boxes rather than custom dialogs.
The cleanest way to handle custom dialogs is to subclass AlertDialog
as a private static class in your context (in this case your activity).
Here is a simplified example:
public class AlertDialogTestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AlertDialog alert = new myCustomAlertDialog(this);
alert.show();
}
private static class myCustomAlertDialog extends AlertDialog {
protected myCustomAlertDialog(Context context) {
super(context);
setTitle("Profile");
Button connect = new Button(getContext());
setView(connect);
connect.setText("Don't push me");
connect.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// I want the dialog to close at this point
dismiss();
}
});
}
}
}
Related Topics
How to Compile a Static Library Using the Android Ndk
How to Get Add to Home Screen Pop Up on Site Open in Mobile Browser
Rate Google Play Application Directly in App
How to Use Scale Gesture Detector for Pinch Zoom in Android
Menu Items Are Not Showing on Action Bar
Android Broadcastreceiver Within Activity
Ontouchevent Not Working on Child Views
Parcelable Encountered Ioexception Writing Serializable Object Getactivity()
Java.Lang.Unsatisfiedlinkerror: Dalvik.System.Pathclassloader
Android Studio Doesn't Recognize My Device
Recycler View Showing Single Item
How to Put the Text on the Left of a Radio Button in Android
How to Setcontentview in a Fragment