Nested LinearLayout shows only the first View

I have seen some question the can be similar to this. However, those scenario were different and some are not for beginners.
So I just started Android recently. Please understand that I'm new to *.xml. Anyway, my concern is regarding the nested LinearLayout that only shows the first View.

<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"
    tools:context=".MainActivity" >

    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="horizontal">

        <TextView
            android:layout_gravity="center"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:textSize="20sp"
            android:text="@string/app_title">
        </TextView>

    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="horizontal">

        <Button 
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/button_ok"
            android:onClick="changeMessage">
        </Button>

        <TextView
            android:layout_gravity="center"
            android:textSize="20sp"
            android:id="@+id/this_text"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/sample_text">
        </TextView>

    </LinearLayout>

</LinearLayout>

Here's the XML so far. With this code, only the first TextView is only showing.

Upvotes: 0

Views: 798

Answers (3)

TNR
TNR

Reputation: 5869

change the layout as below

<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"
    tools:context=".MainActivity" >
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_gravity="center"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:textSize="20sp"
        android:text="@string/app_title">
    </TextView>

</LinearLayout>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:orientation="horizontal">

    <Button 
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/button_ok"
        android:onClick="changeMessage">
    </Button>

    <TextView
        android:layout_gravity="center"
        android:textSize="20sp"
        android:id="@+id/this_text"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/sample_text">
    </TextView>

</LinearLayout>

Your inner Layouts are matching with the Parent layout so only first layout will be shown.

Upvotes: 1

Renjith
Renjith

Reputation: 5803

because you have given first LinearLayout width and height to matchparent

Change its height to wrapcontent

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="horizontal">

    <TextView
        android:layout_gravity="center"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:textSize="20sp"
        android:text="@string/app_title">
    </TextView>

</LinearLayout>

// other layouts

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157437

android:layout_height="match_parent" the height of the nested LinearLayout should be wrap_content instead of match_parent

Upvotes: 2

Related Questions