Will using dp take care of physical screen size?

144 views Asked by At

I read quite a few of the questions posted here but I didnt get a clear answer to my question. Will using the unit dp take care of physical screen size? I have used the unit dp for all my Views and I have tried to make the sizes of them relative.But even then i had to give absolute sizes to some of them. Will the app run the same in a 4 inch as well as a 5 inch screen.Here is the structure : enter image description here

Here is My XML:

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

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="400dp" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_above="@+id/relativeLayout2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >

        <ImageButton
            android:id="@+id/imageButton4"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/left" />

        <ImageButton
            android:id="@+id/imageButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/ic_launcher" />

        <ImageButton
            android:id="@+id/imageButton6"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/right" />
    </RelativeLayout>

         <RelativeLayout
        android:layout_width="fill_parent"
        android:id="@+id/rel2"
        android:layout_height="350dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/relativeLayout1" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="30dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:src="@drawable/lc" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="wrap_content"
            android:layout_height="280dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/linearLayout1"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_marginLeft="120dp"
                android:layout_marginTop="140dp"
                android:src="@drawable/untitled" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="30dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/linearLayout2"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="64dp"
                android:src="@drawable/lc" />

        </LinearLayout>

  </RelativeLayout>

</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
     >

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/left" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignBottom="@+id/imageButton1"
        android:layout_alignParentRight="true"
        android:src="@drawable/right" />

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />
</RelativeLayout>

</LinearLayout>

Thanks in advance....

4

There are 4 answers

4
Gaurav On

No using dp we cant take care of diffrent screen size.

for that you need to use difftent strategy You can use

android:layout_weight=".5"

this will cover half of screen if you set android:layout_width="0dp" and two views are ina row

if you set height= 0dp it will cover half of the total height of screen

Note

complete weight = 1 ; you can divide according ly like 0.5 0.3 etc

0
Darshak On

You can use values/dimens.xml

Like following (for example: 3dp for all device)

Create values/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="three">3dp</dimen>
</resources>

Create values-sw360dp/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="three">3.375dp</dimen>
</resources>

Create values-sw480dp/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="three">4.5dp</dimen>
</resources>

Create values-sw600dp/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="three">5.625dp</dimen>
</resources>

Create values-sw720dp/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="three">6.75dp</dimen>
</resources>

Ratio of dp

for default values folder its 1

for 360dp - 1.125

for 480dp - 1.5

for 600dp - 1.875

for 720dp - 2.25

How to use? example: @dimen/three

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="wrap_content"
    android:layout_height="@dimen/three"
    android:layout_above="@+id/relativeLayout2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" >
0
Karol Żygłowicz On

We don't know how your drawable sizes are, so its hard to answear your question. But as mentioned here

dp is not about screen size its about it's density. You could have 2x 5 inch screen. One with hdpi and second with xxhdpi and your layout will rescale to pixels available.

ldpi is 0.75x dimensions of mdpi

hdpi is 1.5x dimensions of mdpi

xhdpi is 2x dimensinons of mdpi

0
Mik S On

You might need to employ a different tactic. First of all you can use the dimens files to define layout for different screen densities or use layout_weight.

I recommend that you take a look at the Android developers guide for designing layout for different screen sizes: https://developer.android.com/training/multiscreen/index.html