92

In my XML file, I'm using bitmap as the following

<bitmap
    android:src="@drawable/Icon"
    android:gravity="center"/>

Here the image width of the icon exceeds the screen.

I tried android:width="100dp" and all. But it isn't working.

Complete XML file:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
      <shape android:shape="rectangle">
            <stroke android:width="3dp" android:color="@color/black" />
            <solid android:color="@color/bg_green" />
        </shape>
   </item>
   <item>
    <bitmap
        android:src="@drawable/Icon"
        android:gravity="center"/>
    </item>
</layer-list>

How can I reduce the width and height of the above bitmap?

wonea
  • 4,783
  • 17
  • 86
  • 139
Erma Isabel
  • 2,167
  • 8
  • 34
  • 64
  • Is it a hard requirement that you use an XML resource? It seems far easier to create a scaled bitmap (through code) of the required size and use that. – Abandoned Cart Jun 02 '19 at 18:09

10 Answers10

104

Only API 23 or later

Just use

<item
    android:width="200dp"
    android:height="200dp"
    android:drawable="@drawable/splash_background"
    android:gravity="center" />

instead

<item
    android:left="200dp"
    android:right="200dp">

    <bitmap
        android:src="@drawable/splash_background"
        android:gravity="center" />

</item>
Mustafa Ehsan Alokozay
  • 5,583
  • 2
  • 24
  • 30
MaxF
  • 2,123
  • 1
  • 14
  • 17
69

Although there is no width and height parameters for bitmap, you can set bitmap size using gravity and item width/height.

<item
    android:width="230dp"
    android:height="70dp">
    <bitmap
        android:gravity="fill_horizontal|fill_vertical"
        android:src="@drawable/screen" />
</item>

You can set item width and height and scale bitmap inside of it using gravity.

I know this is old question but maybe someone will find it useful.

WARNING:

As already mentioned in comments, this method ONLY works with API level 23 (Android 6.0) and up.

Nicolo
  • 1,600
  • 2
  • 16
  • 18
33

If you want to reduce the width and height so it fits on the screen, and you don't need to set exact dimensions, you can simply set left and right on the parent item. That will essentially add a margin around the image so it takes up less space:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:left="20dp"
        android:right="20dp">

        <bitmap
            android:src="@drawable/Icon"
            android:gravity="center" />

    </item>

</layer-list>

Ideally, though, you should upload different size images for each screen resolution (e.g. hdpi, xxhdpi) and then you won't need to programmatically set the size at all. You can see the full list here.

Kevin Cooper
  • 5,018
  • 4
  • 37
  • 51
  • 11
    Thanks for this suggestion. It seems crazy that you can't just set the size of the image when laying out a splash screen. – ahalls Oct 28 '16 at 17:41
  • 2
    @ahalls you **can** set the size if you inflate a layout, but for performance reasons splash screens generally use drawables instead of layouts, which don't allow setting the size :( – Kevin Cooper Oct 28 '16 at 19:43
  • 6
    I end up with a stretched image when using this method, or, if i supply `android:gravity="center"`, the image still goes off the screen regardless of the margins. – Nathan F. May 31 '19 at 20:20
28

this one I use for resize my mipmap on to my launch screen.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
    <item android:drawable="@color/colorPrimary"/>
    <item
        android:width="200dp"
        android:height="200dp"
        android:gravity="center">
        <bitmap
            android:gravity="fill"
            android:src="@mipmap/ic_launcher"
            android:mipMap="true"/>
    </item>
</layer-list>
Gilbert Arafat
  • 441
  • 5
  • 10
20

Try to do it this way, this worked for me:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

  <item
    android:width="30dp"
    android:height="30dp"
    android:gravity="center"
  >
        <bitmap
       android:src="@drawable/screen"/>
  </item>

</layer-list>
chandsie
  • 2,865
  • 3
  • 27
  • 37
8

From Android Developers I saw that there is no tag for Bitmap XML that allow you to change it size. You need to do this via some Graphic Applications like GIMP. As @Kevin Cooper told already, ideally you need to create different sets of resources (with different sizes) for different screen types.

But still you can resize it using width and height attributes in ImageView, instead of creating bitmap with different size:

<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:scaleType="center"
    android:src="@drawable/Icon" />

If you want to reuse this image in other places, you can create separate style and reuse it:

// in styles.xml
<style name="Icon24x24">
    <item name="android:src">@drawable/Icon</item>
    <item name="android:gravity">center</item>
    <item name="android:layout_width">24dp</item>
    <item name="android:layout_height">24dp</item>
</style>

// in layout.xml
<ImageView style="@style/Icon24x24"/>

Another way of reusing, may be creating of separate layout file for one ImageView and then including in to other layouts.

If you want to use this bitmap inside of layer list, please see @Kevin Cooper answer, as he suggested it is possible to add paddings to layer list item:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:left="20dp"
        android:right="20dp">
        <bitmap
            android:src="@drawable/Icon"
            android:gravity="center" />
    </item>
</layer-list>

And as @Nicolo also pointed, form API 23, it is possible to set android:width and android:height" to item.

Yakiv Mospan
  • 8,174
  • 3
  • 31
  • 35
  • No, its a View. I've just saw your update. Maybe you can simply change that image size with some Graphic Applications like Pain NET or something else – Yakiv Mospan Apr 15 '14 at 09:24
  • 27
    Does not work in layer list, for example: in android:drawableLeft property for custom background drawable size in xml. The question is resize bitmap in layer list, the response is incorrect. – e-info128 Apr 23 '16 at 18:47
  • 4
    incorrect answer to the question, no ImageView tag is supported inside drawable resource xml – Umar Ata Oct 20 '16 at 06:53
  • @e-info128 Seems my answer was not clear enough, when I told to use "ImageView instead" I've mean to use simple view with simple image resource (.png or .jpg) instead of trying to create xml image resource. – Yakiv Mospan Apr 25 '17 at 08:51
  • @UmarAta I've updated my answer, please check it out and respond to me if it is still not clear enough. – Yakiv Mospan Apr 25 '17 at 09:08
  • @YakivMospan This is a really irregular way to handle the issue. Even after all of the updates, it seems counterproductive to make a collection of xml files as a workaround for scaling your image. – Abandoned Cart Jun 02 '19 at 18:11
7

so i fixed that by typing:

<item
  android:height= "250dp"
  android:width= "250dp"
  android:gravity="center">
   <bitmap
    android:gravity="fill_horizontal|fill_vertical"
    android:src="@mipmap/ic_launcher"/>
</item>
Marcelo Cesar
  • 89
  • 1
  • 4
5

Complete Solution just copy paste and change the Image Path

  <?xml version="1.0" encoding="utf-8"?>

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">

 <item
    android:gravity="center"
    android:width="286dp"
    android:height="176dp">

    <bitmap
        android:src="@drawable/logo"
        android:gravity="fill_horizontal|fill_vertical" />
   </item>
 </layer-list>
Arun R. Prajapati
  • 2,654
  • 1
  • 14
  • 14
3

I just resized the image manually then it works for me

amorenew
  • 10,760
  • 10
  • 47
  • 69
2

1.Step 1: Add android:background= to the Spinner/AppCompatSpinner

 <androidx.appcompat.widget.AppCompatSpinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="@dimen/input_height"
            android:background="@drawable/shape_drop_down_normal"
            />
  1. Create a shape_drop_down_normal.xml in /drawable

2A) If you have a ic_spinner_dropdown.png drawable resource for the Spinner bitmap

shape_drop_down_normal.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:width="20dp"
        android:height="13dp"
        android:gravity="end|center_vertical">
        <bitmap
            android:src="@drawable/ic_spinner_dropdown"
            android:gravity="fill"
            android:mipMap="true"/>
    </item>
</layer-list>

2B) If you have a ic_spinner_dropdown_vector.xml vector drawable resource for the Spinner bitmap shape_drop_down_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:width="20dp"
        android:height="13dp"
        android:gravity="end|center_vertical"
        android:drawable="@drawable/ic_spinner_dropdown_vector">
    </item>
</layer-list>
  1. Warning!

    android:width="20dp" android:height="13dp"

in layer-list require API 23+, use

android:left="20dp"
android:right="13dp" 

for lower API levels, or simply omit the width and height constraints, and set the dp directly in vector drawable (it works perfectly for all API levels).

Alex Burov
  • 1,881
  • 1
  • 17
  • 13