1

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....

Community
  • 1
  • 1
Sourav Kanta
  • 2,727
  • 1
  • 18
  • 29
  • You are able to test with many different screen sizes using the emulator, why not check it out on there? – Galax Jun 11 '15 at 13:42
  • @HMiller All the sizes that above 4inch have a higher resolution than my monitor can support. Thats why I couldnt try it out. – Sourav Kanta Jun 11 '15 at 13:43

4 Answers4

4

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" >
Darshak
  • 2,298
  • 4
  • 23
  • 45
0

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

Gaurav
  • 3,615
  • 2
  • 27
  • 50
  • Yeah I thought about it too but I need to know the height occupied by one of my layout during runtime(the small black ball in the middle of the picture) – Sourav Kanta Jun 11 '15 at 13:56
  • 1
    OK, I think you want to read current possition of that black ball, If YES then on relative layout you can get X,Y of that view by using `getX() getY() ` you may also use `getLeft() ` or you want height then `getHeight()` these functions will work fine, no issue of screen size – Gaurav Jun 12 '15 at 04:50
  • You want to get current possition of ball ? – Gaurav Jun 15 '15 at 12:14
  • no actually i am am using left and top margins in linearlayout to move the ball around so i know where the ball is.I have to fit all of the view in my screen. – Sourav Kanta Jun 15 '15 at 12:23
0

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

Community
  • 1
  • 1
Karol Żygłowicz
  • 2,442
  • 2
  • 26
  • 35
0

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

Mik S
  • 1
  • 1