3

I have the following button defined:

public class PressedButton extends Button {

private static final int[] STATE_PRESSED = {R.attr.state_pressed};
private static final int[] STATE_UNPRESSED = {R.attr.state_unpressed};
private boolean mIsPressed = false;
private boolean mIsUnpressed = false;

public PressedButton(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public PressedButton(Context context) {
    super(context);
}

public void setPressed(boolean isPressed) {mIsPressed = isPressed;}
public void setUnpressed(boolean isUnpressed) {mIsUnpressed = isUnpressed;}

@Override
protected int[] onCreateDrawableState(int extraSpace) {
    final int[] drawableState = super.onCreateDrawableState(extraSpace + 2);
    if (mIsPressed) {
        mergeDrawableStates(drawableState, STATE_PRESSED);
    }
    if (mIsUnpressed) {
        mergeDrawableStates(drawableState, STATE_UNPRESSED);
    }
    return drawableState;
}

Then, in the main Activity I call it this way:

PressedButton btn01=(PressedButton)findViewById(R.id.buttonP1);

When I try to set a Click Listener like this:

btn01.setOnClickListener(new View.OnClickListener() {
        //@Override
        public void onClick(View v) {
            typeLogin = 0;
            Log.e("FINGERPRINT: ", "TypeLogin set to "+ new Integer(typeLogin).toString());
        }
    });

Nothing happens when I press it, the button is displaying correctly but it doesn't enter the code when I click on it, it is worth to be noted that when I use (Button) instead of (PressedButton) it works flawlessly, so I would like to know what am I doing wrong with my custom class.

Some more information, I'm using the selected answer here to create a custom button with states: How to add a custom button state, this is my attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="buttonPressed">
        <attr name="state_pressed" format="boolean" />
        <attr name="state_unpressed" format="boolean" />
    </declare-styleable>
</resources>

And this is pressed_buton.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.fgtit.fingermap">
    <item
        app:state_pressed="true"
        app:state_unpressed="false"
        android:drawable="@drawable/pressed" />
    <item
        app:state_pressed="false"
        app:state_unpressed="true"
        android:drawable="@drawable/unpressed" />
</selector>

And finally, the button it's used in MainActivity like this:

    <com.fgtit.utils.PressedButton
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res/com.fgtit.fingermap"
        android:id="@+id/buttonP1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableTop="@drawable/msign"
        android:height="100dp"
        android:paddingTop="20dp"
        android:text="@string/action_btn1"
        android:textColor="@color/darkgreen"
        android:width="180dp"
        app:state_pressed="false"
        app:state_unpressed="true"
        android:background="@drawable/pressed"/>
Francisco Peters
  • 677
  • 6
  • 22

1 Answers1

0

Make your PressedButton class implement OnClickListener and implement it.

Following code:

public class YourButton extends Button implements OnClickListener {

     YourButton(stuff) { 
         //bla bla bla
         setOnClickListener(this);
      }

    // bla bla bla

    @Override
    public void onClick(View v) {
        // Do something
    }

}
FlyingNades
  • 432
  • 3
  • 16