Reputation: 81
I try change color "stroke", through "GradientDrawable" but not work.
also, I don't know how get id stroke, and change only stroke (I see google, all example are failed)
My XML item
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/selectable_kachel_shape" >
<shape
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="2"
android:useLevel="false" >
<stroke android:width="11dp" android:color="#ff00ffff"/>
<gradient
android:centerColor="#FFFFFF"
android:endColor="#FFFFFF"
android:startColor="#FFFFFF"
android:type="sweep" />
</shape>
</item>
</layer-list>
GradientDrawable gd = (GradientDrawable) circleProgress.getBackground();
gd = (GradientDrawable) circleProgress.getBackground();
gd.setColor(Color.RED);
android.graphics.drawable.LayerDrawable cannot be cast to android.graphics.drawable.GradientDrawable
Here Works, but ... not get my item
GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.RED);
gd.setCornerRadius(10);
gd.setStroke(2, Color.WHITE);
circleProgress.setBackgroundDrawable(gd);
UPDATE here Works, BUT NOT CHANGE MY STROKE (I TRIED WITH ID BUT NOT WORK) ONLY WORK WITH ID ITEM.
GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.selectable_kachel_shape));
shape.setColor(this.getResources().getColor(android.R.color.background_dark));
circleProgress.setBackgroundDrawable(shape);
Upvotes: 7
Views: 17369
Reputation: 6053
Instead of this you may be use any layout and apply your xml as selector and for that layout you apply dynamic backgrounds via GradientDrawable
.
For example, main.xml:
<RelativeLayout
android:orientation="vertical"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shapes"/>
Then your xml, shapes.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/selectable_kachel_shape">
<shape android:innerRadius="0dp" android:shape="ring" android:thicknessRatio="2" android:useLevel="false">
<stroke android:width="11dp" android:color="#ff00ffff" />
<gradient android:centerColor="#FFFFFF" android:endColor="#FFFFFF" android:startColor="#FFFFFF" android:type="sweep" />
</shape>
</item>
</layer-list>
And write following code in your activity.
RelativeLayout mainLayout= (RelativeLayout ) findViewById(R.id.mainLayout);
GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.RED);
gd.setCornerRadius(10);
gd.setStroke(2, Color.WHITE);
mainLayout.setBackgroundDrawable(gd);
For more GradientDrawable
use the link: http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html.
Upvotes: 15