onClick buttons -- New Image


Last Updated: 2010-12-05 17:14:24
  1. Vitality93

    Vitality93 Member

    Hey all, I've added a background to my buttons with the background: @drawable/button1 option in the properties menu.

    What I'm trying to do is have the button change to a @drawable/button1_pressed image so that it looks like the image is being clicked whenever someone pushes it. And then I want it to return to normal after they take their finger off the button.

    Right now, I have it changing to the button1_pressed image whenever someone clicks it but then it doesn't change back to it's original state whenever someone takes their finger off. Also, there is a little delay for the button1_pressed image to show up and it doesn't show up if they hold their finger on the button.

    Here is the part of the code in my main.java file:
    Code (Text):
    1.  
    2.        
    3. button1.setOnClickListener(new OnClickListener() {
    4.         public void onClick(View v) {
    5.              button1.setBackgroundResource(R.drawable.button1_pressed);
    6.              Intent startNewActivityOpen = new Intent(main.this, page2.class);
    7.              startActivityForResult(startNewActivityOpen, 0);
    8.         }
    9. });
    10.  
    Is there an easier way to do this via propeties? I realize there should be a way to do this by coding but I just can't figure it out... Any suggestions?
    -Thanks!

    Advertisement
  2. id0001

    id0001 Well-Known Member

    You have to make a custom drawable like this:
    Code (Text):
    1.  
    2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
    3.     <item android:state_focused="true" android:state_pressed="false"
    4.         android:drawable="@drawable/grid_selector_background_focus" />
    5.     <item android:state_focused="true" android:state_pressed="true"
    6.         android:drawable="@drawable/grid_selector_background_pressed" />
    7.     <item android:state_focused="false" android:state_pressed="true"
    8.         android:drawable="@drawable/grid_selector_background_pressed" />
    9.     <item android:state_focused="false" android:state_pressed="false"
    10.         android:drawable="@android:color/transparent" />
    11. </selector>
    12.  
  3. Vitality93

    Vitality93 Member

    thank you that worked perfectly

Share This Page