Nik
Nik

Reputation: 193

How to change the progressBar color and height in Android

I am using the below xml layout:

<ProgressBar
    android:id="@+id/totalScoreProgress"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:layout_marginTop="29dp"
    android:maxHeight="30dp"
    android:minHeight="30dp"
    android:indeterminateTint="@color/grey"
    android:max="30"/>

<TextView
    android:id="@+id/bestResultText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/totalScoreProgress"
    android:text="Best Result : 0"
    android:textSize="15sp" />

and java code:

ProgressBar totalScoreProgress = (ProgressBar) findViewById(R.id.totalScoreProgress);
totalScoreProgress.setProgress(30);

These are my efforts for the grey progress bar with text, I am attaching the image of the expected and actual output. Please guide me where I am making a mistake.

Thanks.

Upvotes: 0

Views: 5940

Answers (4)

Nik
Nik

Reputation: 193

The exact solution to the problem is:

  1. Define a custom progress_bar.xml for the progress bar in xml layout
  2. In java code, apply bringToFront() to bring bestScoreText above totalScoreProgress ProgressBar

Xml Layout

<ProgressBar
    android:id="@+id/totalScoreProgress"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:theme="@style/CustomProgressBarHorizontal"
    android:progressDrawable="@drawable/progress_bar"
    android:max="30"/>

<TextView
    android:id="@+id/bestResultText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/totalScoreProgress"
    android:text="Best Result : 0"
    android:textSize="15sp" />

progress_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />

                <solid android:color="@color/grey" />
            </shape>
        </clip>
    </item>
</layer-list>

Java Code

ProgressBar totalScoreProgress = (ProgressBar) findViewById(R.id.totalScoreProgress);
totalScoreProgress.setProgress(30);
TextView bestScoreText = (TextView) findViewById(R.id.bestResultText);
bestScoreText.bringToFront();

Upvotes: 0

Devangi
Devangi

Reputation: 375

You can also use external library for progressBar : https://github.com/lopspower/CircularProgressBar you can manage height and color and more.

Upvotes: 0

AskNilesh
AskNilesh

Reputation: 69689

to change higth of progressbar use below theme in your style

<style name="CustomProgressBarHorizontal" parent="android:Widget.ProgressBar.Horizontal">
  <item name="android:progressDrawable">@drawable/custom_progress_bar_horizontal</item>
  <item name="android:minHeight">10dip</item>
  <item name="android:maxHeight">20dip</item>

to change color of prograss-bar create a drawable progress_bar.xml file

<?xml version="1.0" encoding="utf-8"?>

<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <corners android:radius="5dip" />

            <solid android:color="#f58233" />
        </shape>
    </clip>

    <color android:color="#f58233" />
</item>
<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners android:radius="5dip" />

            <solid android:color="#f58233" />
        </shape>
    </clip>

    <color android:color="#f58233" />
</item>

</layer-list>

apply this both in your progrss-bar like this

<ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="200dp"
        android:layout_height="3dip"
        android:theme="@style/CustomProgressBarHorizontal"
        android:progressDrawable="@drawable/progress_bar" />

Upvotes: 1

A. Shevchuk
A. Shevchuk

Reputation: 2169

you need to specify drawable resource in your xml.

For example progress_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@android:id/background">
        <shape>
            <corners
                android:radius="5dip"/>
            <solid android:color="@color/grey"/>
        </shape>
    </item>
    <item
        android:id="@android:id/progress">
        <clip>
            <shape>
                <corners
                    android:radius="5dip"/>
                <solid android:color="@color/blue"/>
            </shape>
        </clip>
    </item>
</layer-list>

and then use it like:

<ProgressBar
            android:id="@+id/pbLoadingProgress"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="@dimen/your_progress_bar_height"
            android:layout_gravity="bottom"   
            android:progressDrawable="@drawable/progress_background" />

Upvotes: 3

Related Questions