onClick buttons -- New Image


  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):

           
    button1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                 button1.setBackgroundResource(R.drawable.button1_pressed);
                 Intent startNewActivityOpen = new Intent(main.this, page2.class);
                 startActivityForResult(startNewActivityOpen, 0);
            }
    });
     
    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):

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_focused="true" android:state_pressed="false"
            android:drawable="@drawable/grid_selector_background_focus" />
        <item android:state_focused="true" android:state_pressed="true"
            android:drawable="@drawable/grid_selector_background_pressed" />
        <item android:state_focused="false" android:state_pressed="true"
            android:drawable="@drawable/grid_selector_background_pressed" />
        <item android:state_focused="false" android:state_pressed="false"
            android:drawable="@android:color/transparent" />
    </selector>
     
  3. Vitality93

    Vitality93 Member

    thank you that worked perfectly
Loading...

Share This Page