Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom progress drawable not working on Android Lollipop (API 21) devices

I have a progress drawable which does not work properly on devices running Android Lollipop.

Screenshot on M enter image description here

Screenshot on Lollipop enter image description here

circle_percentage_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape android:shape="oval">
      <solid android:color="@color/colorTranslucentBlack"/>
    </shape>
  </item>
  <item android:id="@android:id/progress">
    <rotate
        android:fromDegrees="270"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="270">
      <shape
          android:innerRadiusRatio="2.5"
          android:shape="ring"
          android:thicknessRatio="25.0">
        <gradient
            android:centerColor="@android:color/holo_red_dark"
            android:endColor="@android:color/holo_red_dark"
            android:startColor="@android:color/holo_red_dark"
            android:type="sweep"/>
      </shape>
    </rotate>
  </item>
  <item android:id="@android:id/secondaryProgress">
    <rotate
        android:fromDegrees="270"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="270">
      <shape
          android:innerRadiusRatio="2.5"
          android:shape="ring"
          android:thicknessRatio="25.0">
        <gradient
            android:centerColor="@android:color/holo_red_dark"
            android:endColor="@android:color/holo_red_dark"
            android:startColor="@android:color/holo_red_dark"
            android:type="sweep"/>
      </shape>
    </rotate>
  </item>

</layer-list>

This drawable is used as a background to ProgressView like this:

<ProgressBar
      android:id="@+id/circle_progress"
      style="?android:attr/progressBarStyleHorizontal"
      android:layout_width="70dp"
      android:layout_height="70dp"
      android:gravity="center"
      android:progress="65"
      android:indeterminate="false"
      android:progressDrawable="@drawable/circle_percentage_drawable"
      />

The renders as a circle drawn to 65% show up on devices running Android M, KitKat, Jellybean. However, if the same code is run on Android Lollipop (API 21) the circle shows as 100%.

Full source code available here: https://github.com/slashrootv200/CircleProgressPercentage

like image 392
Pratap Patil Avatar asked Mar 24 '16 06:03

Pratap Patil


People also ask

What's new in Android Lollipop?

Welcome to Android 5.0 Lollipop—the largest and most ambitious release for Android yet! This release is packed with new features for users and thousands of new APIs for developers. It extends Android even further, from phones, tablets, and wearables, to TVs and cars.

What is a drawable in Java?

A Drawable is a general abstraction for "something that can be drawn.". Most often you will deal with Drawable as the type of resource retrieved for drawing things to the screen; the Drawable class provides a generic API for dealing with an underlying visual resource that may take a variety of forms.

What's new in the accessibility APIs?

New accessibility APIs can retrieve detailed information about the properties of windows on the screen that sighted users can interact with and define standard or customized input actions for UI elements. New Input method editor (IME) APIs enable faster switching to other IMEs directly from the input method.


1 Answers

Add android:useLevel=true" in your circular progressbar xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>

    <shape android:shape="oval"
           android:useLevel="true">
      <solid android:color="@color/colorTranslucentBlack"/>
    </shape>

  </item>

  <item android:id="@android:id/progress">

    <rotate
        android:fromDegrees="270"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="270">
      <shape
          android:innerRadiusRatio="2.5"
          android:shape="ring"
          android:thicknessRatio="25.0"
          android:useLevel="true">
        <gradient
            android:centerColor="@android:color/holo_red_dark"
            android:endColor="@android:color/holo_red_dark"
            android:startColor="@android:color/holo_red_dark"
            android:type="sweep"/>
      </shape>
    </rotate>

  </item>

  <item android:id="@android:id/secondaryProgress">

    <rotate
        android:fromDegrees="270"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="270">
      <shape
          android:innerRadiusRatio="2.5"
          android:shape="ring"
          android:thicknessRatio="25.0"
          android:useLevel="true">
        <gradient
            android:centerColor="@android:color/holo_red_dark"
            android:endColor="@android:color/holo_red_dark"
            android:startColor="@android:color/holo_red_dark"
            android:type="sweep"/>
      </shape>
    </rotate>

  </item>

</layer-list>
like image 178
Ahsan Kamal Avatar answered Nov 14 '22 22:11

Ahsan Kamal