How to change the stroke width of a form 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?

+7
java android shape
source share
2 answers

You may need to create it programmatically

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

you need to set the properties after this (indentation, color, etc.) and then change its course

 circle.getPaint().setStrokeWidth(12); 

then set it as background for presentation

 textview.setBackgroundDrawable(circle); 
+7
source share

Do the following:

1) Get a TextView using regular findViewById() :

 TextView textView = (TextView) rootView.findViewById(R.id.resourceName); 

2) Get Drawable from TextView using getBackground() and drop it onto GradientDrawable :

 GradientDrawable backgroundGradient = (GradientDrawable) textView.getBackground(); 

3) Apply it using the setStroke() method (pass its width in pixels and color):

 backgroundGradient.setStroke(5, Color.BLACK); 

All code:

 TextView textView = (TextView) rootView.findViewById(R.id.resourceName); GradientDrawable backgroundGradient = (GradientDrawable) textView.getBackground(); backgroundGradient.setStroke(5, Color.BLACK); 
+9
source share

All Articles