Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make round corners for a ring shape in Android drawable

I have a custom progress bar that looks like this:

enter image description here

Here's the .xml code I've used to create it:

background_drawable.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="@dimen/progress_bar_radial_inner_radius"
    android:thickness="@dimen/progress_bar_radial_thickness"
    android:shape="ring"
    android:useLevel="false" >
    <solid android:color="@color/main_color_alpha"/>
</shape>

progress_drawable.xml

<?xml version="1.0" encoding="UTF-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="270"
    android:toDegrees="270">
    <shape
        android:innerRadius="@dimen/progress_bar_radial_inner_radius"
        android:thickness="@dimen/progress_bar_radial_thickness"
        android:shape="ring" >
        <solid android:color="@color/main_color"/>
    </shape>
</rotate>

What I want to get is a round corners for the ring that I use to show progress. Something that would look like this:

enter image description here

Does someone has any idea on how this can be achieved?

like image 706
Yenn Avatar asked Aug 27 '18 14:08

Yenn


People also ask

How do I round corners in XML?

To do that right click on drawable (under resources), choose New, choose Drawable Resource File. Name the resource rounded_blue_border and make the root element shape. We will use the attributes stroke, corners and padding. The stroke determines the width and color of the border.

How do I make drawable circles in Android?

Usage In Activity Layout Now we can use this shape in the same way we use drawables. Let's set it to android:src of the ImageView. The important thing that you have to notice is that i have given 100dp to both layout_width and layout_height because a circle should have same radius around the area.

How to make the corners of a button round in Android?

This example demonstrates how to make the corners of a button round 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. Step 3 − Add the following code to src/MainActivity.java

How to create rounded button in Android using Kotlin?

Step 1. Create a new project “ Build Your First Android App in Kotlin “ Step 2. Create a new XML file and add the below code. Filename: rounded_button.xml Step 3. Add following code in “ activity_main.xml “

How to create a circle image view in Android Studio?

Below are steps on how to do so: Create a new drawable resource file in the drawable directory which defines the shape of image view that is a circle. Here filename is circular.xml Step 2: Next step is to make a layerlist drawable so that it can act as background to your imageview.

How to create a drawable resource file in Android Studio?

You can also refer to this GFG Tutorial for Creating a new android studio project. Step 2: Make sure that you have selected the Android option for project structure on the top left corner of the screen, then go to the res/drawable folder. Step 3: Here right-click on the drawable folder and click on new and select drawable resource file.


1 Answers

I was able to achieve this using a layer list, and adding a dot to each side of the line. The first one will be stuck at the top, while the second one will follow the progress as an inset is added inside a rotate element. You will have to adjust it according to the size of your progressbar layout though. Mine is 250dp x 250dp.

progress_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <rotate android:fromDegrees="270" android:toDegrees="270">
        <shape
            android:innerRadiusRatio="2.55"
            android:shape="ring"
            android:thickness="15dp"
            android:useLevel="true">
            <solid android:color="@color/main_color" />
        </shape>
        </rotate>
    </item>
    <item android:bottom="211dp">
        <shape
            android:innerRadiusRatio="1000"
            android:shape="ring"
            android:thickness="7dp"
            android:useLevel="false">
            <solid android:color="@color/main_color" />
        </shape>
    </item>
    <item>
        <rotate>
            <inset android:insetBottom="211dp">
                <shape
                    android:innerRadiusRatio="1000"
                    android:shape="ring"
                    android:thickness="7dp"
                    android:useLevel="false">
                    <solid android:color="@color/main_color" />
                </shape>
            </inset>
        </rotate>
    </item>
</layer-list>

enter image description hereenter image description here

like image 75
Jon Avatar answered Oct 20 '22 16:10

Jon