-3

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 ?

Community
  • 1
  • 1
Chief Madog
  • 1,738
  • 4
  • 28
  • 55

4 Answers4

1

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

Dipali Shah
  • 3,742
  • 32
  • 47
0

Add the following attribute in your LinearLayout

 android:weightSum="2"

After adding this your code will work. WeightSum attribute defines the total number of equal divisions you want it to be divided.If you want it to be divided into 3 parts, change its value as 3.

0
<?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:orientation="horizontal">

       <RelativeLayout
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:background="@color/red">
       </RelativeLayout>

       <RelativeLayout
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:background="@color/white">
       </RelativeLayout>
</LinearLayout>

**Preview Image Link**

  [1]: https://i.stack.imgur.com/6JKTP.png
Anu
  • 80
  • 1
  • 2
  • 13
-1

If you want to divide screen vertically than you need to use android:orientation="vertical" and in child layout use android:layout_width="match_parent"

check below xml,

 <?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:baselineAligned="false"
    android:orientation="vertical"
    android:weightSum="2"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" >

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#00b0f0" >
    </RelativeLayout >
</LinearLayout >
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142