Search code examples
androidbackgroundtextviewnine-patch

background picture for textview


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.


Solution

  • Use setBackgroundResource(), not setBackgroundDrawable.