Question

I'm trying to design the layout for tablet. I want each control to have equal spaces between each other. How all of these can be aligned properly ? Sorry I cannot attach the image as I don't have 10 reputation :(. Please help

Here is the layout code and the parent layout is linear layout.

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="125dp"
    android:background="#517398"
    >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="125dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="30dp"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/image_agenda"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1.0"
            android:src="@drawable/agenda_view" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="125dp"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:layout_marginRight="40dp"
            android:background="#000000" />

        <ImageButton
            android:id="@+id/image_month"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:src="@drawable/month_view" />

        <TextView
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:layout_marginLeft="40dp"
            android:background="#000000" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="125dp"
        android:layout_alignParentRight="true"
        android:layout_marginRight="30dp"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/image_day"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1.0"
            android:src="@drawable/day_view" />
    </LinearLayout>
</RelativeLayout>
Was it helpful?

Solution

// try this way,hope this will help you...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="125dp">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center">

            <ImageButton
                android:id="@+id/image_agenda"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:src="@drawable/ic_launcher" />
        </LinearLayout>
        <TextView
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#000000" />
        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center">

            <ImageButton
                android:id="@+id/image_month"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:src="@drawable/ic_launcher" />
        </LinearLayout>
        <TextView
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#000000" />
        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center">

            <ImageButton
                android:id="@+id/image_day"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:src="@drawable/ic_launcher" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

OTHER TIPS

Please check the below Sample layout code i have made to test i dont know for what purpose you are using and how you are using the center element 2 textview so right now i have commented that if you want that center element all 2 textview and imagebutton with equal space distribution then same formula layout_weight you use that i have used for this code

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="125dp"
    android:layout_weight="1"
    android:gravity="center"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/image_agenda"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="125dp"
    android:layout_weight="1"
    android:gravity="center"
    android:orientation="horizontal" >

  <!--   <TextView
        android:layout_width="1dp"
        android:layout_height="fill_parent"
        android:layout_marginRight="40dp"
        android:background="#000000" /> -->

    <ImageButton
        android:id="@+id/image_month"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

   <!--  <TextView
        android:layout_width="1dp"
        android:layout_height="fill_parent"
        android:background="#000000" /> -->
</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="125dp"
    android:layout_weight="1"
    android:gravity="center"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/image_day"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
</LinearLayout>

if you have any doubt you can ask me.....

try Using Weight Attribute and Avoid Using Relative Layout. Define WeightSum to parent Layout and weight to childs. It will work for both Tablet and Phones[portrait]

If u are trying to divide spaces acc. to width then give width = 0dp to childs and if Acc to height then give height 0dp to childs when using weights.

code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="125dp"
    android:background="#517398"
    android:weightSum="9" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="125dp"
        android:layout_marginBottom="8dp"
        android:paddingLeft="30dp"
        android:layout_weight="3"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/image_agenda"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1.0"
            android:src="@drawable/agenda_view" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="125dp"
        android:layout_centerHorizontal="true"
        android:layout_weight="3"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:layout_marginRight="40dp"
            android:background="#000000" />

        <ImageButton
            android:id="@+id/image_month"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:src="@drawable/month_view" />

        <TextView
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:layout_marginLeft="40dp"
            android:background="#000000" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="125dp"
        android:layout_alignParentRight="true"
        android:layout_weight="3"
        android:orientation="horizontal"
        android:paddingRight="30dp" >

        <ImageButton
            android:id="@+id/image_day"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1.0"
            android:src="@drawable/day_view" />
    </LinearLayout>

</LinearLayout>

Note: Relative Layout do not support weight or WeightSum.

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