Skip to content
Advertisement

How can I conditionally ‘remove’ or hide a button?

I have a fragment containing a recyclerView.

default UI

When an item is clicked on in the recyclerView I set the recyclerview item background to green and change the “saveBtn” text to “Update”.

after click UI

I need to also be able to remove the “deletebtn” every time a user clicks on a recyclerView item, or hide it so that the UI looks somewhat like this:

UI change on recyclerView click

How could this be done?

Method I am using to update UI on recyclerView click

    public void onExerciseClicked(int position) {
        saveBtn.setText("Update");
        clearBtn.setText("Delete");
    }

XML

<?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"
    android:background="@drawable/gradientfrozen"
    android:id="@+id/constraint_layout21">

    <LinearLayout
        android:layout_width="0dp"

        android:layout_height="0dp"
        android:background="#292929"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


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



            <TextView
                android:id="@+id/textView5"
                android:layout_width="327dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:paddingLeft="20dp"
                android:layout_marginTop="10dp"
                android:text="WEIGHT (kgs)"
                android:textColor="@android:color/background_light"
                android:textSize="16dp" />

        </LinearLayout>

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="75dp"
                android:layout_marginLeft="30dp"

                android:layout_marginRight="30dp"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/dec_weight"
                    android:layout_width="1dp"
                    android:layout_height="50dp"
                    android:layout_weight="0.5"
                    android:background="@drawable/down22"
                    android:textColor="@color/design_default_color_background" />

                <EditText
                    android:id="@+id/editTextWeight"
                    android:layout_width="80dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_weight="1"
                    android:backgroundTint="@color/green"
                    android:ems="10"
                    android:gravity="center"
                    android:hint="0.0"
                    android:inputType="numberDecimal"
                    android:singleLine="false"
                    android:textColor="@color/design_default_color_background"
                    android:textColorHint="@color/light_grey"
                    android:textSize="30sp" />

                <Button
                    android:id="@+id/inc_weight"
                    android:layout_width="1dp"
                    android:layout_height="50dp"
                    android:layout_weight="0.5"
                    android:background="@drawable/up22" />

            </LinearLayout>


            <TextView
                android:id="@+id/textView8"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="20dp"
                android:text="REPS"
                android:textColor="@android:color/background_light"
                android:textSize="16dp" />


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


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="80dp"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="30dp"
                    android:orientation="horizontal">



                    <Button
                        android:id="@+id/dec_reps"
                        android:layout_width="1dp"
                        android:layout_height="50dp"
                        android:layout_weight="1.6"
                        android:background="@drawable/down22"
                        android:shadowColor="@color/design_default_color_background" />

                    <EditText
                        android:id="@+id/editTextReps"
                        android:layout_width="161dp"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_weight="1"
                        android:backgroundTint="@color/green"
                        android:ems="10"
                        android:gravity="center"
                        android:hint="0"
                        android:inputType="number"
                        android:textColor="@color/design_default_color_background"
                        android:textColorHint="@color/light_grey"
                        android:textSize="30sp" />

                    <Button
                        android:id="@+id/inc_reps"
                        android:layout_width="1dp"
                        android:layout_height="50dp"
                        android:layout_weight="1.6"
                        android:background="@drawable/up22" />


                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:orientation="horizontal">

                    <Button
                        android:id="@+id/save_btn"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginRight="20dp"
                        android:layout_weight="1"
                        android:background="@drawable/my_small_green_shape"
                        android:text="Save"
                        android:textColor="#ffffff"
                        android:textSize="20sp" />

                    <Button
                        android:id="@+id/clear_btn"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginRight="20dp"
                        android:layout_weight="1"
                        android:background="@drawable/my_small_red_shape"
                        android:text="Clear"
                        android:textColor="#ffffff"
                        android:textSize="20sp" />

                </LinearLayout>



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


                    <TextView
                        android:id="@+id/textView"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:text="Set"
                        android:textColor="@android:color/holo_green_light" />

                    <TextView
                        android:id="@+id/textView4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:text="Kgs"
                        android:textColor="@android:color/holo_green_light" />

                    <TextView
                        android:id="@+id/textView7"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:text="Reps"
                        android:textColor="@android:color/holo_green_light" />
                </LinearLayout>


                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/completed_exercise_ListView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="25dp"
                    android:layout_marginRight="25dp"
                    android:background="#292929"
                    tools:listitem="@layout/completed_exercise_item" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Advertisement

Answer

Using LinearLayout as a direct child of ConstraintLayout defeats the whole purpose of using ConstraintLayout. It is not a good practice to use nested layout from the performance point of view. Besides, if deleteBtn is direct child of ConstraintLayout, then setting the layout_width of saveBtn to match_constraint will enable it to take up the whole space if we change the visibility of deleteBtn to gone.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement