Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android ImageButton do not stretch image

How can I prevent the ImageButton from stretching the image?

Using this snippet:

 <ImageButton
                android:src="@drawable/prediction"
                android:background="?android:attr/selectableItemBackground"
                android:gravity="center_horizontal"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:contentDescription="some description"
                android:text="@string/predict"
                local:MvxBind="Click ExecuteQueryCmd" />

I get the following effect:

enter image description here

Image is 64x64 png.

All I want is for the ImageButton to respect native width and length, hence resolution, of the image it is being assigned. Recommendations based on a similar post is to use a Button, set its background to an image then use custom selectors but this sounds like a hack. Or is this the proper way to do this?

like image 791
Klaus Nji Avatar asked Nov 19 '13 15:11

Klaus Nji


People also ask

How do you make a picture fit in ImageButton?

How to programatically resize and show them? Use a android:scaleType="fitCenter" to have Android scale the images, and android:adjustViewBounds="true" to have them adjust their bounds due to scaling. All of these attributes can be set in code on each ImageButton at runtime.

What is ImageButton in android?

Displays a button with an image (instead of text) that can be pressed or clicked by the user. By default, an ImageButton looks like a regular Button , with the standard button background that changes color during different button states.

How to set image on button in android XML?

In android, we can add an image to the button by using <ImageButton> attribute android:src in XML layout file or by using the setImageResource() method. In android, we can create ImageButton control in two ways either in the XML layout file or create it in the Activity file programmatically.


1 Answers

It work for me with

  <ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:background="@android:color/transparent"
    android:contentDescription="some description"
    android:scaleType="centerInside"
    android:src="@drawable/ci_icon"
    android:text="@string/predict"
    local:MvxBind="Click ExecuteQueryCmd" />

Put your image in android:src attribute

like image 96
azerto00 Avatar answered Oct 04 '22 20:10

azerto00