How to create a progress bar with rounded corners in android

In this android programming source code example, we are going to create a progress bar with rounded corners in android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to create a progress bar with rounded corners in android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".progressbar.ProgressBar3">


    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="318dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:progress="10"
        android:progressDrawable="@drawable/progressbar_rounded_corner"/>
</android.support.constraint.ConstraintLayout>

drawable/progressbar_rounded_corner.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="8dp"/>
            <solid android:color="@color/colorWhite"/>
        </shape>
    </item>

    <item android:id="@android:id/progress"
        android:top="1dp"
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp">

        <scale android:scaleWidth="100%">
            <shape>
                <corners android:radius="8dp"/>
                <solid android:color="@color/colorPrimaryDark"/>
            </shape>
        </scale>
    </item>
</layer-list>

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


public class ProgressBar3 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress_bar3);
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment