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?

1
  • you dont need to create the layout programatically to use setView you know right?
    – tyczj
    Oct 16, 2015 at 12:59

3 Answers 3

5

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

1
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);
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.