-1

I have the following Layout

            <Button
                android:layout_width="fill_parent"
                android:layout_height="75dp"
                android:drawableLeft="@drawable/binocular"
                android:gravity="left|center_vertical"
                android:text="search" />

and it looks like :

enter image description here

The picture is too large. I simply want to align the image to the left with the corredct size, so it fits with the height of the button.

I know I could adjust the bound of the image by code. But I would like to know, if there is a way to do with layout specification.

There is a solution mentoined : how to change a drawableLeft icon size on a button? (accepted answer)

I guess this is the correct one, but I do not understand , where to put the layer-list and how to reference it in the button.

Can someone help ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
mcfly soft
  • 11,289
  • 26
  • 98
  • 202

3 Answers3

1

You have to create new drawable xml file and there put layer-list code. For example button_background.xml.

Then set on button this drawable

android:drawableLeft="@drawable/button_background"
mac229
  • 4,319
  • 5
  • 18
  • 24
  • Wow. Thats it. Thanks a lot. Someone downvoted yours, I upvoted your correct answer. Have to wait 10 minutes to accept. Thanks – mcfly soft Dec 05 '17 at 13:50
  • @mcflysoft If it is correct answer you can set as corrected with check mark, to close question. Happy coding :) – mac229 Dec 05 '17 at 13:53
1

Set the scaleType attribute on the ImageButton

<ImageButton android:id="@+id/iconBtn"
  android:layout_width="64dip"
  android:layout_height="64dip"
  android:src="@drawable/binocular"
  android:scaleType="fitCenter"
  android:background="#00000000"/>
Jon
  • 9,156
  • 9
  • 56
  • 73
Nirav Joshi
  • 1,713
  • 15
  • 28
0

use ImageButton with these properties

<ImageButton
        android:id="@+id/button_topleft"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="0dp"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:padding="20dp"
        android:adjustViewBounds="true"
        android:scaleType="centerInside"        
 />

padding plays very important role in sizing the image after scaleType fails.

hope this will help you.

Nouman Ch
  • 4,023
  • 4
  • 29
  • 42