Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the stroke width of a shape programmatically in Android?

This is circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="oval">
    <solid android:color="#00000000"/> 
    <padding android:left="30dp" android:top="30dp"
             android:right="30dp" android:bottom="30dp" />
    <stroke android:color="#439CC8" android:width="7dp" />
</shape>

This is my code:

textview.setBackgroundResource(R.drawable.circle);

I want to change the stroke thickness in my java code. How can I change it programmatically?

like image 387
Taha Körkem Avatar asked Dec 15 '14 20:12

Taha Körkem


People also ask

How do I change the stroke shape on my Android?

To set the shape stroke color programmatically this example uses GradientDrawable method setStroke(int width, int color) which sets the stroke width and change shape's color. Here default orange color of the shape drawable is changes programetically usingb GradientDrawable method setStroke(int width, int color).

What is layerlist in android?

Layer list. A LayerDrawable is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top. Each drawable is represented by an <item> element inside a single <layer-list> element.

What is the stroke width?

The stroke-width attribute is a presentation attribute defining the width of the stroke to be applied to the shape. You can use this attribute with the following SVG elements: <altGlyph> <circle>


1 Answers

You may need to create it programmatically

ShapeDrawable circle = new ShapeDrawable( new  OvalShape() );

you need to set the properties after this, ( padding, color, etc) then change its stroke

circle.getPaint().setStrokeWidth(12);

then set it as the background for the view

textview.setBackgroundDrawable(circle);
like image 183
browep Avatar answered Oct 14 '22 14:10

browep