Ed Lee
Ed Lee

Reputation: 187

In Android, is it possible to get the stroke width of a gradient drawable without reflection?

I was looking through the Android source code and it seems like it's not possible to get the stroke width of a GradientDrawable without getting it via reflection. The reason I'm trying to get the stroke width is because I wrote the GradientDrawable in xml and am trying to dynamically update it.

Upvotes: 2

Views: 1516

Answers (2)

Bea
Bea

Reputation: 158

You can dynamically update the stroke of a GradientDrawable with something like:

((GradientDrawable) mView.getBackground().mutate()).setStroke(width, color);

as explained in How to change stroke color dynamically?

There are more options to change the stroke, explained around https://developer.android.com/reference/android/graphics/drawable/GradientDrawable#setStroke(int,%20int)

I'm still looking for a way of getting the stroke width.

(I know this was asked over 3 years ago, but someone might still read it now)

Upvotes: 0

Ed Lee
Ed Lee

Reputation: 187

If it were a ShapeDrawable, the stroke width would be available via the ShapeDrawable's paint:

((ShapeDrawable) view.getBackground()).getPaint().getStrokeWidth();

Upvotes: 1

Related Questions