androidandroid-layoutandroid-linearlayoutandroid-buttonandroid-wrap-content

Android - Button within nested LinearLayout doesn't show


I am trying to create a vertical Linear Layout in which every row is a horizontal Linear Layout with 2 buttons (as you can see in the picture below). The height of both buttons should be determined by the content of the left one ("EXERCISE"), and the width of the icon on the right should ne equal to it's height. This is my code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".ui.home.ShowWorkout">

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

        <Button
            android:id="@+id/refresh_button"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginStart="2dp"
            android:layout_marginLeft="2dp"
            android:background="@drawable/icon_refresh"
            android:onClick="refresh"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
        <TextView
            android:id="@+id/FirstSetText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF"
            android:fontFamily="sans-serif"
            android:text="      First set"
            android:textColor="#000000"
            android:textSize="22sp" />

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:weightSum="9">

            <Button
                android:id="@+id/ex1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#AFEEEE"
                android:fontFamily="sans-serif"
                android:onClick="ShowExercise"
                android:text="Exercise"
                android:textColor="#FFFFFF"
                android:textSize="24sp"/>

            <Button
                android:id="@+id/ex1_refresh_button"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_weight="8"
                android:background="@drawable/icon_refresh"
                android:onClick="refresh"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:orientation="horizontal"
            android:weightSum="9">

            <Button
                android:id="@+id/ex2"
                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:background="#AFEEEE"
                android:fontFamily="sans-serif"
                android:onClick="ShowExercise"
                android:text="Exercise"
                android:textColor="#FFFFFF"
                android:textSize="24sp"/>

            <Button
                android:id="@+id/ex2_refresh_button"
                android:layout_width="fill_parent"
                android:layout_weight="8"
                android:layout_height="match_parent"
                android:background="@drawable/icon_refresh"
                android:onClick="refresh"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:orientation="horizontal"
            android:weightSum="9">

            <Button
                android:id="@+id/ex3"
                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:background="#AFEEEE"
                android:fontFamily="sans-serif"
                android:onClick="ShowExercise"
                android:text="Exercise"
                android:textColor="#FFFFFF"
                android:textSize="24sp"/>

            <Button
                android:id="@+id/ex3_refresh_button"
                android:layout_width="fill_parent"
                android:layout_weight="8"
                android:layout_height="match_parent"
                android:background="@drawable/icon_refresh"
                android:onClick="refresh"/>
        </LinearLayout>

        <TextView
            android:id="@+id/SecondSetText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF"
            android:fontFamily="sans-serif"
            android:text="      Second set"
            android:textColor="#000000"
            android:textSize="22sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:orientation="horizontal"
            android:weightSum="9">

            <Button
                android:id="@+id/ex4"
                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:background="#AFEEEE"
                android:fontFamily="sans-serif"
                android:onClick="ShowExercise"
                android:text="Exercise"
                android:textColor="#FFFFFF"
                android:textSize="24sp"/>

            <Button
                android:id="@+id/ex4_refresh_button"
                android:layout_width="fill_parent"
                android:layout_weight="8"
                android:layout_height="match_parent"
                android:background="@drawable/icon_refresh"
                android:onClick="refresh"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:orientation="horizontal"
            android:weightSum="9">

            <Button
                android:id="@+id/ex5"
                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:background="#AFEEEE"
                android:fontFamily="sans-serif"
                android:onClick="ShowExercise"
                android:text="Exercise"
                android:textColor="#FFFFFF"
                android:textSize="24sp"/>

            <Button
                android:id="@+id/ex5_refresh_button"
                android:layout_width="fill_parent"
                android:layout_weight="8"
                android:layout_height="match_parent"
                android:background="@drawable/icon_refresh"
                android:onClick="refresh"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:orientation="horizontal"
            android:weightSum="9">

            <Button
                android:id="@+id/ex6"
                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:background="#AFEEEE"
                android:fontFamily="sans-serif"
                android:onClick="ShowExercise"
                android:text="Exercise"
                android:textColor="#FFFFFF"
                android:textSize="24sp"/>

            <Button
                android:id="@+id/ex6_refresh_button"
                android:layout_width="fill_parent"
                android:layout_weight="8"
                android:layout_height="match_parent"
                android:background="@drawable/icon_refresh"
                android:onClick="refresh"/>
        </LinearLayout>

        <TextView
            android:id="@+id/ThirdSetText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF"
            android:fontFamily="sans-serif"
            android:text="      Third set"
            android:textColor="#000000"
            android:textSize="22sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:orientation="horizontal"
            android:weightSum="9">

            <Button
                android:id="@+id/ex7"
                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:background="#AFEEEE"
                android:fontFamily="sans-serif"
                android:onClick="ShowExercise"
                android:text="Exercise"
                android:textColor="#FFFFFF"
                android:textSize="24sp"/>

            <Button
                android:id="@+id/ex7_refresh_button"
                android:layout_width="fill_parent"
                android:layout_weight="8"
                android:layout_height="match_parent"
                android:background="@drawable/icon_refresh"
                android:onClick="refresh"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:orientation="horizontal"
            android:weightSum="9">

            <Button
                android:id="@+id/ex8"
                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:background="#AFEEEE"
                android:fontFamily="sans-serif"
                android:onClick="ShowExercise"
                android:text="Exercise"
                android:textColor="#FFFFFF"
                android:textSize="24sp"/>

            <Button
                android:id="@+id/ex8_refresh_button"
                android:layout_width="fill_parent"
                android:layout_weight="8"
                android:layout_height="match_parent"
                android:background="@drawable/icon_refresh"
                android:onClick="refresh"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:orientation="horizontal"
            android:weightSum="9">

            <Button
                android:id="@+id/ex9"
                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:background="#AFEEEE"
                android:fontFamily="sans-serif"
                android:onClick="ShowExercise"
                android:text="Exercise"
                android:textColor="#FFFFFF"
                android:textSize="24sp"/>

            <Button
                android:id="@+id/ex9_refresh_button"
                android:layout_width="fill_parent"
                android:layout_weight="8"
                android:layout_height="match_parent"
                android:background="@drawable/icon_refresh"
                android:onClick="refresh"/>
        </LinearLayout>

        <TextView
            android:id="@+id/PointsText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF"
            android:fontFamily="sans-serif"
            android:text="      Total points:"
            android:textColor="#000000"
            android:textSize="22sp" />

        <Button
            android:id="@+id/goButton"
            style="@style/Widget.AppCompat.Button.Colored"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:onClick="goToShowBySets"
            android:text="GO!"
            android:textSize="34sp" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

And this is what it looks like:

[![enter image description here][1]][1]
[1]: https://i.sstatic.net/HmyKu.png][1]][1]

The rows in which I specify the height of the linear layout look good (all the rows other than the first one). But when I try doing it according to WrapContent, the button on the left doesn't show within the layout, and as you can see in the picture it appears to be lower down where the broken lines are. How do I fix it to look like the rest of the rows without specifying a height?


Solution

  • Eventually solved it by defining a set size for the icon. Not exactly what I was looking for, but close enough. This is what a single row's code looks like:

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <Button
                android:id="@+id/ex1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#AFEEEE"
                android:fontFamily="sans-serif"
                android:onClick="ShowExercise"
                android:text="Exercise"
                android:textColor="#FFFFFF"
                android:textSize="22sp"/>
    
            <Button
                android:id="@+id/ex1_refresh_button"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:background="@drawable/icon_refresh"
                android:onClick="refreshExercise"/>
        </LinearLayout>