Is it possible to center a layout/view inside CoordinatorLayout?

AndroidAndroid Coordinatorlayout

Android Problem Overview


I have a FrameLayout inside CoordinatorLayout to inflate my fragments which contain RecyclerView. The thing is, I want to display a progress bar when loading the RecyclerView, but the progress bar is always at the top of the screen under the Toolbar. I have tried various layouts, setting gravity or centerInParent but none had worked. So is there any way to achieve this?

Android Solutions


Solution 1 - Android

    android:layout_gravity="center" 

works just fine. And remove your FrameLayout, it's redundant.

Solution 2 - Android

After some research I failed to find a way to make this work, so in the end I did this:

  1. Put the progress bar inside the CoordinatorLayout with android:layout_gravity="center".

  2. In the activity, make 2 methods to show/hide progress bar:

    public void showProgress() {
          progressBar.setVisibility(View.VISIBLE);
    }
    
    public void hideProgress() {
          progressBar.setVisibility(View.INVISIBLE);
    }
    
  3. In the fragment, get a reference of the above activity through getActivity(), cast it to the actual activity and call the necessary method.

    context = getActivity();
    ((MainActivity)context).showProgress();
    

EDIT: this seems to be fixed in support library 23.1.1

Solution 3 - Android

I have created My View Like:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:wheel="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="fill_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/order_list_app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/order_list_collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|enterAlways">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="@dimen/padding_normal">

                <TextView
                    android:id="@+id/order_list_outstanding_amount"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@android:id/icon"
                    android:layout_margin="@dimen/margin_normal"
                    android:layout_marginLeft="@dimen/margin_high"
                    android:layout_toRightOf="@android:id/icon"
                    android:text="@string/format_outstanding_amout"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/font_large" />
            </RelativeLayout>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>


        <android.support.v7.widget.RecyclerView
            android:id="@+id/order_list_recycler_view"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </android.support.v7.widget.RecyclerView>

        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:layout_margin="16dp"
            android:src="@android:drawable/ic_input_add"
            app:layout_anchor="@id/order_list_recycler_view"
            app:layout_anchorGravity="bottom|right|end" />

        <include
            android:id="@+id/order_list_empty_view"
            layout="@layout/empty_view"></include>

    <com.pnikosis.materialishprogress.ProgressWheel
        android:id="@+id/progress_wheel"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="center"
        wheel:matProg_barColor="@color/colorAccent" />
</android.support.design.widget.CoordinatorLayout>

enter image description here

Solution 4 - Android

Put the CoordinatorLayout inside RelativeLayout and add progressbar inside this RelativeLayout. Make CenterInParent for progressbar to true. Make progressbar Gone/Visible as per need.

Solution 5 - Android

If you are going to center a view inside another, you need to add constrainsts to all sides:

    app:layout_constraintBottom_toBottomOf="@id"
    app:layout_constraintLeft_toLeftOf="@id"
    app:layout_constraintRight_toRightOf="@id"
    app:layout_constraintTop_toTopOf="@id"

For the example below, I simply centered a TextView in the middle of a constraint layout.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
            android:text="This is a centered View"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

The result:

centered view in coordinator layout

Solution 6 - Android

Use FrameLayout inside RelativeLayout and set CenterInParent property to Progress bar.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/myFrameLyt"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKiseView Question on Stackoverflow
Solution 1 - AndroidDefueraView Answer on Stackoverflow
Solution 2 - AndroidKiseView Answer on Stackoverflow
Solution 3 - AndroidPratik ButaniView Answer on Stackoverflow
Solution 4 - AndroidSwapnil ChaudhariView Answer on Stackoverflow
Solution 5 - AndroidDimas MendesView Answer on Stackoverflow
Solution 6 - AndroidMobile Team ADR-FlutterView Answer on Stackoverflow