Question

hello i had problem in layout my layout is fit for all screen when i set it in linear Layout but the problem in linear Layout is that when i bring View to front it disturb other views now i change the required layout to relative Layout in which i also bring view to front which work perfect but now the problem is in relative layout is hard coded values for different screen resolutions so what can i do for this any one help me please and Thanks in advance.........

here is my layout code ....

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:weightSum="1" >

               <LinearLayout 
             android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                 android:layout_weight=".8"
                android:background="@drawable/prdttrans">
                </LinearLayout>


               <RelativeLayout 
                    android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                 android:layout_weight=".2"

                  >
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/animimage" 
                android:background="@drawable/ab"/>

            <ImageView
                android:id="@+id/waterleftDisplayimg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_marginBottom="17dp"
                android:layout_marginRight="39dp"
                android:background="@drawable/ic_launcher" />

            <ImageView
                android:id="@+id/waterrightdisplayimg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignTop="@+id/waterleftDisplayimg"
                android:layout_marginLeft="32dp"<-------------- these hardcoded values  
   cause problems 
                android:background="@drawable/ic_launcher" />

   </RelativeLayout>
            </LinearLayout>

i want to set relative Layout on all screen

Was it helpful?

Solution

If you want to hardcode for multiple screens, you can use dimens.

Under values folder in your resources, create (if not exists) an xml named dimens.xml which will look like below:

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

Create dimens files for different screen resolutions, and set your desired dp values according to these screens. Example screens:

res/values/dimens.xml as default
res/values-sw360dp/dimens.xml
res/values-sw480dp/dimens.xml
res/values-sw600dp/dimens.xml
res/values-sw720dp/dimens.xml
res/values-xlarge/dimens.xml

After creating and settings those files, in your layout xml file, change ImageView margin like this:

android:layout_marginLeft="@dimen/left_margin_value"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top