views:

30

answers:

1

Hi,

It's possible to specify a particular function to be called when button is clicked by setting its onClick attribute in xml: android:onClick="function_name" I want to do something similar for FocusChange of buttons, is this possible?

Specifically, I wish to increase my button's size when it has focus and make it normal sized when not focussed. To apply this to all my buttons, I hoped to call a specific function which does this and then specify this function name in my style.xml. But there is no onFocusChange attribute in android.

It works when I add overriden onSetFocusChangeListener for each of my buttons separately in the code. But is there any other solution?

Pls help.

-Kiki

+1  A: 

Handling UI Events you need to implement event listeners.

// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
    public void onClick(View v) {
      // do something when the button is clicked
    }
};

You can use the item selector to specify the states of a button:

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
     <item android:state_pressed="true"
           android:drawable="@drawable/l_bar_default3" /> <!-- pressed -->
     <item android:state_focused="true"
           android:drawable="@drawable/l_bar_default3" /> <!-- focused -->
     <item android:drawable="@drawable/l_bar_pressed3" /> <!-- default -->
 </selector>
ArtWorkAD
I need to change the ButtonSize when focussed, not clicked. It works when I set the OnFocusChangeListener and inside onFocusChange, I change the button size. But I have to do this separately for each button. Is there a way I can give just one implementation and all my buttons will use this when Focus changes? Is that what you explained as anonymous implementation of OnClickListener?
kiki
yes, use the same structure for View.OnFocusChangeListener. use button.setOnFocusListener(mCorkyListener) to add the listener to a button
ArtWorkAD
thanx, ArtWork!
kiki