0

I want to set opacity to my ImageButton, so when it is unselected, I can see the background a bit, and when I press on it - it becomes normal(no transparency).

lomza
  • 9,412
  • 15
  • 70
  • 85
  • See this thread [How to have a transparent ImageButton: Android](http://stackoverflow.com/questions/3402787/how-to-have-a-transparent-imagebutton-android) – Vetsin Apr 14 '11 at 22:52
  • But the background of my ImageButton is transparent. I want to make the foreground(the image) on my button a bit transparent. – lomza Apr 15 '11 at 06:48
  • You can use the property `Alpha` as explain in the link http://stackoverflow.com/questions/4931071/android-and-setting-alpha-for-image-view-alpha – CIRCLE Jul 17 '15 at 11:38

2 Answers2

0

If the background you are using is itself an image, then you can't simply "set" the transparency, it's coming from the png image that is the resource for the background. I'd recommend creating 3 9-patch png images for the different stages of the button using transparency as necessary for whichever stage you like. There's a description of how to use a different graphic and xml config file for your own background images in the docs on ImageButton

http://developer.android.com/reference/android/widget/ImageButton.html

If you used a solid color for the background, transparency can be achieved using a color code that has AARRGGBB as elements. android:background="#55FF0000" would be a partially transparent red background.

Maximus
  • 8,351
  • 3
  • 29
  • 37
  • I already use the "background" tag, as I want to have just an image, without button: android:background="@null". "9-patch png images" - what if it looks like this - http://png-3.findicons.com/files/icons/1405/clipper_system/128/get_info.png? How then I can create a 9-patch image? – lomza Apr 15 '11 at 04:52
  • Since that image is a definite size, it wouldn't need to be 9 patch. 9 patch is used for images that you want to be stretchable. – Maximus Apr 15 '11 at 14:53
  • there are no words, just an image. Why do I need to make it stretchable? – lomza Apr 16 '11 at 12:21
  • You don't, I was saying that it wouldn't have to be 9 patch. Do you have Photoshop or GIMP? You could load up the image and make it partially transparent, then as the example in my link describes and how Kocus describes, you can create 3 images for the different button stages. – Maximus Apr 16 '11 at 18:17
0

Use Selector (http://developer.android.com/reference/android/content/res/ColorStateList.html)

The layout code would look sth like that :

android:background="@drawable/my_selector"

and the selector code would be my_selector.xml with following content :

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/button_without_opactity" />
    <item android:state_selected="true" android:drawable="@drawable/button_without_opactity" />

    <item android:drawable="@drawable/button_with_opacity" />

</selector>

button_without_opacity & button_with_opacity should be 9-patches

Kocus
  • 1,613
  • 17
  • 31