Jon
Jon

Reputation: 8021

Android: proper way to size UI elements for different size screens - dp vs?

Basic question here - if I define a widget in the following manner:

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@android:color/blue"/>

and I look at the widget on two different devices - one of them with a large screen and one of them with a small screen - which of the following will happen?

a) The widget will appear the same "size" on both screens - i.e. if it takes up one inch of real space on the big screen, it will take up one inch of real space on the small one, even if it that means that it doesn't all fit in the visible screen area.

b) The widget will appear as one inch on the big screen, but as some fraction of an inch on the small screen. However the result will be relative - if the widget took up three quarters of the width of the large screen, it will take up exactly three quarters of the width of the small screen.

Now, assuming that your answer was a - how do I redefine the widget to make it b? Assuming your answer was b, how do I redefine the widget to make it a? Many thanks.

Upvotes: 0

Views: 80

Answers (2)

Alan S
Alan S

Reputation: 439

Each widget must define a minWidth and minHeight, indicating the minimum amount of space it should consume by default....When your widget is added, it will be stretched to occupy the minimum number of cells, horizontally and vertically, required to satisfy its minWidth and minHeight constraints.

http://developer.android.com/guide/practices/ui_guidelines/widget_design.html

Upvotes: 1

breakline
breakline

Reputation: 6073

DP is "density independent pixel" which is by definiton equals 1px on a screen with a dpi (dot per inch) of 160. Every device has a different dpi, the cheaper ones usually have less, the more expensive ones more etc.

You can calculate "real" size based on the device's specification but to answer your question, 50dp will look different on different screens, and their "real size" will be also different.

You can read more about this in the Android docs:

http://developer.android.com/guide/practices/screens_support.html

Upvotes: 2

Related Questions