With the new Toolbar API in Android Lollipop and AppCompat-v7, they are removing a lot of the automatic features to make Toolbar/ActionBar more robust. One of those things is the progress bar. Since Toolbar is simply a ViewGroup, I assumed adding a ProgressBar would be simple. However, I cannot seem to get it to work properly.
I have done the following (using the SmoothProgressBar library):
// I instantiate the toolbar and set it as the actionbar
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
// I create a ProgressBar and set the drawable to the SmoothProgressBar drawable
ProgressBar progressBar = new ProgressBar(this);
progressBar.setIndeterminateDrawable(new SmoothProgressDrawable.Builder(this).color(Color.BLUE).interpolator
(new DecelerateInterpolator()).sectionsCount(4).separatorLength(8).speed(2f).mirrorMode(true).build());
// I add the progressbar to the view with what I thought were the proper LayoutParams.
Toolbar.LayoutParams params = new Toolbar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 20);
params.gravity = Gravity.BOTTOM;
mToolbar.addView(progressBar, params);
progressBar.setIndeterminate(true);
I had figured this would work as I'm simply adding a ProgressBar to the bottom of the ViewGroup. However, it is not showing at all and is removing the Title. Below you can see a before and after. Does anyone know how to go about fixing this? My goal is to have a ProgressBar underneath the actionbar.
Before
After
In Android, by default a progress bar will be displayed as a spinning wheel but If we want it to be displayed as a horizontal bar then we need to use style attribute as horizontal. It mainly use the “android. widget. ProgressBar” class.
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. Show activity on this post.
In Android, SeekBar is an extension of ProgressBar that adds a draggable thumb, a user can touch the thumb and drag left or right to set the value for current progress. SeekBar is one of the very useful user interface element in Android that allows the selection of integer values using a natural user interface.
The Toolbar is a kind of ViewGroup that can be placed in the XML layouts of an activity. It was introduced by the Google Android team during the release of Android Lollipop(API 21). The Toolbar is basically the advanced successor of the ActionBar.
The easiest way is to the to add the ProgressBar
directly in the XML-Layout files.
Using a RelativeLayout
as root and use android:layout_below
to keep the ProgressBar
and the main content below the toolbar.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"/>
<fr.castorflex.android.smoothprogressbar.SmoothProgressBar
android:id="@+id/loadProgressBar"
style="@style/LoadProgressBar"
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_below="@+id/toolbar"
android:indeterminate="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:orientation="vertical">
<!-- Your Content here -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Content Text"/>
</LinearLayout>
</RelativeLayout>
Now you can access the Toolbar
and the ProgressBar
in the Activitiy
onCreate
method
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
progressBar = (SmoothProgressBar) findViewById(R.id.loadProgressBar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
}
include
A more general approach is to put the Toolbar
and the ProgressBar
in a separate XML-Layout file and include this in the activity layout.
toolbar.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"/>
<fr.castorflex.android.smoothprogressbar.SmoothProgressBar
android:id="@+id/loadProgressBar"
style="@style/LoadProgressBar"
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_below="@+id/toolbar"
android:indeterminate="true"/>
</merge>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/toolbar"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:orientation="vertical">
<!-- Your Content here -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Content Text"/>
</LinearLayout>
</RelativeLayout>
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