23

Is there any way to do this? I have tried padding the image and setting the width/height of the view, but neither seems to work. Here is an example:

<ImageButton
    android:id="@+id/search"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/search_small"
    android:paddingTop="4sp"
    android:paddingBottom="4sp"
    android:paddingLeft="6sp"
    android:paddingRight="6sp"
    android:layout_marginRight="10sp"
    android:layout_marginTop="6sp"
    android:layout_marginBottom="6sp"
    android:layout_alignParentRight="true"
/>

I want the button to be wider than it is tall, but it is coming out the other way round.

Philip Sheard
  • 5,789
  • 5
  • 27
  • 42

6 Answers6

30

Just had a play to try and understand your problem.

Seems ImageButton is a composite view which has a few pre-set values. Such as some sort of margin which you cannot override with the XML. If you cannot change your image to match what you want to happen then you are better to create your own composite view.

Here is my example of a composite view you can make yourself:

<FrameLayout android:layout_width="wrap_content" 
             android:layout_height="wrap_content">          
    <Button android:id="@+id/saveSearchButton"  
            android:layout_width="50dp"         
            android:layout_height="50dp" />
    <ImageView android:layout_width="45dp"  
               android:layout_height="45dp"
               android:scaleType="fitXY"
               android:src="@drawable/ic_menu_save" 
               android:layout_gravity="center"/>
</FrameLayout>

<FrameLayout android:layout_width="wrap_content" 
             android:layout_height="wrap_content">          
    <Button android:id="@+id/clearSearchButton"
            android:layout_width="50dp" 
            android:layout_height="50dp" />
    <ImageView android:layout_width="45dp"
               android:layout_height="45dp"
               android:scaleType="fitXY" 
               android:src="@drawable/ic_menu_close_clear_cancel" 
               android:layout_gravity="center"/>
</FrameLayout>      

And the original buttons:

<ImageButton android:id="@+id/imageButton1"  
             android:src="@drawable/ic_menu_save"               
             android:layout_height="45dp" android:layout_width="45dp"/>

<ImageButton android:id="@+id/imageButton2"
             android:src="@drawable/ic_menu_close_clear_cancel" 
             android:layout_height="45dp"
             android:layout_width="45dp"/>  

Here we can see custom image/button composite followed by the build in ImageButton as part of the SDK:

Example

Community
  • 1
  • 1
Graeme
  • 25,714
  • 24
  • 124
  • 186
22

Set android:background instead of android:src to set the image on the button. This will adjust the image to your button's size. Then adjust the padding after.

C.O
  • 223
  • 2
  • 6
  • 1
    Far simpler and MUCH easier to implement than the accepted answer. _This_ should have been the accepted answer. – kc_dev Aug 16 '21 at 07:23
11

You shouldn't use sp as a size dimension - dp should be used as it will help your view scale directly with different screen density and resolutions. See Here for dimensions.

padding will push other elements away from your view boundary. margin will push the contents of your view inward from the your boundary (ie would squash the available space for your picture) . The boundary is specified by height and width. Without more information I would guess you are being confused by your margins - delete them and experiment.

Also useful to you: android:scaleType="fitXY" makes the image stretch to match both the X and Y dimensions that are available to it. It helps you to see the canvas available to your image. Once you feel the area is large enough for a correctly scaled image change the scale type to centerInside. See Here for all scale types.

Graeme
  • 25,714
  • 24
  • 124
  • 186
  • Also, to "Button is wider than it is tall". This is controlled by your layout_width and layout_height attributes. They control teh width and height of your component. "match_parent" means the button will try to fill as much space as it can in relation to other views on the page. Read over [this section](http://developer.android.com/guide/topics/ui/index.html) to learn about UI design. – Graeme Aug 02 '11 at 09:12
  • 1
    Thank you, but I already know the difference between margin and padding, and between sp and dp. That is not the source of my problem. – Philip Sheard Aug 02 '11 at 12:24
  • Ah - Since your question is such a basic one I assumed you needed some help. Obviously mistaken, my apologies. – Graeme Aug 02 '11 at 14:48
6

I use minWidth and minHeight attributes, combined with a fitXY scale type and wrapping its content to modulate the shape of my button.

<ImageButton
        android:id="@+id/fooButton"
        android:background="@drawable/play_button"
        android:backgroundTint="#00000000"
        android:minWidth="200"
        android:minHeight="100"
        android:scaleType="fitXY"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="playStuff"
        />

enter image description here

Campa
  • 4,267
  • 3
  • 37
  • 42
1

I finished the layout following Graeme's answer. Four "imageButton" fix the bottom, same width, changeable image size. thanks!

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@color/#000"
        android:weightSum="100" >

        <FrameLayout 
            android:id="@+id/flBottom1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="25"
            >
            <Button 
                android:id="@+id/ibBottom1" 
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:background="@drawable/detail_tab_bg_selector"/>
            <ImageView 
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:scaleType="fitXY" 
                android:src="@drawable/icon_home_48_48" 
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="5dp"
                />
            <TextView
                android:id="@+id/tvBottom1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="35dp"
                android:text="@string/bailty_text_home"
                style="@style/bailtyTextBottom"
                />
        </FrameLayout>


        <FrameLayout 
            android:id="@+id/flBottom2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="25"
            >
            <Button 
                android:id="@+id/ibBottom2" 
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:background="@drawable/detail_tab_bg_selector"/>
            <ImageView 
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:scaleType="fitXY" 
                android:src="@drawable/icon_compose_48_48" 
                android:layout_gravity="center_horizontal"
                />
            <TextView
                android:id="@+id/tvBottom2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="35dp"
                android:text="@string/bailty_text_comment"
                style="@style/bailtyTextBottom"
                />
        </FrameLayout>


        <FrameLayout 
            android:id="@+id/flBottom3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="25"
            >
            <Button 
                android:id="@+id/ibBottom3" 
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:background="@drawable/detail_tab_bg_selector"/>
            <ImageView 
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:scaleType="fitXY" 
                android:src="@drawable/icon_search_48_48" 
                android:layout_gravity="center_horizontal"
                />
            <TextView
                android:id="@+id/tvBottom3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="35dp"
                android:text="@string/bailty_text_search"
                style="@style/bailtyTextBottom"
                />
        </FrameLayout>


        <FrameLayout 
            android:id="@+id/flBottom4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="25"
            >
            <Button 
                android:id="@+id/ibBottom4" 
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:background="@drawable/detail_tab_bg_selector"/>
            <ImageView 
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:scaleType="fitXY" 
                android:src="@drawable/icon_barcode_48_48" 
                android:layout_gravity="center_horizontal"
                />
            <TextView
                android:id="@+id/tvBottom4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="35dp"
                android:text="@string/bailty_text_scan_again"
                style="@style/bailtyTextBottom"
                />
        </FrameLayout>
    </LinearLayout>
Lei Cao
  • 457
  • 6
  • 13
1

Can you explain your question more widely so that we can more understood.

As per my understanding You want to set your ImageButton Height/Width. But it doesn't work is it? I want to ask you that, if you write any specific height/width then also it doesn't work?

I copied your code in my files and I changed the height/width manually then it will work.

Please explain your question.

Thanks.

anddev
  • 3,144
  • 12
  • 39
  • 70