5

I have a button which have both text and image. The image size it is taking as actual size of image. How can i change the size of image in button?

<Button
    android:id="@+id/button3"
    android:layout_weight="1"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:drawableTop="@drawable/home_icon"
    android:text="Settings"
    />
Devesh Agrawal
  • 8,982
  • 16
  • 82
  • 131

5 Answers5

4

Set

android:scaleType="fitXY"

Also wrap_content is the preferred Parameter for width and height so that the OS will automatically adjust depending upon the screen size. Please refer to Android - Image Button Scale and Adjusting the size of an ImageButton in Android

Community
  • 1
  • 1
RajeshDA
  • 481
  • 2
  • 13
2

Control the size deterministically with specific measurements. For example:

<Button
    android:id="@+id/button3"
    android:layout_weight="1"
    android:layout_height="50dp"
    android:layout_width="50dp"
    android:background="@drawable/home_icon"
    android:text="Settings"
/>

For even more control over the button's design use a container such as a RelativeLayout to hold the image and the text. Then assign an onClickListener to the container to turn it into a clickable button. Here's an example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/snazzy_button_with_image_above_text"
    android:layout_width="wrap_content"
    android:layout_height="64dp"
    android:layout_gravity="center"
    android:layout_marginLeft="1dp"
    android:background="@color/SkyBlue"
    android:paddingBottom="2dp"
    android:paddingLeft="1dp"
    android:paddingRight="1dp"
    android:paddingTop="1dp" >

<ImageView
    android:id="@+id/your_image"
    android:layout_width="50dp"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@color/white"
    android:contentDescription="image inside button"
    android:scaleType="centerCrop" />

<TextView
    android:id="@+id/your_text"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/your_image"
    android:layout_alignParentLeft="true"
    android:layout_gravity="center"
    android:layout_marginBottom="0dp"
    android:layout_marginTop="0dp"
    android:background="@color/translucent_black"
    android:gravity="bottom|center_horizontal"
    android:paddingTop="0dp"
    android:text="Your Button Text"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="@color/white"
    android:textSize="10sp" />

</RelativeLayout>

In the code assign the onClickListener as such:

yourButton = (RelativeLayout) findViewById(R.id.snazzy_button_with_image_above_text);
yourButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View clickedButton) {
    //button click action here 
}
PeteH
  • 1,870
  • 25
  • 23
  • I think this set the size for complete button not of just image on button. – Devesh Agrawal Dec 10 '15 at 07:12
  • @DeveshAgrawal: I understand now you want an image to be contained within the button. One of the beauties of Android is the ability to turn any layout into a clickable button. In your case you could wrap the image and the text inside a LinearLayout (or a RelativeLayout if you want to get even fancier). You can now totally control the size and placement of the image and text. In your code add an onClickListener to the containing LinearLayout. Voila, you'll have a button that exactly meets your needs. – PeteH Dec 11 '15 at 05:58
  • @DeveshAgrawal: I added an example using a RelativeLayout to my answer. Once you've tried it feel free to select it as the approved answer. – PeteH Dec 11 '15 at 06:18
2

You should use a ImageButton and specify the image in android:src, and set android:scaletype to fitXY


Set Drawable in your code

Drawable drawable = getResources().getDrawable(R.drawable.icon);
drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()*0.10), 
                     (int)(drawable.getIntrinsicHeight()*0.10));
ScaleDrawable sd = new ScaleDrawable(drawable, 0, sWidth,sHeight);
Button btn = findViewbyId(R.id.btnId);
btn.setCompoundDrawables(sd.getDrawable(), null, null, null); //set drawableLeft/Right for example
santoXme
  • 802
  • 5
  • 20
1

Set android:background="@drawable/image_name"

and give width and height like

android:layout_height="10dp"
android:layout_height="10dp"

or you can try using ImageButton instead of Button because ImageButton has more image rendering options than Button You can refer here

Community
  • 1
  • 1
Jayanth
  • 5,954
  • 3
  • 21
  • 38
0

Simple way: add next line to dimens.xml This parameter change size all icons on buttons in AlertDialog

<resources>
    <dimen name="abc_alert_dialog_button_dimen">24dp</dimen>
</resources>