Button Not Showing Up In Layout

Adrian Coutsoftides Source

despite addressing other similar questions I don't quite understand why these buttons won't appear in my layout screen, could someone please explain why?

The textview shows up fine

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.lab1.ac01220.com1032_lab.MainActivityFragment"
    tools:showIn="@layout/activity_main"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Hello World!"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/text_string"/>

    </LinearLayout>
</LinearLayout>
androidxmlandroid-layout

Answers

answered 3 months ago Pomagranite #1

This is a simple fix. Turn the device "horizontal" and you can see the button.

answered 3 months ago codeMagic #2

Your TextView is taking up all of the space do to the height and width being match_parent. And since LinearLayouts are laid out in sequential order there's no space for anything that comes after it.

<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Hello World!"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

Change that to wrap_content, use layout_weight, or a specific dimension (whatever suits your needs).

comments powered by Disqus