Search code examples
androidandroid-layoutdpimobile-devices

Android layout not consistent across devices instead of using dp


Screenshot of first image Screenshot of other phoneI have a LinearLayout ,RelativeLayout,LinearLayout and a ImageView in it.This is basically a landing page of my app,inspite of using the dp my output is not same on the all device. There is the screenshot of two different phones. Thanks in advance

<LinearLayout
        android:id="@+id/lb_LinearLayout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RelativeLayout
        android:id="@+id/lb_Relative_HomeScreen"
        android:layout_width="match_parent"
        android:layout_height="450dp">
            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="350dp">
           ``   <ImageView
                android:id="@+id/lb_Background_Image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY" />
            </LinearLayout>
            <TextView
                android:id="@+id/lb_Welcome"
                android:text="@string/welcome"
                android:gravity="start"
                style="@style/Heading2.yellow"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:layout_gravity="start|center_vertical"
                android:layout_above="@+id/lb_Descrption"
                android:layout_marginLeft="20dp"
                />
...
</RelativeLayout>
</LinearLayout>

Solution

  • You need to create different layout files for the major screen density buckets and screen sizes. This way you will be able to fine tune your layout to look consistent across multiple displays.

    You can easily create a new layout for a specific bucket from the preview window:

    • Click on this icon enter image description here in the left corner.
    • Then select Create Other....
    • A popup window will appear then select from the Available Qualifiers the size item for the different screen sizes of Density for the different density buckets.
    • After you selected the desired item press the >> key, and you can select the appropriate bucket for witch you wish to create a new layout file.
    • After this you press ok and the android studio creates for you a copy of the current layout and places it in the appropriate folder for you, all that you need to do is to edit it so that it look the way you want it to look on the specific layout.