1

I declare a Android button like this:

<Button
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:background="@drawable/festival_button_selector"
 android:textColor="@color/white"
 android:text="@string/STR_FEST_SELOFF"
 android:onClick="onBtnFestSelectionClick"
/>

And here is the content of this button's selector background (festival_button_selector):

<item android:state_focused="true" 
    android:drawable="@drawable/festival_button_focus"
    />
<item android:state_pressed="true" 
    android:drawable="@drawable/festival_button_press"
    />
<item android:state_focused="false" 
    android:state_pressed="false" 
    android:drawable="@drawable/festival_button"
    />

Actually, I want to set this button background is in "focus" state (so button background will be festival_button_focus) as default. If we click other button, this button background will return to "normal" state (with background is festival_button). Can we do it?

P/S: I don't want to change festival_button_selector. It'll be ideal if we have a function like button.setFocusState(true) to make this button on focus state.

anticafe
  • 6,816
  • 9
  • 43
  • 74

3 Answers3

2

Take as look at requestFocus() to set the initial focus state.

androidworkz
  • 2,902
  • 1
  • 19
  • 19
  • requestFocus() looks right, but unfortunately this method is buggy in Android 2.1 and 2.2: http://stackoverflow.com/questions/4211851/requestfocus-problem. Anyway, thanks for response. – anticafe Mar 05 '11 at 14:45
  • from looking at your link it looks like the solution is to use a runnable to set the focus. – androidworkz Mar 05 '11 at 15:43
  • new Thread(new Runnable() { public void run() { ((Button) findViewById(R.id.btnFestivalSelection)).requestFocus(); } }); //-> it doesn't work for me. – anticafe Mar 06 '11 at 02:48
1

Sounds like you need to call View.requestFocus() on your Button.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
0
<item android:state_focused="true" 
    android:drawable="@drawable/festival_button_focus"
    />
<item android:state_pressed="true" 
    android:drawable="@drawable/festival_button_press"
    />
<item
    android:drawable="@drawable/festival_button"
    />

try this

LK Yeung
  • 3,462
  • 5
  • 26
  • 39