6

I am trying to create a transparent Button. In details I set the background's button with an image and I would like to modify the transparency of the button in order to see the background layout's color.

6 Answers 6

9

you can use setAlpha() method to your Button.

Usingbutton.setAlpha(0.0)from your java code, will make your button fully transparent.

Or you can also use it in your layout xml. Inside your <Button /> tag use the following attribute:

android:alpha="0"
3
  • but my problem is that "backgroud" attribute is just set with the path of my image on my button....So can I obtain transparency?
    – francedema
    May 5, 2013 at 11:11
  • no doesn't work... I set "android:alpha="0"" in my layout xml but the button dissapear but if i click in his position I can revele your click...so i try to set "android:alpha="50"" or android:alpha="255" but nothing happens the button is the same !!!
    – francedema
    May 5, 2013 at 12:30
  • @francedema The alpha value may be set in the range from 0 to 1. Where 0 - 100% transparency, where 1 - 0% transparency. So it's work, You just need to use it in a right way.
    – udenfox
    Jan 25, 2016 at 10:57
3

You can use android:alpha="0.5" to set your button half-transparent (the value has to be a float, where 0.0 is invisible and 1.0 is opaque), but be aware that this only works from API Level 11 (Android 3.0) onwards.

Another option would be to make change the background image itself and make it transparent using a graphics program like Gimp or Fireworks.

1
  • How did you found that this only works from api 11 onwards? Jan 9, 2016 at 10:08
3

If you use

?android:attr/selectableItemBackground

you'll have a transparent background with Ripple Effect according to this article of android-developers

Here an example

<Button
    android:id="@+id/test"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Change Password"
    android:background="?android:attr/selectableItemBackground"
    android:textSize="12sp"
    android:gravity="left|center"/>
1
  • 2
    or use style="@style/Widget.AppCompat.Button.Borderless"
    – Roel
    Mar 11, 2016 at 14:58
1

simply if you want to make button transparent you can use

android:background="@android:color/transparent"

or use #0000 four zero in has code.let it only be 4 otherwise it will be considered as black.

hope this helps

1
  • Thanks @Karan Mer for the four zero concept! Dec 10, 2013 at 4:43
1

I know the question is about Button, but it's easy to achieve 'button' that is:

  • Transparent
  • Without shadows or borders
  • With animations

using TextView with attributes:

        android:background="?attr/selectableItemBackground"
        android:clickable="true"

it looks like this:

enter image description here

Code example:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="?attr/selectableItemBackground"
        android:clickable="true"
        android:onClick="onClickMethod"
        android:textStyle="bold"
        android:text="@string/transparent"/>
-1

Instead of using a

Button or Image Button

You can use an

Image

and set the attribute

android:onClick="action"

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.