4

Possible Duplicate:
How to Set Opacity (Alpha) for View in Android

i want to ask two questions:

1) i want to make the buttons in my main.xml i.e simply the first screen look semi-transparent. It should be such that the background image can be partly-seen through it. But the button should maintain its normal size and looks.

2)(This is regarding another button in another view) i changed the background of buttons using

android:background="#2563EA"

but now when it is clicked it does not change its color. how can i reset it. also can anyone tell me how to give it a new onClick color.

Community
  • 1
  • 1
Tejas Tamkahne
  • 233
  • 2
  • 5
  • 11
  • There is already a thread about setting the alpha of buttons in Android here. http://stackoverflow.com/questions/2838757/how-to-set-opacity-alpha-for-view-in-android – Turnsole Jun 08 '11 at 17:26
  • If you want to ask two questions, **ask two questions**, don't put them both in **one** question =) – Rob Jun 08 '11 at 17:55

2 Answers2

3

To change the appearance of a button depending on state, use a StateList.

Turnsole
  • 3,422
  • 5
  • 30
  • 52
2
  1. Use the alpha property for the transparency of the color. Also see this thread.
  2. You can dynamically change the color by using the OnTouch event. Or better, you can assign the background in the XML to be a selector.

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true"
              android:drawable="@drawable/button_pressed" /> <!-- pressed -->
        <item android:state_focused="true"
              android:drawable="@drawable/button_focused" /> <!-- focused -->
        <item android:drawable="@drawable/button_normal" /> <!-- default -->
    </selector>
    
Community
  • 1
  • 1
Dayerman
  • 3,973
  • 6
  • 38
  • 54