Search code examples
androidbackgroundandroid-imageviewnine-patch

ImageView src vs background


I an trying to use Imageview to overlay a bitmap over a layout.
From what I can see.

  • if I use the bitmap (which is a 9-patch) as android:src it does not get streched to cover the layout
  • if I use it as android:background it gets streched

My dilemma is that I want to use a bunch of 9-patchs in a level-list resource and select the right one using setImageLevel().

  • setImageLevel only works for android:src and not for android:background

How do I get the 9-patch also streched but also selectable from level-list?


Solution

  • You can use your level-list drawable. That drawable will be the background for the ImageView, to set the level to the desired value(and to make the proper drawable be selected) you'll use:

    LevelListDrawable background = (LevelListDrawable) findViewById(R.id.image).getBackground();
    background.setLevel(1);