How to align the views to center when other views visibility are in "gone" in xml

Viewed 111

discover.xml :

<?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="wrap_content"
    android:orientation="vertical"
    app:behavior_peekHeight="@dimen/peek_height">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/discover_displays"
            android:layout_width="match_parent"
            android:layout_height="@dimen/peek_height"
            android:background="@drawable/round_corners"
            android:orientation="vertical">

            <TextView
                android:id="@+id/to"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingTop="@dimen/pad_17"
                android:paddingBottom="@dimen/pad_16"
                android:text="@string/to"
                android:textAlignment="center"
                android:textColor="@color/light_gray"
                android:textSize="@dimen/pad_12"
                android:visibility="gone"
                android:textStyle="bold" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/disp"
                android:layout_width="match_parent"
                android:layout_height="@dimen/disp_height"
                tools:listitem="@layout/disp_item" />

            <ProgressBar
                android:id="@+id/progress"
                style="?android:attr/small"
                android:layout_width="@dimen/progress_bar"
                android:layout_height="@dimen/progress_bar"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/text_width"
                android:indeterminate="true"
                android:progressBackgroundTint="@color/progressbar_background"
                android:progressTint="@color/progressbar_color" />

            <TextView
                android:id="@+id/discovery_message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/pad_26"
                android:gravity="center"
                android:paddingStart="@dimen/element_margin"
                android:paddingEnd="@dimen/element_margin"
                android:text="@string/discovery_message"
                android:textColor="@color/text_color"
                android:textSize="@dimen/text_size"
                android:visibility="visible" />

            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/error_action"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/pad_12"
                android:background="@color/discovery_error_item"
                android:padding="@dimen/discovery_error"
                android:textColor="@color/white"
                tools:text="@string/bluetooth_label" />

        </LinearLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>

Current Layout :

enter image description here

Expected layout : The Recyclerview in the above pick should have a height as below, while the 3 views progressbar, discover state message, error action should be aligned in the middle as in order (one below the other). How to accomplish the same ?

enter image description here

The view with id : "to" is optional, since its being used in two fragments, 1 fragment uses it and other dont, hence the visibility is hidden and made visible whenever required. I am using the coordinator layout, since a bottomsheet view is being used.

The recyclerview height needs to be adjusted, keeping the progress bar, discovery state message, error actions being aligned to center when its visible one after the other (either recyclerview is visible at a time, or the 2 views are visible at a time (progress bar, discover error state or the button) or only the error button is visible at a time aligned to the center) . Please help in aligning the view items to the center when the visibility of one view is not present. currently the view aligns to the top if there is no visibility. Thanks.

2 Answers

I believe you can nest ConstraintLayout within the CoordinatorLayout and implement your screen in the Constraint Layout by adding the constraints you wish.

Should be easy enough.

You have to use a combine of linear layout and relative layout with the relative as parent , and recycler and the linear as childs of it , its look like this :

<RelativeLayout>
<RecyclerView android:layout_width="match_parent" android:layout_height="match_parent"\><LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android: orientation="vertical" ... android:gravity="center"></LinearLayout>

</RelativeLayout>
Related