Search code examples
androidandroid-drawablexml-drawable

change drawable shape parameter programmatically


i a m looking to make a ring loading effect and to do that i have to change the dashWidth value.

I want to do that programmatically.

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

  <stroke
    android:width="3dp"
    android:color="#8FFFFFFF" 
    android:dashWidth="304dp"
    android:dashGap="500dp"/>

  <size 
    android:width="100dp"
    android:height="100dp"/>
</shape>

Solution

  • As psink sad it's possible to access the dashGap and dashWidth attribute from the stroke. I just used the setStroke method on the background object after casting it to a GradientDrawable

    GradientDrawable background = (GradientDrawable)backgroundContainer.getBackground();
    background.setStroke(3, Color.WHITE, 50, 500);