StevenR
StevenR

Reputation: 41

Android, images and dpi

I am using WVGA800 skin in Android emulator, so the density (hw.lcd.density) is 240. I have to put 4 image buttons (72 pixels per inch) at the bottom of activities view. As the resolution is 480X800, I asummed that the width of image must be 120 pixels. The problem is when application is launched, the 3 buttons take all width and there is no place for 4th button... Then I created button image in Fireworks with resolution 240 pixels per inch and in 120px width, but problem remains. Could somebody explain how to make correct drawables (sizes and dpi) so that they can be displayed pixel in pixel on Android?

Upvotes: 2

Views: 10015

Answers (3)

SnakE
SnakE

Reputation: 2581

If you put your images in res/drawable, Android assumes they're for 160dpi and scales them accordingly for different resolutions.

Your options are either to put the button images into res/drawable-hdpi signaling that they're intended for densities around 240dpi, or into res/drawable-nodpi to make them never scale regardless of the current screen density.

You still reference them as @drawable/whatever in layouts, the engine picks the best match automatically.

Upvotes: 5

Kevin Coppock
Kevin Coppock

Reputation: 134714

Disregard the image DPI entirely. It's irrelevant, all you need to know is the pixel size. What I would actually suggest is to create a NinePatch image to use as your button background. You'll actually need a few to put into a StateListDrawable, as this is how the different focus states are defined (e.g. Pressed, Focused). Once you have your NinePatch, just create a LinearLayout like so:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/my_nine_patch"
        android:text="button 1"
        android:layout_weight="1"
     />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/my_nine_patch"
        android:text="button 2"
        android:layout_weight="1"
     />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/my_nine_patch"
        android:text="button 3"
        android:layout_weight="1"
     />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/my_nine_patch"
        android:text="button 4"
        android:layout_weight="1"
     />
</LinearLayout>

Set all the buttons to fill_parent and give them all an equal weight. They'll stretch to fit evenly to the parent, and you don't have to worry at all about specifying a constant pixel or dip size. Also, it'll evenly split onto any Android device, no matter the resolution.

Upvotes: 1

Shomari
Shomari

Reputation: 414

The width is actually 320 pixels, so for 4 buttons across the entire screen set each to 80dip. Android will then adjust your density independent pixels to the proper screen size of 480 pixels.

Upvotes: 0

Related Questions