How can I place a ProgressBar at the right of the Toolbar?
Solution 1
You can try this. It worked for me. Key here is to define layout_gravity
in the xml: android:layout_gravity="right"
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/material_green_500"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<!-- Color is Brown 500 -->
<ProgressBar
android:id="@+id/toolbar_progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateTint="#795548"
android:indeterminateTintMode="src_in"
android:layout_gravity="right"
/>
</android.support.v7.widget.Toolbar>
Solution 2
I also hit the same wall, but programmatically it works:
Toolbar.LayoutParams layoutParams = new Toolbar.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
Gravity.TOP | Gravity.RIGHT);
In my snippet, I align it to the top, to match the alignment of the menu.
Related videos on Youtube
doplumi
Updated on September 12, 2022Comments
-
doplumi over 1 year
With the new Lollipop API, we have to use a Toolbar if we want to personalize the action bar aspect.
Adding a ProgressBar to the Toolbar is as simple as adding it to the Toolbar ViewGroup, as Chris Banes said.
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/material_green_500" android:minHeight="?attr/actionBarSize" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <!-- Color is Brown 500 --> <ProgressBar android:id="@+id/toolbar_progress_bar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:indeterminateTint="#795548" android:indeterminateTintMode="src_in"/> </android.support.v7.widget.Toolbar>
But how can we place it at the right of the Toolbar, where it belongs?
The
layout_gravity
attribute seems to not be defined for the Toolbar. Setting it from the xml has no effect. I tried to change the width of the ProgressBar, with no success.What do I do?
EDIT: There is a programmatical solution to this problem, see @mdelolmo reply for that.
-
Philio over 9 yearslayout_gravity is working fine in appcompat 21.0.2
-
-
doplumi over 9 yearsI already tried programmatically, and yes, it's working. The problem is the XML declaration. +1'ed because that was something worth mentioning anyway, thank you!
-
bryant1410 almost 8 yearsPrefer using
layout_gravity="end"
for Right-To-Left compatibility. However notice this is only available from API 17.