3

I have a EditText object:

    EditText textbox = new EditText (this);
    textbox.setHint (something);

I want to add this view to an AlertDialog using the builder.

    AlertDialog.Builder builder = new AlertDialog.Builder (this);
    builder.setTitle (R.string.enter_password_name)
            .setPositiveButton (R.string.save_text, new DialogInterface.OnClickListener () {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            })
            .setNegativeButton (R.string.cancel_text, new DialogInterface.OnClickListener () {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });

I know that there is a method setView(int) but I am using API 15 so I can only use setView(View), which is also a reason why I am creating the view by code.

So far so good, but I now need to set the LayoutParams of the view. What LayoutParams should I choose? I know that in a RelativeLayout you use a RelativeLayout.LayoutParams and in a LinearLayout you use a LinearLayout.LayoutParams. But what should I use in a dialog?

Sweeper
  • 213,210
  • 22
  • 193
  • 313

3 Answers3

5

According to the source code here , alertdialog's root element is a LinearLayout, so I would suggest using LinearLayout.LayoutParams

Bhargav
  • 8,118
  • 6
  • 40
  • 63
0

Even if you want to setView with view as a parameter you can always create your view in xml and inflate it using a layout inflator and then use it as a parameter.

example:

View view = getLayoutInflater().inflate(R.layout.yourlayout, null);
setView(view);
Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
0

It depends if you inflate a custom view or not.

  • If you do that, you should use the LayoutParams according the custom view container (Linear or relative).
  • If you don't use a custom view you can set Linear LayoutParams.
eljec
  • 172
  • 1
  • 9