0

I am writing an android app, I have an activity, inside it i have a button, and it's on click listener opens a dialog box from a custom XML using the following code:

I would like to add that dialog box another button that is not set in it's XML file.

All the components are excising in the XML and working just fine, the dialog opens but I can't add the b button.

this.addCheer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Dialog d = new Dialog(map.this);
                LinearLayout layout = findViewById(R.id.dialog_layout_root);
                LayoutInflater layoutInflater = d.getLayoutInflater();
                d.requestWindowFeature(Window.FEATURE_NO_TITLE);
                d.setContentView(R.layout.cheer_dialog);// custom layout for the dialog
                WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
                lp.copyFrom(d.getWindow().getAttributes());
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.MATCH_PARENT;
                d.show();
                d.getWindow().setAttributes(lp);
                final EditText title = d.findViewById(R.id.cheerDialogText);

                ImageButton addCheerOk = (ImageButton) d.findViewById(R.id.addCheerOk);

                Button b = new Button(d.getContext());
                b.setText("yo");

                cheerDialogLayout.addView(b, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}

I tried to use this example but it does not work for me. What am i doing wrong here? Thanks!

thebeancounter
  • 4,261
  • 8
  • 61
  • 109

2 Answers2

1

Just try below code

this.addCheer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Dialog d = new Dialog(map.this);
                LayoutInflater layoutInflater = d.getLayoutInflater();
                d.requestWindowFeature(Window.FEATURE_NO_TITLE);
                d.setContentView(R.layout.cheer_dialog);// custom layout for the dialog

                WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
                lp.copyFrom(d.getWindow().getAttributes());
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.MATCH_PARENT;
                d.show();
                d.getWindow().setAttributes(lp);

                LinearLayout layout = d.findViewById(R.id.dialog_layout_root);
                final EditText title = d.findViewById(R.id.cheerDialogText);

                ImageButton addCheerOk = (ImageButton) d.findViewById(R.id.addCheerOk);

                Button b = new Button(d.getContext());
                b.setText("yo");

                layout.addView(b, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}

You were using only findViewById(R.id.dialog_layout_root); instead of d.findViewById(R.id.dialog_layout_root);

Nikunj Peerbits
  • 785
  • 4
  • 12
0

Here ll_button is the id of layout which contains the two xml buttons you have.

LinearLayout ll_button = d.findViewById(R.id.ll_button);
LinearLayout.LayoutParams layoutParams = new inearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll_button.addView(b, layoutParams);

Remove your last line.

cheerDialogLayout.addView(b, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
Deˣ
  • 4,191
  • 15
  • 24