Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss AlertDialog.Builder with custom button

I have Custom AlertDialog and i want dismiss when the user click the button.

This is my code:

    Button btn = (Button) dialoglayout.findViewById(R.id.custom_alert_dialog_horarios_btn_aceptar);

    btn.setOnClickListener(new Button.OnClickListener() {

           @Override
           public void onClick(View arg0) {
            // TODO Auto-generated method stub
            //I want dismiss alertDialog

           }});


    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setView(dialoglayout);
    builder.show()
like image 582
aldakur Avatar asked Dec 06 '25 04:12

aldakur


2 Answers

You can try this :

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

builder.setView(dialoglayout);

final AlertDialog d = builder.show();

Button btn = (Button) dialoglayout.findViewById(R.id.custom_alert_dialog_horarios_btn_aceptar);

btn.setOnClickListener(new Button.OnClickListener() {

       @Override
       public void onClick(View arg0) {
          d.dismiss();
       }});
like image 75
druci Avatar answered Dec 08 '25 02:12

druci


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);
like image 40
aldakur Avatar answered Dec 08 '25 02:12

aldakur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!