Sérgio Carneiro
Sérgio Carneiro

Reputation: 3966

ProgressBar's progress not working properly when activity starts

I'm using an ExpandableListActivity and at the bottom I have a ProgressBar to determine how much lines were checked.

Activity's XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:id="@+id/activity_main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100" >

    <include
        android:id="@+id/activity_checklist_titlebar"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="10"
        layout="@layout/titlebar" />

    <ExpandableListView
        android:id="@+id/android:list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="90" >
    </ExpandableListView>
</LinearLayout>

<ProgressBar
    android:id="@+id/activity_checklist_progress"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="@dimen/height_activity_checklist_progress"
    android:layout_alignParentBottom="true" />

<TextView
        android:id="@+id/activity_checklist_progress_text"
        style="@style/Text.Strong.Medium"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/height_activity_checklist_progress"
        android:gravity="center"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="0/50" />

</RelativeLayout>

I update the progress here:

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) 
{
    // ...
    TextView overall_progress_text = 
       (TextView) main_layout.findViewById(R.id.activity_checklist_progress_text);
        int overall_progress = checklist.getProgress();
        overall_progress_text.setText(overall_progress + "/" + checklist.size());
    ProgressBar overall_progress_bar = 
       (ProgressBar) main_layout.findViewById(R.id.activity_checklist_progress);
        overall_progress_bar.setProgress(overall_progress);
        overall_progress_bar.setMax(checklist.size());

    return convertView;
}

As you can see I have a TextView that shows the progress too, and apparently the values are fine. Everything works fine too when I start to change the checkboxes' state in the list, the only problem is at the start of the activity.

The progressBar shows this (I think the max value is taking '100' instead of '9'):

enter image description here

Upvotes: 0

Views: 990

Answers (2)

S&#233;rgio Carneiro
S&#233;rgio Carneiro

Reputation: 3966

I simply solved my problem setting the max value before the progress. It seems Android's ProgressBar doesn't work even if you set the max right after the progress...

Changed this:

overall_progress_bar.setProgress(overall_progress);
overall_progress_bar.setMax(checklist.size());

To this:

overall_progress_bar.setMax(checklist.size());
overall_progress_bar.setProgress(overall_progress);

Upvotes: 2

Nirav Tukadiya
Nirav Tukadiya

Reputation: 3417

you can use Async Task to view progress and in onProgressUpdate u can put a progress dialog or a progress bar..

Upvotes: 0

Related Questions