Adrian Coutsoftides
Adrian Coutsoftides

Reputation: 1293

Button Not Showing Up In Layout

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>

Upvotes: 1

Views: 248

Answers (3)

pcodex
pcodex

Reputation: 1940

you can still set the TextView to match_parent. But remember to set a margin and layout weight.

Take a look at my snippet below

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:layout_margin="10dp"

    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:layout_gravity="center"
        android:text="Hello World!"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:text="Submit"/>

</LinearLayout>

Upvotes: 0

codeMagic
codeMagic

Reputation: 44571

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).

Upvotes: 1

Pomagranite
Pomagranite

Reputation: 696

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

Upvotes: 0

Related Questions