What is difference between Dialog.setContentView( View ) & AlertDialog.setView( View )
Asked Answered
D

3

14

I'm working on creating a custom dialog to get input from the user. I've noticed that when I use an AlertDialog I can add widgets such as an EditText to the AlertDialog using setView() method. I'm looking into customizing AlertDialog and noticed that Dialog has a method setContentView(). In the subclass of AlertDialog I could use either setContentView() or setView().

One difference I've noticed is when I use setView() on an AlertDialog, I can see Positive & Negative buttons. In my subclass of AlertDialog, using setContentView() I don't get the Positive & Negative buttons.

Can anyone explain other differences?

Thanks.

Dread answered 18/5, 2011 at 6:47 Comment(4)
You cannot use "setContentView()" with AlertDialog.Altigraph
Actually you can. Since AlertDialog is a subclass of Dialog, AlertDialog inherits all of Dialog's methods including Dialog.setContentView()Dread
I have tried it you'll get this error The method setContentView(int) is undefined for the type AlertDialog.Builder :)Altigraph
You can use it with AlertDialog but not with AlertDialog.Builder.Suffumigate
L
31

If I undesrstood it correctly, setContentView inflates the entire Dialog window with your custom layout. Title, icon, buttons... nothing remains. setView on the other hand sets the layout of the view between the buttons and the title.

See image

Lindgren answered 8/11, 2011 at 12:35 Comment(1)
I assume you meant setContentView ?Malva
Z
2

setContentView is AlertDialog' father class method, it means all the dialog windows, and setView is AlertController method, in AlertDialog windows view, there are topPanel,contentPanel,buttonPanel.setView only set customView to contentpanel.

Zendejas answered 21/8, 2017 at 8:1 Comment(0)
B
0

BTW you can also call the AlertDialogs setView method, which seems to also remove the extra "spacing" around the view.

void setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight, int viewSpacingBottom)

like this.

alertDialog.setView(myView,0,0,0,0);

Bibby answered 28/1, 2014 at 8:11 Comment(1)
i have read the same in the Android Reference, but it does not seem to work for me; it would only recognize setView(myview); Do you know how to make it use setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight, int viewSpacingBottom); ??Muniment

© 2022 - 2024 — McMap. All rights reserved.