Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android custom Dialog with Button onClick event

Tags:

android

dialog

i have CustomDialog.java like this :

public class CustomDialog {
Dialog dl;
public void ShowDialog(Context context, String message) {
    dl = new Dialog(context);
    dl.setContentView(R.layout.custom_dialog);

    TextView tv_message = (TextView) dl.findViewById(R.id.textViewMessage);

    tv_message.setText(message);

    Button bt_yes = (Button)dl.findViewById(R.id.buttonYes);
    Button bt_no = (Button)dl.findViewById(R.id.buttonNo);

    bt_yes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckYes();
        }
    });
    bt_no.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dl.dismiss();
        }
    });
    dl.show();

}

public Boolean CheckYesNo(Boolean check){
    return check;
}

public Boolean CheckYes() {
    return true;
}

public void CloseDialog() {
    dl.dismiss();
}

}

this is code in Activity to use CustomDialog :

CustomDialog cdl = new CustomDialog ();
                        cdl.ShowDialog(Activity1.this, "test");
                        if (cdl.CheckYesNo(true)) {
                            // doing something with data and go to Activity 2
                        }
                        else {
                            cdl.CloseDialog();
                        }

I want like this :
1. In Activity1, when click ImageButton, CustomDialog will show.
2. after CustomDialog show, if click Button yes, it doing something with data and go to Activity2.
3. if click Button no, CustomDialog will close and don't doing something with data.

but my problem is :
when click ImageButton, CustomDialog show, the code I want to do with data will doing and auto go to Activity2.
I can't choose yes or no to click.
I think problem in Button yes onClick event.
how to fix it?

like image 787
1234abcd Avatar asked Jul 20 '16 08:07

1234abcd


Video Answer


1 Answers

parameter initilize

  Dialog dialog;

You use this where You want to use

dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.custom_dialog);
    dialog.show();


TextView tv_message = (TextView) dialog .findViewById(R.id.textViewMessage);

tv_message.setText(message);

Button bt_yes = (Button)dialog.findViewById(R.id.buttonYes);
   Button bt_no = (Button)dialog.findViewById(R.id.buttonNo);

bt_yes.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        CheckYes();
    }
});
bt_no.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        dialog.dismiss();
    }
});
like image 174
Arjun saini Avatar answered Oct 15 '22 12:10

Arjun saini