Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android progressbar in a listview is always indeterminate

Tags:

android

I'm trying to set a fixed progress on items in a ListView. I only see an indeterminate spinning circle in the ListView.... what am I doing wrong? (I'm sure the answer is simple... I'm just not seeing it)

For the following adapter layout xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
    >
<TextView
        android:id="@+id/some_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
<ProgressBar
        android:id="@+id/some_progress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

Code:

public class SomeListAdapter extends ArrayAdapter<MyItem> {

private static class ViewHolder {
    TextView nameTextView;
    ProgressBar valueProgressBar;
}

public BestChoiceListAdapter(Context context, List<MyItem> items) {
    super(context, R.layout.list_item, items);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    View view = convertView;
    if (view == null) {
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = vi.inflate(R.layout.list_item, null);

        holder = new ViewHolder();
        holder.nameTextView = (TextView) view.findViewById(R.id.some_text);
        holder.valueProgressBar = (ProgressBar) view.findViewById(R.id.some_progress);

        view.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    MyItem optionItem = getItem(position);

    holder.nameTextView.setText(optionItem.getOptionName());
    int progress = (int) (optionItem.getOptionValue());

    holder.valueProgressBar.setProgress(progress);

    return view;
}
}
like image 621
Jeff Campbell Avatar asked Jun 08 '12 19:06

Jeff Campbell


People also ask

What is indeterminate ProgressBar?

Indeterminate ProgressBars are a useful tool for communicating to our users that an operation is in progress when we cannot predict how long it is likely to take.

What is a difference between SeekBar and ProgressBar in android?

A SeekBar in Android is an extension of ProgressBar that includes a movable thumb to change the progress in real-time. Android ProgressBar is a user interface control that indicates the progress of an operation.

How to show ProgressBar in android?

In android there is a class called ProgressDialog that allows you to create progress bar. In order to do this, you need to instantiate an object of this class. Its syntax is. ProgressDialog progress = new ProgressDialog(this);

What is ProgressBar in android?

Android ProgressBar is a graphical view indicator that shows some progress. Android progress bar displays a bar representing the completing of the task. Progress bar in android is useful since it gives the user an idea of time to finish its task.


1 Answers

The simplest way is to apply the horizontal style to it:

<ProgressBar
    android:id="@+id/some_progress"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

This will set up all the attributes you will otherwise need to add manually to apply a determinate progress mode.

If you're curious, this is what that system style looks like, in case you wanted to apply the properties individually:

<style name="Widget.ProgressBar.Horizontal">
    <item name="android:indeterminateOnly">false</item>
    <item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
    <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
    <item name="android:minHeight">20dip</item>
    <item name="android:maxHeight">20dip</item>
</style>

HTH

like image 180
devunwired Avatar answered Nov 16 '22 01:11

devunwired