Welcome Android Developpers

Welcome Android Developpers
My name is Mathias Séguy, I am an Android Expert and Trainer and created the Android2EE company.

For full information, it’s here : Android2EE's Training
You can meet me on Google+, follow me on Twitter or on LinkedIn

Monday, November 14, 2011

Define your picture in dp not in px

Bandeau Conference

When defining an image (here, for example, use an image 73 pixels wide and 29 pixels high), you tend to write:

<ImageButton android:id="@+id/btnFacebookContact" 
android:layout_width="73px" android:layout_height="29px

And with such a configuration when you change the screen density (ldpi, mdpi, hdpi) pictures become either too large either too small ... arg.
What you should do is to define them using dp (density-independent pixels) instead of px (pixels) because then the system itself resizes your picture to make it to the correct size regardless of the density the screen. Layout of your files become independent of the density of the device. This does not prevent you to set your images to the four devices known densities.
It is therefore necessary:

<ImageButton android:id="@+id/btnFacebookContact" 
android:layout_width="73dp" android:layout_height="29dp

The source of this post: http://developer.android.com/guide/practices/screens_support.html:

"Density-independent pixel (dp)
    A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way.

    The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities. "

So, Thanks who?
Thanks, Android2ee, the Android Programming Ebooks :o)

Mathias Séguy
Auteur Android2EE
Ebooks to learn Android Programming.

No comments:

Post a Comment