Question

I have a textview and a nine-patch image. Assigning this image in the xml editor to the tag "background" works as desired. Now I want to set this image dynamically in the coding, I thought

myTextView.setBackgroundDrawable(mydrawable);

would be the right thing to do. My png is in res/drawable. The API says that mydrawable needs to be an object of type drawable

 R.drawable.myninepatch

is of type int. So my problem is that I have to convert from that int to a drawable somehow. Then I thought I can use a constructor

NinePatchDrawable mydrawable = new NinePatchDrawable();

But I am again not able to construct such an object. Anyone an idea?

Thanks.

Was it helpful?

Solution

Use setBackgroundResource(), not setBackgroundDrawable.

OTHER TIPS

I had the problem, and I've used your answer but didn't work. I solved it using the draw 9 patch padding pixels.

Use setBackgroundResource(R.drawable.image)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top