Ilias
Ilias

Reputation: 210

How do I change the progress value of a progress bar programmatically?

I'm trying to create a custom view of some components because I need this combination a lot in my application. Everything in the code works except the progress of my progressBar. I think that it doesn't get the value correctly of the XML file.

In my init void I call the setProgressBar(progress) to provide the current value to my xml file. When I enter for example 'setProgressBar(88)' it works perfectly but not with a value that it has to find in the xml file.

CustomProgressbar.java

public class CustomProgressbar extends RelativeLayout {
    @StyleableRes
    int index0 = 0;
    @StyleableRes
    int index1 = 1;
    @StyleableRes
    int index2 = 2;

    TextView titleText;
    TextView valueText;
    ProgressBar progressBar;

    public CustomProgressbar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        inflate(context, R.layout.custom_progressbar_layout, this);

        int[] sets = {R.attr.title, R.attr.value, R.attr.progress};

        TypedArray typedArray = context.obtainStyledAttributes(attrs, sets);

        CharSequence title = typedArray.getText(index0);
        CharSequence value = typedArray.getText(index1);
        int progress = typedArray.getInteger(index2,0);

        typedArray.recycle();

        titleText = findViewById(R.id.title_text);
        valueText = findViewById(R.id.value_text);
        progressBar = findViewById(R.id.progressbar_attr);

        setTitleText(title);
        setValueText(value);
        setProgressBar(progress);

    }

    public CharSequence getTitleText() {
        return titleText.getText();
    }
    public CharSequence getValueText() {
        return valueText.getText();
    }
    public int getProgressBar() {
        return progressBar.getProgress();
    }

    public void setTitleText(CharSequence value) {
        titleText.setText(value);
    }
    public void setValueText(CharSequence value) {
        valueText.setText(value);
    }
    public void setProgressBar(int value) {
        progressBar.setProgress(value);
    }
}

custom_progressbar_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

        <TextView
            android:id="@+id/title_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textColor="@android:color/black" />

        <TextView
            android:id="@+id/value_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textColor="@android:color/black" />

        <ProgressBar
            android:id="@+id/progressbar_attr"
            style="@android:style/Widget.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:layout_below="@id/value_text"
            android:layout_alignParentStart="true"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="10dp"
            android:max="100"
            android:maxHeight="10dip"
            android:minHeight="10dip"
            android:progressTint="@android:color/holo_green_dark"
            android:progressDrawable="@drawable/progressbars" />
</RelativeLayout>

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainClass"
    android:background="@android:color/background_dark">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        app:cardCornerRadius="8dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="15dp">

            <com.x.customprogressbar.CustomProgressbar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:title="ThisIsTheTitle"
                app:value="€ 28,30"
                app:progress="77" />
        </LinearLayout>

    </android.support.v7.widget.CardView>
</LinearLayout>

And my attributes

<resources>
    <declare-styleable name="CustomProgressbar">
        <attr name="title" format="string"/>
        <attr name="value" format="string"/>
        <attr name="progress" format="integer"/>
    </declare-styleable>
</resources>

Current results:

(I'm not allowed yet to add images so I got links.)

Upvotes: 1

Views: 4534

Answers (1)

user10539074
user10539074

Reputation:

try this, styles attributes are getting in the other way

public class CustomProgressbar extends RelativeLayout {

private TextView titleText;
private TextView valueText;
private ProgressBar progressBar;

public CustomProgressbar(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context, attrs);
}

private void init(Context context, AttributeSet attrs) {
    inflate(context, R.layout.custom_progressbar_layout, this);

    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomProgressbar);

    CharSequence title = typedArray.getText(R.styleable.CustomProgressbar_title);
    CharSequence value = typedArray.getText(R.styleable.CustomProgressbar_value);
    int progress = typedArray.getInteger(R.styleable.CustomProgressbar_progress, 0);

    typedArray.recycle();

    titleText = findViewById(R.id.title_text);
    valueText = findViewById(R.id.value_text);
    progressBar = findViewById(R.id.progressbar_attr);

    setTitleText(title);
    setValueText(value);
    setProgressBar(progress);

}

public CharSequence getTitleText() {
    return titleText.getText();
}

public CharSequence getValueText() {
    return valueText.getText();
}

public int getProgressBar() {
    return progressBar.getProgress();
}

public void setTitleText(CharSequence value) {
    titleText.setText(value);
}

public void setValueText(CharSequence value) {
    valueText.setText(value);
}

public void setProgressBar(int value) {
    progressBar.setProgress(value);
}

}

Upvotes: 1

Related Questions