Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add an image button with transparent background

I want to add image buttons on my main app screen that have a transparent background. I have created the images in GIMP and saved them with transparent backgrounds in png format but when I add them to my android app in Eclipse they appear with a white background. How do I remove this in my code?

like image 919
Izzy Dev. Avatar asked Apr 17 '12 15:04

Izzy Dev.


People also ask

Can you make a button transparent?

The transparent button can be easily created by using HTML and CSS. In this article, we will use background-color: transparent; property to design the transparent background button. HTML Code: In this section, we will create a basic structure of button using the button Tag.

How to set image button background transparent in android studio?

You can specify a transparent background for the image button. The result will be a clickable image that acts like a button but can look like whatever you want it to look like. Just set android:background="@null" for the image button.

How do you fit a picture inside a button?

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. However, it is much easier to set and preview in xml in my opinion.

How do I remove background from ImageButton?

To remove the standard button background image, define your own background image or set the background color to be transparent. Save the XML file in your project res/drawable/ folder and then reference it as a drawable for the source of your ImageButton (in the android:src attribute).


2 Answers

Try using null for the background for your image button in the xml layout.

android:background="@null"
like image 100
Anurag Ramdasan Avatar answered Oct 13 '22 14:10

Anurag Ramdasan


A transparent background generally works when used in photoshop, if it is not, you have to set the alpha bits of the pixels around the border of your image

use alpha-masking( subset of alpha blending, google them to know more).

A little theory: depending on the alpha bits for each pixel in your Bitmap (the translucency bits), the extent of blending of that pixel with over-written pixel is determined. Considering extremes, if alpha is 255, overwriting pixel is used instead of the over-written one (fully opaque, in regular terms); if alpha is 0, overwriting pixel is just ignored (transparent). For other alphas in between: there is blending.

In your case you would have to make the alpha of the border zero, to achieve complete blend. Outlining all the steps in geral:.

  1. The drawable to be used can't be a JPEG( JPEG doesn't store alpha values per pixel). Go for PNGs

    2.You will need to create and keep your bitmap drawable in that way beforehand(use google for alpha blending PNGs) such that the borders have zero alpha value ( use softwares like Paint.NET for ex).

  2. If images are being created dynamically, you would need to use a blending equation ( for details read materials by Porter and Duff).

    For ex, this is PNG image with transparent pixels having alpha 0 other than the alphabet itself
    . original PNG with alpha blending

aplha blended PNG on colored background

Above is the alpha blended PNG on colored background of an Activity. I hope this is what you really want to achieve.

like image 28
Akhil Avatar answered Oct 13 '22 14:10

Akhil