Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw partial circle as progress bar in android studio

SUMMARY :

Use picture 1 for reference.

I need to keep the grey part under the green part, so that when green decreases, grey becomes visible. But I also want the uncovered part (just grey to not be there). Resulting in a 2/3rd overlapping circle.


I am trying to create 2/3rd circle (with rounded corners) as a progress bar in android studio. I am not able to do it. I was hoping to get your help.

Current circle: (PICTURE 1)

enter image description here

Circle I need :

enter image description here

Shape:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="ring" android:innerRadiusRatio="2.5" android:useLevel="false"
            android:thicknessRatio="12">
            <solid android:color="#DDD"/>
     </shape>
    </item>
    <item>
        <shape android:shape="ring" android:innerRadiusRatio="2.5" android:useLevel="true"
            android:thicknessRatio="12">
            <solid android:color="@color/colorAccent"/>
        </shape>
    </item>
</layer-list>

Layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <ProgressBar
        android:id="@+id/timerProgressBar"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="1:1"
        android:indeterminateOnly="false"
        android:progressDrawable="@drawable/circle"
        android:rotation="-90"
        app:layout_constraintBottom_toTopOf="@+id/guidelineBottom"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guidelineTop"
        android:progress="10"/>

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineBottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.1" />

 
</androidx.constraintlayout.widget.ConstraintLayout>
like image 265
Newbie Avatar asked Oct 12 '20 19:10

Newbie


People also ask

How do I make a custom circular progress bar?

This example demonstrates how do I make circle custom progress bar in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 Answers

If you don't want to draw a bar on your own, you can use https://github.com/futuredapp/donut. It is highly customizable so you can achieve the requested behavior easily.

donuts_preview

like image 130
skywall Avatar answered Sep 18 '22 13:09

skywall


You can use the CircularProgressIndicator provided by the Material Components library.

Something like:

        <com.google.android.material.progressindicator.CircularProgressIndicator
            android:id="@+id/progress"
            app:growMode="bidirectional"
            app:trackThickness="8dp"
            app:indicatorColor="@color/green"
            app:trackColor="@color/grey"
            app:indicatorSize="100dp"
            app:trackCornerRadius="8dp"
            android:progress="75"
            android:rotation="225"
            .../>

Use these attributes:

  • trackColor you can change the color of the grey/uncovered part. Use the parent background color to hide it.
  • trackCornerRadius to change the rounded corners of the indicator.

You have to apply a rotation depending by the progress value.

progress.rotation = 180+((1-value/100)/2*360)

enter image description here enter image description here

Note: it requires at least the version 1.3.0-alpha04.

enter image description here

like image 41
Gabriele Mariotti Avatar answered Sep 22 '22 13:09

Gabriele Mariotti