Show numbers in ProgressBar
Asked Answered
T

4

8

I have a ProgressBar. It works ok but I need to show the max value and the current value. They are not there.

Do I need to change to a ProgressDialog?

Clarification:

I am showing a ProgressBar. The bar comes up perfectly but I want the numbers to appear as well. So, for example, it the max is 1000 the current point is 300 then I want 1000 to appear and the end of the bar and 300 to appear somewhere on the bar or just below it.

I am using this class as the List Adaptor for the filling of an ListView. The line displayed depends on the type of data in the Array of "awards". When doing a ProgressBar it appears without the numbers.

Code:

private class AwardsAdapter extends ArrayAdapter <AwardItem>{


    private ArrayList<AwardItem> awards = new ArrayList<AwardItem>();

    public AwardsAdapter(Context context, int textViewResourceId, ArrayList<AwardItem> awards) {
        super(context, textViewResourceId,awards);
        this.awards = awards;
        // TODO Auto-generated constructor stub
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)AwardsActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.awards_row, parent, false);
        }
        AwardItem award = awards.get(position);
        ProgressBar pb = (ProgressBar) v.findViewById(R.id.pgbAwardProgress);

        if(award.award_type == PROGRESSIVE) {
            pb.setVisibility(View.VISIBLE);
            pb.setMax(award.requirement);
            pb.setProgress(award.current_amount);
            pb.setIndeterminate(false);

        } else {
            pb.setVisibility(View.GONE);
        }
        ((TextView) v.findViewById(R.id.tvwShortMessage)).
                      setText(MessageBuilder.buildMessage(AwardsActivity.this, award.award_text, 
                              Integer.valueOf(award.requirement).toString()));

        return v;
    }

}

Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent"
 android:layout_height="wrap_content">
<TextView android:id="@+id/tvwShortMessage"
android:typeface="sans"
android:textSize="14sp"
android:textStyle="italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwLongMessage"
android:typeface="sans"
android:textSize="14sp"
android:textStyle="italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvwShortMessage"/>
<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"    
android:layout_marginBottom="15dp" 
android:layout_below="@id/tvwLongMessage">
<ProgressBar
android:id="@+id/pgbAwardProgress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"    
style="?android:attr/progressBarStyleHorizontal"
android:layout_marginRight="5dp" 
android:layout_marginTop="5dp" 
android:layout_marginBottom="15dp" 
android:layout_alignParentTop="true"/>
</LinearLayout>
</RelativeLayout>

This is the final screen. It is a list of a series of tasks and their progress so far. Each "line" is a separate progressbar.

enter image description here

Trenna answered 10/11, 2011 at 13:37 Comment(6)
Added more clarification to the question.Trenna
Means you just want to display minimum and maximum value in each listview row ?Manx
Minimum is always zero - but want maximum and current.Trenna
Add a textview in a listview row for display current and maximum value and set it from getview() method.You already have value maximum = award.requirement; and current = award.current_amount; that you are setting in progressbarManx
Question is why doesn't it appear automatically? A progress bar is meant to show it.Trenna
Could you just put 2 textviews and update them as you are the progress bar?Crematorium
G
3

Why dont you just add the text manually, it seems like a much simple approach, I placed it where ever I wanted here, but you can just change the layout to what your need are.

Sorry if there are any mistakes in the code, I didnt run this.

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/tvwShortMessage"
android:typeface="sans"
android:textSize="14sp"
android:textStyle="italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwLongMessage"
android:typeface="sans"
android:textSize="14sp"
android:textStyle="italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvwShortMessage"/>
<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"    
android:layout_marginBottom="15dp" 
android:layout_below="@id/tvwLongMessage">
<ProgressBar
android:id="@+id/pgbAwardProgress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"    
style="?android:attr/progressBarStyleHorizontal"
android:layout_marginRight="5dp" 
android:layout_marginTop="5dp" 
android:layout_marginBottom="15dp" 
android:layout_alignParentTop="true"/>
</LinearLayout>
<TextView android:id="@+id/pgbAwardProgressText"
android:typeface="sans"
android:textSize="14sp"
android:textStyle="italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pgbAwardProgress"
android:layout_alignParentRight="true"
android:visibility="gone"/>
</RelativeLayout>

Class:

private class AwardsAdapter extends ArrayAdapter <AwardItem>{


    private ArrayList<AwardItem> awards = new ArrayList<AwardItem>();

    public AwardsAdapter(Context context, int textViewResourceId, ArrayList<AwardItem> awards) {
        super(context, textViewResourceId,awards);
        this.awards = awards;
        // TODO Auto-generated constructor stub
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)AwardsActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.awards_row, parent, false);
        }
        AwardItem award = awards.get(position);
        ProgressBar pb = (ProgressBar) v.findViewById(R.id.pgbAwardProgress);

        if(award.award_type == PROGRESSIVE) {
            pb.setVisibility(View.VISIBLE);
            pb.setMax(award.requirement);
            pb.setProgress(award.current_amount);
            pb.setIndeterminate(false);
            TextView progressText = ((TextView) v.findViewById(R.id.pgbAwardProgressText));
            progressText.setVisibility(View.VISIBLE);
            progressText.setText(award.current_amount+"/"+award.requirement);
        } else {
            pb.setVisibility(View.GONE);
        }
        ((TextView) v.findViewById(R.id.tvwShortMessage)).
                      setText(MessageBuilder.buildMessage(AwardsActivity.this, award.award_text, 
                              Integer.valueOf(award.requirement).toString()));

        return v;
    }

}
Gilkey answered 22/11, 2011 at 19:22 Comment(0)
O
5

New NumberProgressBar library:

Number progress bar

Have a look at this awesome library NumberProgressBar in Github with many more color styles.

enter image description here

Credits: Mr.Daimajia,

According to him The NumberProgressBar is a bar, slim and sexy (every man wants! ).

Oballa answered 10/6, 2014 at 4:2 Comment(2)
Hey, I'm the author of this library. If any problem, feel free to contact me.Poteen
I was inspired with NumberProgressBar so I made something similar with custom circular and line progress bar. Check it out if you want.. Android-PercentProgressBarFigge
H
3

ProgressBar is deisigned to show the progress. you will find a good tutorial here to show the progress accordingly. It is recommended to use AsyncTask for Progress update, because it is painless threading.

Basic AsyncTask with a progress bar widget

Heavyweight answered 10/11, 2011 at 13:44 Comment(5)
I have no problem adding the numbers myself. I was just wondering if there is a way for the ProgressBar to show then itself.Trenna
@Trenna I didn't get you, what do you wanna do.Heavyweight
Aded clarification - hope that helps.Trenna
The thing is that that link is talking about changing how it is displayed. In my case the numbers are displayed at all. The progress dialog has its max and current value set once at the start of the code. They don't change after that. The numbers simply aren't there.Trenna
could you upload a screen shot plz.Heavyweight
G
3

Why dont you just add the text manually, it seems like a much simple approach, I placed it where ever I wanted here, but you can just change the layout to what your need are.

Sorry if there are any mistakes in the code, I didnt run this.

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/tvwShortMessage"
android:typeface="sans"
android:textSize="14sp"
android:textStyle="italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwLongMessage"
android:typeface="sans"
android:textSize="14sp"
android:textStyle="italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvwShortMessage"/>
<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"    
android:layout_marginBottom="15dp" 
android:layout_below="@id/tvwLongMessage">
<ProgressBar
android:id="@+id/pgbAwardProgress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"    
style="?android:attr/progressBarStyleHorizontal"
android:layout_marginRight="5dp" 
android:layout_marginTop="5dp" 
android:layout_marginBottom="15dp" 
android:layout_alignParentTop="true"/>
</LinearLayout>
<TextView android:id="@+id/pgbAwardProgressText"
android:typeface="sans"
android:textSize="14sp"
android:textStyle="italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pgbAwardProgress"
android:layout_alignParentRight="true"
android:visibility="gone"/>
</RelativeLayout>

Class:

private class AwardsAdapter extends ArrayAdapter <AwardItem>{


    private ArrayList<AwardItem> awards = new ArrayList<AwardItem>();

    public AwardsAdapter(Context context, int textViewResourceId, ArrayList<AwardItem> awards) {
        super(context, textViewResourceId,awards);
        this.awards = awards;
        // TODO Auto-generated constructor stub
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)AwardsActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.awards_row, parent, false);
        }
        AwardItem award = awards.get(position);
        ProgressBar pb = (ProgressBar) v.findViewById(R.id.pgbAwardProgress);

        if(award.award_type == PROGRESSIVE) {
            pb.setVisibility(View.VISIBLE);
            pb.setMax(award.requirement);
            pb.setProgress(award.current_amount);
            pb.setIndeterminate(false);
            TextView progressText = ((TextView) v.findViewById(R.id.pgbAwardProgressText));
            progressText.setVisibility(View.VISIBLE);
            progressText.setText(award.current_amount+"/"+award.requirement);
        } else {
            pb.setVisibility(View.GONE);
        }
        ((TextView) v.findViewById(R.id.tvwShortMessage)).
                      setText(MessageBuilder.buildMessage(AwardsActivity.this, award.award_text, 
                              Integer.valueOf(award.requirement).toString()));

        return v;
    }

}
Gilkey answered 22/11, 2011 at 19:22 Comment(0)
D
0

I don't think you want to use a progressbar. It looks like a SeekBar with some numbers at either end of it.

<LinearLayout
    android:id="@+id/LinearLayout_Lose250HandsSlider"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/TextView_MinValue"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="0"/>
    <SeekBar
        android:id="@+id/SeekBar_Lose250HandsSlider"
        android:max="250"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:layout_weight="1.0"/>
    <TextView
        android:id="@+id/TextView_CurrentValue"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="250"/>
</LinearLayout>
Defer answered 22/11, 2011 at 18:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.