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
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"
-
but my problem is that "backgroud" attribute is just set with the path of my image on my button....So can I obtain transparency? 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 !!! 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.– udenfoxJan 25, 2016 at 10:57
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.
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"/>
-
2
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
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:
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"/>
Instead of using a
Button or Image Button
You can use an
Image
and set the attribute
android:onClick="action"