Why is my progress bar not centered vertically?

I have the following layout ...

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/bg_generic_portrait"> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/issue_list_item_background_normal" android:listSelector="@drawable/issue_background_selector" android:divider="@drawable/separator" > </ListView> <ProgressBar android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="center" android:id="@+id/preloader" style="@android:style/Widget.ProgressBar.Large"/> <TextView android:id="@android:id/empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FF0000"/> </LinearLayout> 

And it looks like this: http://i.stack.imgur.com/Ra5c6.png

I want the preloader to be centered vertically too, what's wrong?

+6
source share
5 answers

Somehow layout_gravity is not working. What you need to do is to enclose the linear_layout line in progress_bar and set the gravity of this layout to the center, for example:

 <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center"> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/preloader" style="@android:style/Widget.ProgressBar.Large"/> </LinearLayout> 

I do not suggest setting gravity to the topmost linear layout because you can have fun things if you change the list or add something.

+12
source

It seems the problem is what you are trying to do. The ListView on top of the progress bar in your layout may have a variable height depending on its contents. Thus, it is not possible to ensure that the progress bar is in the center of the screen using a linear layout.

You can look at the ProgressDialog class, which will show a progress dialog when loading data into a list in the background.

Alternatively, you can overlap the ProgressBar and ListView with RelativeLayout as the top-level layout and specify the android:layout_centerInParent="true" attribute android:layout_centerInParent="true" for the ProgressBar

+1
source

thats, because you are using LinearLayout , I suggest you use RelativeLayout and have an attitute center using layout_centerVertical as follows:

 <ProgressBar android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_centerVertical="true" android:id="@+id/preloader" style="@android:style/Widget.ProgressBar.Large"/> 
0
source

I don't know how much space your Listview takes, but you can use <RelativeLayout> instead of <LinearLayout> if you really want to determine where you want x item.

Hope this helps.

0
source

Set android:gravity="center" in the parent LinearLayout.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:gravity="center" android:layout_width="fill_parent" android:layout_height="fill_parent"> 
0
source

Source: https://habr.com/ru/post/917471/


All Articles