mardi 1 décembre 2015

Android how to set Random GradientDrawable Color programmatically

I have a problem with ProgressBar. When i press the button "Start" Random function choose one color from array, but only the first color is set as "background", when i click one more time new color is choose but not set as background. When i exit from app and start it again new color is choose but now only one. I would like to have different background color all the time when i press the button.

activity_main.xml

<ProgressBar
            android:id="@+id/circularProgressbar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:indeterminate="false"
            android:max="100"
            android:progress="100"
            android:progressDrawable="@drawable/circular"
            android:secondaryProgress="100" />

circular.xml

<?xml version="1.0" encoding="utf-8"?>

 <item  android:id="@+id/SecondaryProgress">  
    <shape
          android:innerRadiusRatio="5.2"
          android:shape="ring"
          android:useLevel="true"
          android:thicknessRatio="9.5">


    </shape>
</item>

MainActivity.java




public void Start(){
Random random = new Random();
String[] hex_colors = {"F8ED31", "000000", "F7931D", "ED1C24", "7A1777", "ED135A", "71BF43"}; int random_hex = random.nextInt(hex_colors.length); String color="#"+hex_colors[random_hex]; layers = (LayerDrawable) ContextCompat.getDrawable(this,R.drawable.circular); shape = ( GradientDrawable) layers.findDrawableByLayerId(R.id.SecondaryProgress); shape.setColor(Color.parseColor(color)); }



Aucun commentaire:

Enregistrer un commentaire