Enzio
Enzio

Reputation: 889

TextView is not visible inside Linear (vertical) layout?

This is my XML code. I have an outer vertical linear layout. Inside this there is a horizontal linear layout that contains two TextViews. Below the horizontal layout is another TextView. But this is not visible.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.praveenmuthukumarana.coursework1_quizapp.GameActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:layout_width="250dp"
                android:layout_height="80dp"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="10dp"
                android:background="@android:color/holo_green_dark"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:layout_marginTop="20dp"
                android:layout_marginRight="20dp"
                android:background="@android:color/holo_red_dark"/>

        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:background="@android:color/holo_blue_dark"/>


    </LinearLayout>

</android.support.constraint.ConstraintLayout>

This is the output

enter image description here

I'm expecting something like this.

enter image description here

How can I achieve this?

Upvotes: 0

Views: 303

Answers (4)

Harshith Shetty
Harshith Shetty

Reputation: 361

Make the height of the inner LinearLayout as "wrap content", you have set it as "match_parent"

wrap_content -> wrap its content, take only the size needed by the view or its inner views and wrap it.

match_parent -> matches to the size of its parent, take all the space of its parent container.

Here your inner LinearLayout has taken all the space of its parent, leaving none for the other view to draw

Upvotes: 1

Deep Patel
Deep Patel

Reputation: 2644

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"  // only change is here
            android:orientation="horizontal">

            <TextView
                android:layout_width="250dp"
                android:layout_height="80dp"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="10dp"
                android:background="@android:color/holo_green_dark"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:layout_marginTop="20dp"
                android:layout_marginRight="20dp"
                android:background="@android:color/holo_red_dark"/>

        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:background="@android:color/holo_blue_dark"/>


    </LinearLayout>

</android.support.constraint.ConstraintLayout>

Upvotes: 0

rawhost
rawhost

Reputation: 154

Make the height of the second linerar layout wrap content. Your linear layout view is overlapped on the text view that why text view is not visible

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="250dp"
            android:layout_height="80dp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="10dp"
            android:background="@android:color/holo_green_dark"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_marginTop="20dp"
            android:layout_marginRight="20dp"
            android:background="@android:color/holo_red_dark"/>

    </LinearLayout>

Upvotes: 1

Rajan Kali
Rajan Kali

Reputation: 12953

make height of horizontal LinearLayout to wrap_content as in

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="250dp"
                android:layout_height="80dp"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="10dp"
                android:background="@android:color/holo_green_dark"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:layout_marginTop="20dp"
                android:layout_marginRight="20dp"
                android:background="@android:color/holo_red_dark"/>

        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:background="@android:color/holo_blue_dark"/>


    </LinearLayout>

Upvotes: 0

Related Questions