0

The following xml code works for setting the width and height of image drawables inside items of a layer-list for API 23+:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item><shape android:shape="rectangle"><solid android:color="@color/white"/></shape></item>
    <item android:drawable="@drawable/my_drawable" android:gravity="center" android:bottom="100dp"
        android:width="100dp" android:height="100dp" />
    <item android:drawable="@drawable/drawable_text" android:gravity="center" android:top="50dp"
        android:width="200dp" android:height="200dp" />
</layer-list>

However, the android:width and android:height inside <item> only works for API levels from 23 and up. Without the explicit width and heigth values the drawables will overflow the screen, and if I use resized PNG images, the quality becomes poor.

I have searched about workarounds on Stackoverflow (here, here, and here) but none of the solutions seem to work.

  1. Adding android:top, android:bottom, android:left, and android:right don't resize/set the height and width of the drawable, possibly because they are PNG images and not vector drawables.
  2. Adding a drawable to a shape (and then use the size tag) doesn't work.
Erlend K.H.
  • 448
  • 6
  • 21

1 Answers1

0

There is no way to achieve your goal. You have to resize your image. Or you can use android:width and android:height for API level >=23, and layer-list with resized image for API level < 23.

Also, you can't use android:top, android:bottom etc. properties to resize any images (not png nor vector drawable), those properties are used for positioning.

Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39
Garnik
  • 145
  • 6
  • Hello Garnik, yes, I have already done that: I use android:width and android:height for API levels 23+, and below 23 I use resized images. Thank you for the confirmation. – Erlend K.H. Mar 20 '20 at 15:33