Reputation: 2905
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?
Upvotes: 52
Views: 29772
Reputation: 2802
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:
Upvotes: 1
Reputation: 2905
After some research I failed to find a way to make this work, so in the end I did this:
android:layout_gravity="center"
.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);
}
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
Upvotes: 13
Reputation: 542
Put the CoordinatorLayout inside RelativeLayout and add progressbar inside this RelativeLayout. Make CenterInParent for progressbar to true. Make progressbar Gone/Visible as per need.
Upvotes: 1
Reputation: 5506
android:layout_gravity="center"
works just fine. And remove your FrameLayout, it's redundant.
Upvotes: 73
Reputation: 62439
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>
Upvotes: 2
Reputation:
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>
Upvotes: 0