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.
Customizing a ProgressBar requires defining the attribute or properties for the background and progress of your progress bar. You can do this in the XML file or in the Activity (at run time). Show activity on this post.
To use the ActionBar utility methods, call the activity's getSupportActionBar() method. This method returns a reference to an appcompat ActionBar object. Once you have that reference, you can call any of the ActionBar methods to adjust the app bar. For example, to hide the app bar, call ActionBar.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With