TheN33k
TheN33k

Reputation: 145

View inside RelativeLayout not showing up in landscape

I'm trying to position two LinearLayouts on the screen: one small one at the bottom and a larger one taking up the rest of the screen space.

They are inside a RelativeLayout. I have the bottom linear layout set to anchor to the bottom of the screen, with a hardcoded height in dp. The larger LinearLayout is set to be above the bottom one.

This works as expected in portrait, but I cannot get the bottom (smaller) linearlayout to show up in landscape. Even if I set its height to a very high number. Both layouts look as expected in the Android Studio editor by the way.

Here's the XML:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#ff1b284b"
    android:orientation="vertical">


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/plusZoomMinusButtons">

       -- Removed inner views --

        </LinearLayout>

    <LinearLayout
        android:id="@+id/plusZoomMinusButtons"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="top"
        android:orientation="horizontal"

        android:layout_alignParentBottom="true">

        -- Removed inner views --

    </LinearLayout>
</RelativeLayout>

edit: The problem wasn't even with the layout, I found a piece of code elsewhere in the program that messed with the layout, but only in landscape :|

Upvotes: 0

Views: 1354

Answers (2)

Ahsan Kamal
Ahsan Kamal

Reputation: 1105

Try this XML. Maybe it will solve your problem.

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff1b284b">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/plusZoomMinusButtons">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="30dp"
            android:layout_height="match_parent"
            android:id="@+id/eegLegend"

            android:layout_marginBottom="33dp"
            android:layout_marginTop="5dp"
            android:gravity="center_vertical"
            android:layout_marginRight="2dp">

        </LinearLayout>

        <XYPlot
            android:id="@+id/aprHistoryPlot"
            title="A/P/R History"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="0dp"
            android:layout_weight="0"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/plusZoomMinusButtons"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true">

        <Button
            android:id="@+id/minus"
            android:layout_width="10dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@color/grey"
            android:paddingBottom="5dp"
            android:text="-"
            android:textColor="@android:color/white"
            android:textSize="10dp" />

        <TextView
            android:id="@+id/magnification"
            android:layout_width="30dp"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:gravity="center"
            android:text="10x"
            android:textColor="#ffffff"
            android:textSize="25dp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/plus"
            android:layout_width="10dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@color/grey"
            android:paddingBottom="5dp"
            android:text="+"
            android:textColor="@android:color/white"
            android:textSize="10dp" />
    </LinearLayout>
</RelativeLayout>

Upvotes: 0

Shree Krishna
Shree Krishna

Reputation: 8562

It is not necessary to provide android:orientation="vertical" in RelativeLayout that you have written in your code. Ok one simple solution is Just change the main RelativeLayout to LinearLayout and add the weight attribute to your LinearLayout which is a first child of main Parent.

android:layout_weight="1"

Here is how your Top XML Looks like

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff1b284b"
android:orientation="vertical">

    <LinearLayout
       android:orientation="horizontal"
       android:layout_width="match_parent"
       android:layout_weight="1"
       android:layout_height="0dp"
        >
       //Leave others as it is

OR Try giving weightSum to parent and give weight that matches parent's weightSum as follows

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff1b284b"
android:weightSum = "3"
android:orientation="vertical">

    <LinearLayout
       android:orientation="horizontal"
       android:layout_width="match_parent"
       android:layout_weight="2"
       android:layout_height="0dp"
        >
        //Body
    </LinearLayout>

    <LinearLayout
       android:id="@+id/plusZoomMinusButtons"
       android:layout_width="match_parent"
       android:layout_height="40dp"
       android:gravity="top"
       android:layout_weight="1"
       android:orientation="horizontal">

    </LinearLayout>
</LinearLayout>

Note : It is best practice to put 0dp to height if you give android:layout_weight attribute for memory management. These are **Removed because it's unnecessary **

 android:layout_height="wrap_content"
 android:layout_above="@+id/plusZoomMinusButtons"

Upvotes: 1

Related Questions