Changing image of ImageButton on press and release

In Mono For Android, I have an ImageButton and I want to change its image while user is holding the button. And when user release the button image should change to pervious image. I am trying with the code below but nothing happens:

private void _goForward_KeyPress(object sender, View.KeyEventArgs e)
{
    if(e.Event.Action == KeyEventActions.Down)
    {
        _goForward.SetImageResource(Resource.Drawable.arrowUpGreen);
    }
    else
    {
        _goForward.SetImageResource(Resource.Drawable.arrowUpRed);
    }
}

Posts

  • CTCHANGCTCHANG AUMember ✭✭

    The easy way is create a style xml (MyButton.xml) file under the resource\drawable folder

    <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="false" android:drawable="@drawable/recordstopbutton" /> <item android:state_pressed="true" android:state_enabled="true" android:drawable="@drawable/recordstopbutton_pressed" /> <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/recordstopbutton" /> <item android:state_enabled="true" android:drawable="@drawable/recordstopbutton" /> </selector>

    Then under the layout xaml, just consume it like below,

    <ImageButton android:id="@+id/btnAction" android:layout_width="99px" android:layout_height="70px" android:background="@drawable/mybutton" android:clickable="true" android:layout_gravity="center"/>

  • carleslscarlesls ESMember

    The easy way is create a style xml (MyButton.xml) file under the resource\drawable folder

    <?xml version="1.0" encoding="utf-8"?>





    Then under the layout xaml, just consume it like below,

    What about about button? how do you change them?
    ok done thank you anyway

Sign In or Register to comment.