0

I have a custom dialog class as follows where xmlView = R.layout.yourdialoglayout which has 2 buttons. How could I add listeners to these buttons?

heres my class:

public class CustomDialog extends Dialog { public CustomDialog(Context context,int theme,int xmlView) { super(context,theme); requestWindowFeature(Window.FEATURE_NO_TITLE); //Hide the title this.setContentView(xmlView); } public void killDialog() { dismiss(); } 

}

2 Answers 2

1

You can simply attach an OnClickListener just as you would for an Activity, by using View.SetOnClickListener:

public CustomDialog(Context context, int theme, int xmlView) { super(context,theme); requestWindowFeature(Window.FEATURE_NO_TITLE); // hide the title this.setContentView(xmlView); // your special button Button yourButton = findViewById(R.id.yourbutton); yourButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // your action } }); } 

You can attach an action to your other button the same way.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use findViewById to find the buttons, and set OnClickListener on them as usual

2 Comments

would I add it below the setContentView line?
Sorry for slow response. Yes, you must add it before setContentView otherwise it will return null

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.