0

I made a background for my application that includes two images. The first one has a height of 480px; the second one should be displayed at the bottom of the first one.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <bitmap
        android:src="@drawable/bg_top"
        android:tileMode="repeat" />
    </item>
    <item
        android:top="480px">
      <bitmap
            android:src="@drawable/bg_bottom"
            android:tileMode="repeat" />
    </item>
</layer-list>

But this doesn't work on my Smartphone - it interprets the px wrong (actually it looks like the top-value is about 200px)
I know that working with dp is recommended, but how do I convert this px value into dp? I can't do it programmatically, since it's saved in a xml-document.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Tobias Baumeister
  • 2,107
  • 3
  • 21
  • 36
  • Try this : http://stackoverflow.com/questions/2025282/difference-of-px-dp-dip-and-sp-in-android – reidzeibel May 29 '13 at 17:35
  • I see, but what would you recommend me for my problem? I only know the image size in px, so how gonna figure out the margin-top-value in dp? – Tobias Baumeister May 29 '13 at 17:39
  • what is your devices dpi? is it hdpi, mdpi, or what? – reidzeibel May 29 '13 at 17:41
  • does it really matter? It has to work for every screen size – Tobias Baumeister May 29 '13 at 17:42
  • what is the dpi where the image is displayed properly? because of this : `The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion.` So, we will need to know the DPI where the image displayed correctly, and then we'll be able to set the `dp` for every screen size – reidzeibel May 29 '13 at 17:48
  • When I set the margin-top to "480dp" it works on my Galaxy S3 (1280x720px, mdpi I guess), but it doesn't on my Nexus One (hdpi) – Tobias Baumeister May 29 '13 at 17:57
  • Galaxy S3 should've been xhdpi, nexus One is hdpi, logically, your setting of 480dp should be good, it will scale itself on the Nexus One. But if it is wrong, Please try 320dp for your margin-Top :) – reidzeibel May 29 '13 at 18:21
  • I don't know why, but this works for both. thanks! – Tobias Baumeister May 29 '13 at 18:48

2 Answers2

0

This is probably on the default folder (mdpi), so if you need 480px and it worked on xhdpi, then you can set it to 240dp.

thiagolr
  • 6,909
  • 6
  • 44
  • 64
0
    px
    Pixels - corresponds to actual pixels on the screen.

    in
    Inches - based on the physical size of the screen.
    1 Inch = 2.54 centimeters

    mm
    Millimeters - based on the physical size of the screen.

    pt
    Points - 1/72 of an inch based on the physical size of the screen.

    dp or dip
    Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".

    sp
    Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.
Harshal Benake
  • 2,391
  • 1
  • 23
  • 40