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

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?


Solution 1:

    android:layout_gravity="center" 

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

Solution 2:

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