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"
Related
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:
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.
I've been working on my android calculator and i can't seem to insert an image instead of text on a button. For example sqrt I don't want to have sqrt written on my button i want the image of the symbol on it, the same goes for x^y and alot of others. I have my custom background of the button working just fine. Thank you for your help in advance :)
EDIT:
Thats not what i wanted i did manage to get my custom button and thats fine. On my button since its a calculator button i have android:text on it saying sqrt. I want to remove that and insert another png on my button showing the symbol of the square root instead of that text. Thank you for quick responses.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/imageName"/>
You can have both text and image (or no text) when using the attribute drawableTop, drawableBottom, drawableLeft and drawableRight.
And for positioning the image how you want, consider using: paddingTop, paddingBottom, paddingLeft and paddingRight.
You can do this by ImageButton:
<ImageButton
android:id="#+id/submitEmailButton"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_gravity="right" android:layout_marginRight="15dip"
android:background="#drawable/ic_submit" />
Here ic_submit is the image you want to show as a button.
I used this tutorial and it worked perfectly for me: http://www.mkyong.com/android/android-imagebutton-selector-example/
I haven't seen your code but basically you should remove the "text" tag from your xml and it should appear the image without text then.
I don't know if that's exactly what you were looking for or not, but I reccomend you to follow the tutorial and for sure it will work for you ;)
There are a number of alternatives you can try to implement to build your SQRT button:
draw the button as a 9-patch drawable so your SQRT symbol won't get distorted;
use a RelativeLayout with a button inside and implement a selector
You can simply set background for the Button and keep its text blank.
I have an ImageButton and I'd like to remove the ugly (IMHO) background that surrounds the Image. I could just add an ImageView, but they're very hard to get set perfectly in a Layout like the grey one pictured. [gravity "Center" doesn't make it go to the middle, just centers it Vertically.)
So any way to remove that?
Just use android:background="#0000"
(#0000 same with #00000000)
or
ImageButton imageButton = new ImageButton(this);
imageButton.setBackgroundDrawable(null);
The default background is not transparent.
So, just add the transparent color "#00000000" as you background, then you could solve it.
p.s. #00000000 is the transparent color
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:background="#00000000"
/>
You can keep the square box and just change its color like this:
android:backgroundTint="#color/colorPrimary"
works beautifully
I'm having the same problem but now it has been solved. If you are using "android:background="#null" it will resulted of no background at all and no button pressed animation will be appeared to the user.
Just adding this line of code to make an image button become flashing upon clicked. This is the proper way to alert the user an image button has been clicked.
"android:background="?android:selectableItemBackground"
just useandroid:background="#null" in your xml
if you set the
android:background
instead of
android:src
it should overwrite the gray background
Yeah. If you drag ImageButton that will usually ask for ImageSource. But the ImageSource(android:src) doesn't remove the gray background. It is an image that should be on top of background:
Instead of that try android:background in layout XML.
please use the Draw 9-patch format for the image b'use it is provided by SDK
1. From a terminal, launch the draw9patch application from your SDK /tools directory.
2. Drag your PNG image into the Draw 9-patch window (or File > Open 9-patch... to locate the file). Your workspace will now open.
The left pane is your drawing area, in which you can edit the lines for the stretchable patches and content area. The right pane is the preview area, where you can preview your graphic when stretched.
3. Click within the 1-pixel perimeter to draw the lines that define the stretchable patches and (optional) content area. Right-click (or hold Shift and click, on Mac) to erase previously drawn lines.
4. When done, select File > Save 9-patch...
Your image will be saved with the .9.png file name.
Use like this
android:background="#android:color/transparent"
android:src="#drawable/icon"
All proposed solutions remove the ripple animation (on tap)
Settings background tint mode to 'add' will remove the background and keep ripple animation:
android:backgroundTintMode="add"
If using Xamarin Forms it can be done like this in the xaml view. Set background to transparent.
<ImageButton BackgroundColor="Transparent"
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.