What's the use of ImageButton - android

When a background can be set on a Button then what is the use of an ImageButton?

With ImageButton (just like with Image) it is possible to control the way image defined by android:src is shown on the button: you can set scaleType - something you cannot do to the background.

Related

How can I add padding to the background image on a button programatically?

I'm creating a custom implementation that extends android.support.v7.widget.AppCompatButton. I am using setBackground(Drawable) in the button's constructor to put an image on the button, but want the image to be centered on the button with a little padding. How can I do this?
It sounds like you should be using ImageButton instead of Button. If you do so, you can take whatever image you'd normally use and create an Inset Drawable from it, and then use that as the src attribute of your ImageButton.
Say you have a drawable res/drawable-mdpi/mydrawable.png. You could create res/drawable/mydrawable_inset.xml as follows:
<inset
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/mydrawable"
android:insetTop="100dp"
android:insetLeft="100dp"
android:insetRight="100dp"
android:insetBottom="100dp"/>
Here's a layout that includes one ImageButton with just #drawable/mydrawable and a second with #drawable/mydrawable_inset:

Adding background to button breaks sizing

I have an icon I would like to scale down to 48dp and use as a button (no background). However, adding the android:background attribute (to make the background transparent) seems to break the sizing I have applied to the button and it displays at its full size:
Desired size but with ugly background:
<ImageButton
android:id="#+id/composition_send"
android:layout_width="48dp"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:scaleType="fitCenter"
android:src="#drawable/ic_send_black_48dp"/>
Full (wrong) size, but with no background (desired):
<ImageButton
...
android:background="?android:selectableItemBackground"/>
Why does adding that one line ruin the sizing?
try to add
android:src="#drawable/ic_send_black_48dp
android:backgroundTint="#0000"
to ImageButton, it will make the background transparent and then you can set the image on it.
When you don't set the background property for the Imagebutton, the Imagebutton will use the default Imagebutton background of Android. And this background contains the padding so your image is smaller.
So when you set the background for Imagebutton by a color or resource that doesn't contain padding, your image will look larger
You can find the default background of the Imagebutton and another View in
..\Android\sdk\platforms\android-23\data\res\drawable\
(Some resource in this is not public)
For example
If you compile your project with API 23, the default background of ImageButton is btn_default_material
<ImageButton
...
android:background="#android:drawable/btn_default_material"
android:src="#drawable/ic_send_black_48dp"/>
<ImageButton
...
android:src="#drawable/ic_send_black_48dp"/>
So you set the android:background="#android:drawable/btn_default_material" or don't use background property, 2 ImageButton will look same (note: btn_default_material is not public for use)
Hope this help
If you don't want that ugly background in imagebutton,then you can use an imageview as button and set onClickListener on it. It will behave like a button.
This Link will be helpful to you.

Change color of an image in android

I wonder if there is a way to change the color of my image on the button easily,
my simple example is like this,
CircleButton.setImageResource(android.R.drawable.ic_media_play);
I have a play audio icon (white) and want to change the icon color to red, like this
CircleButton.setImageResource(ColorChange(Red, android.R.drawable.ic_media_play));
What seems to be the easiest way for doing ColorChange() ?
Use ColorFilter to change the button image color. Your button should be ImageButton
Set the image resource to the ImageButton
CircleButton.setImageResource(android.R.drawable.ic_media_play);
And, change the image color when required like this
CircleButton.setColorFilter(getResources().getColor(R.color.any_color));
Refer : http://developer.android.com/reference/android/graphics/drawable/Drawable.html#setColorFilter(android.graphics.ColorFilter)

How to overlay a button image over a button object?

I have added an image resource for a button in an activity but the Android button object appears bigger than the image resource as shown here: https://plus.google.com/112628117356947034778/posts/AH4B1pvbTeM
As you can see the grey background is the button object appearing from behind the image resource.
I want the button image to overlay the button but when I try to re size it,it stays the same dimension.Does anyone have a solution to this problem? Thanks
Use:
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#drawable/image_name"
// Use fixed value Or Put the all type of image with fix size in drawable-hdpi, mdpi,xhdpi, ldpi
If you will use ImageButton instead of button image background was overlay
Use ImageButton as the element, and also set the attribute android:scaleType="fitXY"

How to change image button transparency programmatically?

How to set the transparency of the image button programmatically in android? I am creating dynamic image buttons on the fly.
You should be able to change a button with the following:
btnMybutton.getBackground().setAlpha(45);
ImageButton.setBackgroundColor(Color.TRANSPARENT) Should solve the problem.
From http://developer.android.com/reference/android/view/View.html#setBackgroundColor%28int%29
Set the background to a given Drawable, or remove the background. If
the background has padding, this View's padding is set to the
background's padding. However, when a background is removed, this
View's padding isn't touched. If setting the padding is desired,
please use setPadding(int, int, int, int).
I like this:
imageButton.setImageAlpha(alpha_value); //alpha_value between 0...255

Categories

Resources