Question

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/gray"
        android:orientation="vertical" > 

        <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/darkgray"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/attenders"
            android:layout_width="110dp"
            android:layout_height="40dp"
            android:layout_gravity="center"
        android:background="@color/gray"
        android:layout_marginRight="8dp"
        android:text="Attenders" />

    <Button
        android:id="@+id/send"
        android:layout_width="110dp"
        android:layout_height="40dp"
        android:layout_marginLeft="8dp"
        android:layout_gravity="center"
        android:background="@color/gray"
        android:text="Send IM" />
    </LinearLayout>
</LinearLayout>

this is my code but the dp is not working fine for all screen resolutions. suggestions plz, plz tell me if i am doing anything wrong

Was it helpful?

Solution

problem is that when i use dp for setting height or width of a button it does not gets fits to all resolutions i-e on small screens it looks big and on big screens it looks small, whereas i know that when we use dp for setting height and width of any component it automatically converts/adjusts according to screen resolution

What I understand from this is that you thought using dp instead of px (or in, or cm) will magically work such that they will all have the same physical size on all devices, regardless of that device's density (ppi).

That's not the case.

dp, or dip, as explained here, is

An abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px.

A screen with more dpi (denser, meaning more pixels are packed into a square area of the screen), will essentially draw a physically smaller image compared to a screen that has 160dpi when tasked to draw the same, say, 100x100 dp image.

When running on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor appropriate for the screen's dpi.

Solution

There are two easy ways to have your app look proportionally the same on different screen sizes.

The first is to use different layout folders (layout-ldpi, layout-mdpi, etc.). This technique is well-explained here. A much more recommended way would be to use different style values for each density, so you can still maintain one layout folder and refer to the styles instead for measurement. This can be done using the same technique, but instead you will have values-ldpi, values-mdpi, etc. This is useful for having standard sized UI elements across screen sizes.

The other way is to use weights all over your layout. Weights adjust automatically regardless of screen size of density. This will help a lot if you want, say, three columns that have varying width -- you can easily use weights to tell the layout that column A needs to occupy 40% of the available width of the screen, and B and C will have 30% each. This is useful for having a standard layout across screen sizes.

A clean-looking, nicely coded app will implement both.

OTHER TIPS

It is beacause you are giving fixed dimensions which can only be fit for a particular screen size which you are using. So try avoiding static dimensions and make use of match_parent,wrap_content and fill_parent so that you can get your layout fit for every screen size.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top