I have a DrawerLayout with a Coordinator layout hosted as the main content. When I create a SnackBar with the Snackbar.make method, the FAB hosted in the Coordinator layout refuses to animate.
I find this odd, because I've used the same FAB in Coordinator layouts that aren't wrapped in a DrawerLayout and it animates just fine, leading to believe the DrawerLayout is somehow blocking the call back.
I've tried making the CoordinatorLayout the top level view, wrapping the DrawerLayout, but that also doesn't work. What I'm going to attempt is forking the FloatingActionButton Behavior class and making the updateFabTranslationForSnackbar method public so I can call it myself. I'd much rather not do this, so any ideas will be much appreciated.
For both activities, the Snackbar.make call is called from a fragment added dynamically to the RelativeLayout with the ID "container". The view passed to the call is the CoordinatorLayout in the activity XML with the id "coordinator_layout".
Again, everything works as it should in the first XML, but not in the second.
This is my default XML for other activities, the FAB animates fine here:
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinator_layout"
android:name="com.app.mobile.app.ui.BusinessActivityFragment"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/progress_bar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="8dp" />
<RelativeLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<ImageView
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:scaleType="centerCrop"
android:src="#drawable/bg_sign_up"
android:visibility="invisible" />
<View
android:id="#+id/background_mask"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:background="#color/black_40"
android:visibility="invisible" />
</RelativeLayout>
<include
android:id="#+id/action_bar"
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/abc_action_bar_default_height_material"
android:layout_gravity="top" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp" />
<ProgressBar
android:id="#+id/load_more"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="#dimen/half_margin"
android:layout_gravity="bottom"
android:indeterminate="true"
android:visibility="gone" />
This is the XML for my main activity where the FAB refuses to animate:
<android.support.v4.widget.DrawerLayout
android:id="#+id/navigation_layout"
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="com.app.mobile.app.ui.HomeActivity">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
<!-- Toolbar is the last item in the FrameLayout to cause it to overlay -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical">
<ProgressBar
android:id="#+id/home_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_marginBottom="8dp" />
<RelativeLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</RelativeLayout>
<include
android:id="#+id/action_bar"
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/abc_action_bar_default_height_material"
android:layout_gravity="top" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp" />
<ProgressBar
android:id="#+id/load_more"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="#dimen/half_margin"
android:layout_gravity="bottom"
android:indeterminate="true"
android:visibility="gone" />
</android.support.design.widget.CoordinatorLayout>
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<fragment
android:id="#+id/navigation_drawer"
android:name="com.app.mobile.app.ui.NavigationDrawerFragment"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:tag="NAVIGATION_DRAWER_TAG"
tools:layout="#layout/fragment_navigation_drawer" />
Make sure you are passing the right view to the Snackbar.make() method. As per the docs,
Snackbar will try and find a parent view to hold Snackbar's view from the value given to view. Snackbar will walk up the view tree trying to find a suitable parent, which is defined as a CoordinatorLayout or the window decor's content view, whichever comes first.
So you should pass a view that is actually inside the CoordinatorLayout, so that Snackbar will find the coordinator as the first parent available.
I found the solution. You apparently must move to the Navigation view widget in the new design library. Moving to this and keeping all else constant, the FAB animates again.
For implementation see The official blog post for the support libary and this demo of the library by Chris Banes.
This is not an ideal fix, as you lose the flexibility of a fragment to a static inflated XML resource, but it's the only way I've found to make the FAB still animate.
Related
I want to hide my bottom bar on scroll but a FAB should stay on the screen.
If I put the FAB on top of BottomNavigationView using anchors but it appears behind it.
If I put layout_insetEdge="bottom" to the BottomNavigationView then it works but make my tests fail (https://issuetracker.google.com/issues/70162122) so I cannot use that at the moment.
Layout:
<?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:tools="http://schemas.android.com/tools"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".screens.main.MainActivity">
<FrameLayout
android:id="#+id/mainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/scrolling_view_behaviour" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/mainNewQuestionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/spacing_8dp"
app:layout_anchor="#+id/mainBottomNavigationView"
app:layout_anchorGravity="top|right|end"
app:srcCompat="#drawable/create_question"
tools:visibility="visible" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/mainBottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="#color/my_gray"
app:itemIconTint="#drawable/selector_bottombar_item"
app:itemTextColor="#drawable/selector_bottombar_item"
app:layout_anchor="#id/mainContainer"
app:layout_anchorGravity="bottom"
app:layout_behavior="#string/hide_bottom_navigation_view_behaviour"
app:menu="#menu/bottom_navigation_main" />
</android.support.design.widget.CoordinatorLayout>
Just use:
<androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_gravity="bottom"
.../>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_gravity="top"
app:layout_anchor="#id/bottom_navigation"
app:layout_anchorGravity="top|end"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
You seem to be having a Z-Ordering issue here.
You could try the fab.bringToFront() followed by fab.invalidate() for api's below 21, and ViewCompat.setZ(fab, someFloat) for api's above 21.
I used these methods to bring a custom FloatingActionMenu, consisting of a series of FAB's I wrote for a project, to the front so they would overlap everything on the screen.
I want to create an overlay kind of layout to be displayed above an activity including toolbar. I cannot use any library to do this as everything that I've written is custom. I was able to achieve it by adding my view to decor view directly, by using this code :
ViewGroup vg = (ViewGroup)(getWindow().getDecorView().getRootView());
tutorialViewContainer = (RelativeLayout) View.inflate(this, R.layout.layout_simple_overlay, null);
vg.addView(tutorialViewContainer, params);
but this code has an issue that it covers even the status bar and navigation bar. I only need to cover activity space including toolbar.
I have to make something similar to these post - here and here
but the issue is I'm using a coordinator layout having a app bar added to it. So I'm unable to make the view visible above the toolbar.
Here is the xml code. simple_overlay_container is the view that I've added for overlay but it is displayed below the toolbar.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="#+id/main_content"
style="#style/ActivityBaseStyle"
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:fitsSystemWindows="false"
tools:context=".activity.BaseAmcatQuestionActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/main.appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include
android:id="#+id/toolbar_base_question_activity"
layout="#layout/toolbar_test_custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/fl_container_base_amcat"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
<!--Question View Anchor Fab-->
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab1_language"
android:layout_width="wrap_content"
android:layout_height="#dimen/fab_size_40"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/margin_fab_first"
android:clickable="true"
android:scaleType="center"
android:src="#drawable/ic_fab_language"
app:backgroundTint="#color/bg_color_fab_language"
app:layout_anchor="#+id/toolbar_base_question_activity"
app:layout_anchorGravity="bottom|right|end"/>
<!--info view anchor FAB-->
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab2_info"
android:layout_width="wrap_content"
android:layout_height="#dimen/fab_size_40"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/margin_fab_second"
android:clickable="true"
android:scaleType="center"
android:src="#drawable/ic_fab_info"
android:visibility="gone"
app:backgroundTint="#color/bg_color_fab_info"
app:layout_anchor="#+id/fab1_language"
app:layout_anchorGravity="bottom|center_horizontal"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab3_question"
android:layout_width="wrap_content"
android:layout_height="#dimen/fab_size_40"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/margin_fab_third"
android:clickable="true"
android:scaleType="center"
android:src="#drawable/ic_fab_question"
android:visibility="gone"
app:backgroundTint="#color/bg_color_fab_question"
app:layout_anchor="#+id/fab2_info"
app:layout_anchorGravity="bottom|center_horizontal"/>
<RelativeLayout android:id="#+id/simple_overlay_container"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
app:layout_anchor="#+id/main.appbar"
app:layout_anchorGravity="top|left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transparent_overlay_gray_75"
android:visibility="gone"
android:clickable="true">
<ImageButton
android:id="#+id/imageButton_zoom_image_answer_tutorial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#android:color/transparent"
android:paddingEnd="#dimen/margin_12"
android:paddingRight="#dimen/margin_12"
android:paddingTop="#dimen/margin_12"
android:src="#drawable/ic_zoom"
android:visibility="gone"
tools:ignore="RtlSymmetry"/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
I need help bring this view above the toolbar to cover the whole screen which include activity and toolbar not status bar or navigation bar.
I appreciate your help. Thanks in advance.
I have a layout for an Activity that I'm trying to add a navigation drawer to.
The problem is, to work properly, I need to use:
<android.support.v4.widget.DrawerLayout
instead of:
<RelativeLayout
but it messes things up. My ProgressBar becomes much bigger, my RecyclerView doesn't work, the app logs me out when I click something, etc.
My layout XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.st.mf.UserAreaActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="#dimen/activity_vertical_margin"
android:background="#fff">
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp" />
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#layout/navigation_menu"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
How can I create my drawer menu without messing everything else up?
Any direct child View of a DrawerLayout that's not a drawer is considered a content View, and will be laid out to match_parent in both directions, regardless of the width and height attributes you've set on it. In your case - indeed, in most cases - you only need one content View, so the rest of the non-drawer Views should all be inside a single ViewGroup.
We'll place your ProgressBar and RecyclerView both inside a RelativeLayout that acts as the content View, where they'll keep the layout attributes you've set. For example:
<android.support.v4.widget.DrawerLayout
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"
tools:context="com.st.mf.UserAreaActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/activity_vertical_margin"
android:background="#fff">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp" />
</RelativeLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#layout/navigation_menu"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Note that the content View should always be listed before any drawers, to maintain proper z-ordering; i.e., to keep the drawers on top of the content.
How do I get my main layout to simply line up directly underneath my DrawerLayout? My main layout is listed below and is mainly made up of listviews. Right now everything is overlapping. So the below first shows my DrawerLayout. After closing of DrawerLayout my main activity is listed.
<android.support.v4.widget.DrawerLayout
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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
</LinearLayout>
<FrameLayout
android:id="#+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name=“com.example.my.app.FragmentDrawer"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent”
android:orientation="vertical">
<TextView
android:id="#+id/txt_empty_list_cars"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="4dp"
android:gravity="center"
android:text="#string/view1"
android:textAppearance="?android:attr/textAppearance"
android:textColor="#android:color/darker_gray"
android:visibility="gone" />
<ListView
android:id="#+id/list_cars"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txt_header"
android:layout_margin="4dp"
android:divider="#android:drawable/divider_horizontal_bright"
android:dividerHeight="0.5dp"
android:listSelector="#android:drawable/list_selector_background" />
So, your problem is drawer layout overlapping the linear layout below. Use
android: layout_below="#id/drawer_layout"
In your linear layout. That will linearly arrange both layouts.
I dont know if your are looking for a sliding panel like the new gmail app
but if yes you can refer yourself Sliding Pane Layout and this very awesome tutorial.
hope it helps, happy codings.
In order to use the DrawerLayout correctly, it has to be the only root / parent view of your entire layout.
<android.support.v4.widget.DrawerLayout>
/*other nested child views */
<fragment
android:id="#+id/fragment_navigation_drawer"
...
/>
</android.support.v4.widget.DrawerLayout>
Also, to prevent the over lapping, ensure that you have two direct child views as per design guideline at
https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
"To use a DrawerLayout, position your primary content view as the first child with a width and height of match_parent . Add drawers as child views after the main content view and set the
layout_gravity appropriately."
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:layout_gravity="center"
works just fine.
And remove your FrameLayout, it's redundant.
After some research I failed to find a way to make this work, so in the end I did this:
Put the progress bar inside the CoordinatorLayout with 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
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>
Put the CoordinatorLayout inside RelativeLayout and add progressbar inside this RelativeLayout. Make CenterInParent for progressbar to true. Make progressbar Gone/Visible as per need.
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:
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>