How to Use Android Progressbar in Determinate Mode

How do I use Android ProgressBar in determinate mode?

use the style ?android:attr/progressBarStyleHorizontal

for example:

  <ProgressBar android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"

and this is an example with MediaPlayer:

package com.playerpgbar;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Player extends Activity implements Runnable, OnClickListener {

private TextView status;
private ProgressBar progressBar;
private Button startMedia;
private Button stop;
private MediaPlayer mp;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

status = (TextView) findViewById(R.id.status);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
startMedia = (Button) findViewById(R.id.startMedia);
stop = (Button) findViewById(R.id.stop);

startMedia.setOnClickListener(this);
stop.setOnClickListener(this);
}

@Override
public void onClick(View v) {
if (v.equals(startMedia)) {
if (mp != null && mp.isPlaying()) return;
mp = MediaPlayer.create(Player.this, R.raw.exodus_piranha);
mp.start();
status.setText(R.string.PlayingMedia);
progressBar.setVisibility(ProgressBar.VISIBLE);
progressBar.setProgress(0);
progressBar.setMax(mp.getDuration());
new Thread(this).start();
}

if (v.equals(stop) && mp!=null) {
mp.stop();
mp = null;
status.setText(R.string.Stopped);
progressBar.setVisibility(ProgressBar.GONE);
}
}

@Override
public void run() {
int currentPosition= 0;
int total = mp.getDuration();
while (mp!=null && currentPosition<total) {
try {
Thread.sleep(1000);
currentPosition= mp.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
progressBar.setProgress(CurrentPosition);
}
}
}

How to have a ProgressBar circular and in determinate mode?

Unfortunately, this is not possible with stock Android ProgressBar. Look for external libraries like:

http://github.com/dinuscxj/CircleProgressBar

How to make Android ProgressBar determinate in code?

From the ProgressBar source code here, the constructor you are calling is in line 237 which is calling the constructor in line 241 which in turn calls the constructor in line 245 with the style:

com.android.internal.R.attr.progressBarStyle

This style has the attribute android:indeterminateOnly set to true by default so your calls to setIndeterminate are ignored. See the function description at line 433.

I haven't done this but I assume that if you call the constructor in line 245 like this:

progressBar = new ProgressBar(getActivity(), null, <Your Style ID>);

passing as the third parameter a style definition with android:indeterminateOnly to false it should work. Based in the source code I assume that setIndeterminate is there only to enable it and not to disable it.

Hope this helps...

Android Circular Determinate ProgressBar

First add a progress_circle.xml to your res/drawable directory like this:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:drawable="@drawable/progress_circular_background"/>
<item>
<shape
android:innerRadiusRatio="3.4"
android:shape="ring"
android:thicknessRatio="6.0" >
<gradient
android:endColor="#ffffffff"
android:startColor="#ffffff"
android:type="sweep"
android:useLevel="true" />
</shape>
</item>
<item>
<rotate
android:drawable="@drawable/progress_particle"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" />
</item>

</layer-list>

I Googled image searched both "progress_particle.png" and "progress_circular_background.png" (with quotes) since the android default drawables for those were missing. You'll probably want to customize those but they'll do to get you started.

Then in your xml layout:

<ProgressBar
android:id="@+id/timer_progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="200dp"
android:indeterminate="false"
android:max="60"
android:progressDrawable="@drawable/progress_circle" />

My max is 60, since I'm using it for a seconds timer, but you might have something different.

The trick is that you need to use style="?android:attr/progressBarStyleHorizontal" even though it's a circular progress.

Android progress bar is always indeterminate on changing its style to progressBarStyleInverse from progressBarStyleHorizontal

The trick is that you need to use style="?android:attr/progressBarStyleHorizontal" even though it's a circular ProgressBar.
And set a circular drawable as a progress drawable.

Refer to this question :

Android Circular Determinate ProgressBar

How to change progress bar's progress color in Android

I'm sorry that it's not the answer, but what's driving the requirement setting it from code ?
And .setProgressDrawable should work if it's defined correctly

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>

<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>

<item android:id="@android:id/progress">
<clip>
<shape>
<corners
android:radius="5dip" />
<gradient
android:startColor="@color/progress_start"
android:endColor="@color/progress_end"
android:angle="270"
/>
</shape>
</clip>
</item>

</layer-list>


Related Topics



Leave a reply



Submit