Search code examples
androidandroid-layoutandroid-relativelayout

Assigning FrameLayout as half of parent RelativeLayout


I have a frame layout inside a relative layout, that everything else is positioned below it. How can I make this frame layout take up one half of the whole screen?

I tried with weight but it doesn't change anything..

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/mainLayoutStyle">

<FrameLayout 
       android:id="@+id/codeLayout"
       style="@style/codeLayoutLargeStyle">   

        <TextView 
            android:id="@+id/DisplayTextView"
            style="@style/DisplayTextViewLargeStyle"/>
</FrameLayout>
......
.....
   <LinearLayout 

Solution

  • I have a frame layout inside a relative layout, that everything else is positioned below it

    Then you are using wrong layout as your parent. You should replace RelativeLayout with LinearLayout, set FrameLayout heights to match_parent and android:layout_weight="1" then wrap everyghing that you want below anything needed (Relative, Linear whatever) ensuring it got also set height and layout_weight as above. Then you will get 50%-50% height split

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <FrameLayout
            android:layout_weight="1"
            android:background="#ff0000"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
        <RelativeLayout
            android:layout_weight="1"
            android:background="#0000ff"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    </LinearLayout>
    

    enter image description here