Jan Meyer
Jan Meyer

Reputation: 214

How to Create Custom ProgressBar

Edit:
Question 1) is solved. But I still wasn't able to change to color of the ProgressBar. I tried to use my own theme (see code below).

I want my ProgressBar to look like the image below. I have already created a ProgressBar above my ListView.I’m using a ListView , ProgressBar , and two TextViews in a RelativeLayout.

My Question:

1.) How can I align the TextViews above the ProgressBar in the way shown below?
2.) How can I set the Color of the ProgressBar itself and the Background Color of the ProgressBar?

enter image description here

MainActivity.xml

   <ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/progressBarText"
    android:progress="0"
    android:max="100"
    android:paddingTop="0dp"
    android:paddingBottom="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:theme="@style/progressBarTheme"
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">@android:color/holo_green_dark</item>
</style>

<style name="progressBarTheme" parent="@style/Theme.AppCompat">
    <item name="colorAccent">@color/myRedColor</item>
</style>

</resources>

Upvotes: 3

Views: 606

Answers (3)

Jan Meyer
Jan Meyer

Reputation: 214

This changed the color of the progressbar.

progressBar.getProgressDrawable().setColorFilter(
Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);

Upvotes: 1

Chetan Mahajan
Chetan Mahajan

Reputation: 369

try this:

<android.support.constraint.ConstraintLayout
            android:id="@+id/uplaodFile"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/upload"
            android:layout_marginTop="10dp"
            >

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="46dp"
                android:layout_height="65dp"
                android:src="@drawable/file_image"
                app:layout_constraintEnd_toStartOf="@+id/horizontal_progress_bar"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@+id/notification" />

            <TextView
                android:id="@+id/notification"
                android:layout_width="282dp"
                android:layout_height="22dp"
                android:layout_marginBottom="2dp"
                android:text="File Detail"
                app:layout_constraintBottom_toTopOf="@+id/cancel"
                app:layout_constraintStart_toStartOf="@+id/horizontal_progress_bar"
                app:layout_constraintTop_toTopOf="parent"
                tools:ignore="MissingConstraints" />

            <ProgressBar
                android:id="@+id/horizontal_progress_bar"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="201dp"
                android:layout_height="26dp"
                android:layout_marginEnd="8dp"
                android:layout_marginTop="24dp"
                android:indeterminate="false"
                android:max="100"
                android:minHeight="100dp"
                android:progress="2"
                android:progressBackgroundTint="#aaaaaa"
                app:layout_constraintEnd_toStartOf="@+id/cancel"
                app:layout_constraintTop_toTopOf="parent"
                tools:ignore="MissingConstraints" />

            <TextView
                android:id="@+id/process_state"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="40dp"
                android:text="uploading.. 10%"
                android:textAlignment="center"
                android:textColor="#4B74FF"
                android:textSize="10dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.833"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:ignore="MissingConstraints" />

            <ImageView
                android:id="@+id/cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="36dp"
                android:layout_marginEnd="10dp"
                android:src="@drawable/ic_close_black_24dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                tools:ignore="MissingConstraints" />


        </android.support.constraint.ConstraintLayout>

this is the java code

ProgressBar progressBar;

process_status = findViewById(R.id.process_state);
progressBar = findViewById(R.id.horizontal_progress_bar);
progressBar.setProgressTintList(ColorStateList.valueOf(Color.parseColor("#4B74FF")));
progressBar.setProgress(currentProgress);
textView.setText("Uploading..."+currentProgress+ "%");

Upvotes: 1

ʍѳђઽ૯ท
ʍѳђઽ૯ท

Reputation: 16976

1.) How can I align the TextViews above the ProgressBar in the way shown below?

Try this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">

    <ProgressBar
        android:id="@+id/progressCircle"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

    <TextView
        android:id="@+id/txtvStatusCircle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/progressCircle"
        android:layout_alignParentStart="true"
        android:text="Daily Progress"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/txtPercent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/progressCircle"
        android:layout_alignParentEnd="true"
        android:text="0 %"
        android:textSize="18sp" />

</RelativeLayout>

2.) How can I set the Color of the ProgressBar itself and the Background Color of the ProgressBar?

In your styles.xml:

<style name="progressBarBlue" parent="@style/Theme.AppCompat">
        <item name="colorAccent">@color/myBlueColor</item>
</style>

Then set as the ProgressBar theme in the xml:

<ProgressBar
    ...
    android:theme="@style/progressBarBlue" />

About the ProgressBar itself, what ever you choose for the AccentColor in your styles, will be set for the ProgressBar too. So changing the color of:

<item name="colorAccent">@color/colorAccent/item>

Will do the trick.

Output:

enter image description here

Upvotes: 1

Related Questions