Search code examples
androidandroid-layoutandroid-listviewandroid-progressbarandroid-relativelayout

ProgressBar is not center in RelativeLayout when a ListView inside layout


I have the following xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <include
    android:id="@+id/toolbar"
    layout="@layout/toolbar_layout" />

  <ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/toolbar"
    android:gravity="center_horizontal"
    android:padding="@dimen/dimen_16dp"
    android:textSize="@dimen/dimen_16sp" />

  <ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:visibility="visible" />
</RelativeLayout>

I have a toolbar and a listView below it. Until I populate the list I want to display a ProgressBar in center of the layout. I set centerInParent but it doesn't work. The progressBar is displayed over the toolbar.

enter image description here

I tried to put the progressBar inside a LinearLayout but I get the same result. How can I center it?

Edit

This is the toolbar_layout.xml :

  <?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar 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/toolbar"
  android:layout_width="match_parent"
  android:layout_height="@dimen/toolbar_height"
  android:background="@color/colorPrimary"
  app:contentInsetStart="0dp">

  <ImageButton
    android:id="@+id/home"
    android:layout_width="@dimen/margin_16dp"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:src="@drawable/ic_arrow_back_black_16dp"
    android:layout_marginStart="@dimen/margin_16dp"
    tools:ignore="contentDescription" />

  <TextView android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/margin_16dp"
    style="@style/ScreenTitleStyle"
    android:textColor="@color/textColorPrimary"/>
</androidx.appcompat.widget.Toolbar>

Solution

  • Try below snipped

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:gravity="center"
       android:orientation="vertical">
    
       <android.support.v7.widget.Toolbar
            android:layout_alignParentTop="true"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:elevation="4dp"
            android:id="@+id/toolbar" />
       <!-- <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar_layout" />-->
       <ListView
           android:id="@+id/list"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_below="@id/toolbar"
           android:gravity="center_horizontal"
           android:padding="16dp"
           android:textSize="16sp" />
    
        <ProgressBar
           android:id="@+id/progress_bar"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_centerInParent="true"
           android:visibility="visible" />
    
    </RelativeLayout>
    

    Your list view hight and width must be match_parent.