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?
For me I had to make sure that the dialog was created before accessing using findViewById on the dialog. Inside the inner class that I made the dialog in, in the OnStart callback function is where I accessed the buttons. I tried doing it outside the class when I said .show() but it was giving me a null pointer exception.
For me I the only way I could access buttons iD's in the dialog was through the dialog. I couldn't just do getActivity().findViewById(R.id.blah)
Here's the code I used:
Your code looks OK, except that for the
onClickListeners
of your buttons, I don't see why you still want to callCheckYes()
when you can just do whatever you wanted to do in theonClick
method.This is what the code should look like:
I hope this sheds some light. Please clarify exactly what you mean by "doing something with the data when YES button is clicked" - also when you say you can't choose yes or no to click, what do you mean?