Eric
Eric

Reputation: 321

Closing a custom alert dialog on button click

I'm having trouble closing my alert dialog. I am using a layout inflator to make the dialog, so I'm not sure how I would go about closing the thing after I'm done with it. Code is below:

AlertDialog.Builder dialog = new AlertDialog.Builder(AddData.this);
DialogInterface dia = new DialogInterface();

//Create a custom layout for the dialog box
LayoutInflater inflater = (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.add_rep_1_set, parent, false);

TextView title = (TextView)layout.findViewById(R.id.rep_1_title);
Button add_item = (Button)layout.findViewById(R.id.add_button);

add_item.setOnClickListener(new OnClickListener()
{
        @Override
        public void onClick(View v)
        {
        //Need this to close the alert dialog box
        }
});

title.setText(workout_items[position]);
dialog.setView(layout);
dialog.show();

I cant call finish, because that closes the listview that the alert dialog is launched from, and the dialog.dismiss calls are not available to me.

What do you think I should do to fix this?

Upvotes: 32

Views: 85351

Answers (11)

Harsh Rana
Harsh Rana

Reputation: 47

There's a little change from above answers that just check that if you have written AlertDialog alertDialog = builder.create(); before builder.setView(...); method, then just write it after builder.setView(...);

Upvotes: 0

Tasnuva Tavasum oshin
Tasnuva Tavasum oshin

Reputation: 4750

     final viewProgressDialogue viewProgressDialogue = new viewProgressDialogue(DebugActivity.this);
        viewProgressDialogue.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.d("response","im calling");
                viewProgressDialogue.dismiss();
            }
        }, 5000);
> Make it one Instance so that it will work
It Will in the Fragment too.

for Fragment Use
    final viewProgressDialogue viewProgressDialogue = new viewProgressDialogue(getActivity());
        viewProgressDialogue.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.d("response", "im calling");
                viewProgressDialogue.dismiss();
            }
        }, 5000);
> viewProgressDialogue Class
public class viewProgressDialogue extends Dialog {
    public viewProgressDialogue(@NonNull Context context) {
        super(context);

        WindowManager.LayoutParams wlmp = getWindow().getAttributes();
        getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        wlmp.gravity = Gravity.CENTER_HORIZONTAL;
        getWindow().setAttributes(wlmp);
        setTitle(null);
        setCancelable(false);
        setOnCancelListener(null);

        View view = LayoutInflater.from(context).inflate(
                R.layout.custom_progress, null);
        setContentView(view);


    }
}

Upvotes: 0

StrTOInt
StrTOInt

Reputation: 16

You get error, Try this code

   dialog.setView(layout);

   final AlertDialog alertDialog = dialog.create();

   add_item.setOnClickListener(new OnClickListener(){
    @Override
     public void onClick(View v)
        {
          if (alertDialog != null && alertDialog.isShowing()) {
              alertDialog.dismiss();
        }
      }
    });
   alertDialog.show();

Upvotes: 0

Aqib shahzad
Aqib shahzad

Reputation: 747

Try this:

btn_go_anyway.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v)
    {
        getDialog().dismiss();
    }
});

Upvotes: -1

سعید .م
سعید .م

Reputation: 137

use this code in your class:

Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
((Button) dialog.findViewById(R.id.button))
   .setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        dialog.dismiss();
    }
});
dialog.show();

and create custom.xml, then paste this code inside it:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android: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/

Upvotes: 2

Hesham Fas
Hesham Fas

Reputation: 976

You need to implement DialogInterface.OnClickListener rather than View.OnClickListener. then dialog.dismiss() will be available.

new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
       dialog.dismiss();
}
}

Upvotes: 0

Nlinscott
Nlinscott

Reputation: 796

Try this:

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

    LayoutInflater inflater = getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.brush_opts_dialog,null);

    builder.setView(dialogView);


    closeBtn = (Button)dialogView.findViewById(R.id.close_btn);


   final AlertDialog dialog = builder.create();

    closeBtn .setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(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.

Upvotes: 18

Abdus Salam
Abdus Salam

Reputation: 55

i have modified your code plz check..

   AlertDialog.Builder dialog = new AlertDialog.Builder(AddData.this);
    DialogInterface dia = new DialogInterface();

    //Create a custom layout for the dialog box
    LayoutInflater inflater = (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.add_rep_1_set, parent, false);

    TextView title = (TextView)layout.findViewById(R.id.rep_1_title);
    Button add_item = (Button)layout.findViewById(R.id.add_button);



    final AlertDialog Dial = alertDialog.create();
    title.setText(workout_items[position]);
    Dial.setView(layout);

    AlertDialog alertDialog = dialog.create();

    add_item.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(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.

Upvotes: 3

Pasha
Pasha

Reputation: 2425

AlertDialog.Builder dialog = new AlertDialog.Builder(AddData.this);
DialogInterface dia = new DialogInterface();

//Create a custom layout for the dialog box
LayoutInflater inflater = (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.add_rep_1_set, parent, false);

TextView title = (TextView)layout.findViewById(R.id.rep_1_title);
Button add_item = (Button)layout.findViewById(R.id.add_button);

title.setText(workout_items[position]);
dialog.setView(layout);

AlertDialog alertDialog = dialog.create();

add_item.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        alertDialog.dismiss();
    }
});


alertDialog.show();

Upvotes: 49

Hades
Hades

Reputation: 3936

do it like this,

add_item.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                        dialog.dismiss();
                }
            });

Upvotes: 16

Ungureanu Liviu
Ungureanu Liviu

Reputation: 4124

Try dialog.cancel(); in your onclick listener. It should work.

Upvotes: 0

Related Questions