Search code examples
androidandroid-relativelayout

Some views don't show in RelativeLayout


I have the following RelativeView. I want

  • EditText at bottom
  • TextView on top - I have an issue here - it doesn't show but I don't understand why
  • 2 ListViews between them - both taking half of the screen vertically

Please do you see why that top TextView doesn't show ?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/Search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:layout_alignParentBottom="true">
        <requestFocus />
    </EditText>
    <View android:id="@+id/Placeholder"
        android:layout_height="0dp"
        android:layout_width="0dp"
        android:layout_above="@id/Search"
        android:layout_centerInParent="true"/>
    <ListView
        android:id="@+id/ResultsColumn1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignRight="@id/Placeholder"
        android:layout_above="@id/Search" />
    <ListView
        android:id="@+id/ResultsColumn2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignLeft="@id/Placeholder"
        android:layout_above="@id/Search"/>
    <TextView
        android:id="@+id/InformationTextTop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/ResultsColumn1"
        android:text="Why is this not visible ?"/>
</RelativeLayout>

Solution

  • It's simply because the ListViews have match_parent in the android:layout_height property and the TextView is defined after them in the xml. Since this is a pretty simple layout I would recommend to use the LinearLayout instead.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:id="@+id/InformationTextTop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@id/ResultsColumn1"
            android:text="Why is this not visible ?"/>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">
            <ListView
                android:id="@+id/ResultsColumn1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"/>
            <ListView
                android:id="@+id/ResultsColumn2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"/>
        </LinearLayout>
    
        <EditText
            android:id="@+id/Search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text">
            <requestFocus />
        </EditText>
    </LinearLayout>