How to show a Horizontal Android Indeterminate Progress Bar

夙愿已清 提交于 2019-11-28 06:43:51
Vinoth

I already knew that setIndeterminate will give an infinite horizontal progress bar. But it will be similar to the loading wheel, except that it will be horizontal. If you see my question I was looking for horizontal bar which starts from 0 and goes all the way to 100 (a gradual increase). If you want to achieve this in Android, you must use your progress bar as below:

 <ProgressBar
    android:id="@+id/progress_horizontal"
    android:indeterminateOnly="false"
    android:indeterminateDrawable="@drawable/progress_indeterminate_horizontal"
    android:progressDrawable="@drawable/progress_horizontal"
    android:minHeight="24dip"
    android:maxHeight="24dip" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

As I wanted to change the background of my progress bar, I changed the ProgressDrawable and IndeterminateDrawable. The original drawables are located under frameworks/base/core/res/res/drawable. Copy them to your project and change the color according to your needs.

Create a thread which updates the progress count and does a Thread.Sleep. Then it sends the message to the Handler which will update the progress bar in UI thread.

Use the method setIndeterminate of ProgressBar:

android.widget.ProgressBar bar = new android.widget.ProgressBar(context);
bar.setIndeterminate(true);

But yeah, you could have found this pretty quickly in the developer docs.

http://developer.android.com/reference/android/widget/ProgressBar.html#setIndeterminate%28boolean%29

To expand on Vinoth Answer, here is a ready code:

<ProgressBar
        android:id="@+id/progressBarLoadingRecite"
        android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
        android:minHeight="24dip"
        android:layout_marginTop="20dip"
        android:indeterminate="true"
        android:maxHeight="24dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

Maybe a bit late, but you can do something like this:

<ProgressBar
    android:id="@+id/progress"
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:indeterminate="true"
    style="?android:attr/progressBarStyleHorizontal" />

Hope it helps someone!

In the xml defining your progress bar, you can add

style="@android:style/Widget.ProgressBar.Horizontal"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!