Chief Madog Chief Madog - 2 months ago 14
Android Question

how to have 2 relative layouts divided by half in activity?

i want to divide my screen in half vertically, and do a different color in each half,

i was trying to use this solution mentioned in here>

Android: 2 relative layout divided in half screen

but it doesn't work for me.

this is my layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".activities.alignmentActivities.Add_New_Project_Activity"
tools:showIn="@layout/app_bar_add__new__project_">


<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#f00000"
android:layout_weight="1">
</RelativeLayout>

<RelativeLayout
android:layout_width="0dp"
android:background="#00b0f0"
android:layout_height="wrap_content"
android:layout_weight="1">
</RelativeLayout>

</LinearLayout>


how can i achieve the desired effect of 2 layouts each takes half the screen vertical \ what am i doing wrong then the example mentioned in the link ?

Answer

You may Try to use this :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="2"
    android:orientation="horizontal"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">


    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="#f00000"
        android:layout_weight="1">
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:background="#00b0f0"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </RelativeLayout>

Output :

Screenview