Reputation: 1478
I have a custom ImageButton and I want to change it's image on click. So far I havent been able to get the onClick method inside the button to perform it's action.
public class FlashButtonView extends ImageButton{
private Drawable mFlashOffSrc, mFlashOnSrc, mFlashAutoSrc;
private Drawable mCurrentFlashMode = mFlashAutoSrc;
public FlashButtonView(Context context) {
super(context);
}
public FlashButtonView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context.getTheme().obtainStyledAttributes(attrs, R.styleable.FlashButtonView, 0, 0));
}
public FlashButtonView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context.getTheme().obtainStyledAttributes(attrs, R.styleable.FlashButtonView, 0, defStyleAttr));
}
private void init(TypedArray typedArray) {
try {
mFlashAutoSrc = typedArray.getDrawable(R.styleable.FlashButtonView_autoSrcImage);
mFlashOnSrc = typedArray.getDrawable(R.styleable.FlashButtonView_onSrcImage);
mFlashOffSrc = typedArray.getDrawable(R.styleable.FlashButtonView_offSrcImage);
} finally {
typedArray.recycle();
}
setImageDrawable(mFlashAutoSrc);
mCurrentFlashMode = mFlashAutoSrc;
setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
if(mCurrentFlashMode == mFlashAutoSrc) {
setImageDrawable(mFlashOnSrc);
mCurrentFlashMode = mFlashOnSrc;
}
else if(mCurrentFlashMode == mFlashOnSrc){
setImageDrawable(mFlashOffSrc);
mCurrentFlashMode = mFlashOffSrc;
}
else{
setImageDrawable(mFlashAutoSrc);
mCurrentFlashMode = mFlashAutoSrc;
}
}
});
}
}
And this is how my XML looks like:
<!--.___ Flash on/off switcher __.-->
<blablabla.FlashButtonView
custom:autoSrcImage="@drawable/button_autoflash"
custom:offSrcImage="@drawable/button_noflash"
custom:onSrcImage="@drawable/button_flash"
android:id="@+id/flash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="@dimen/distance_20dp"
android:layout_marginStart="@dimen/distance_20dp"
android:layout_marginBottom="@dimen/distance_20dp"
android:background="@android:color/transparent"
android:clickable="true"/>
What am I missing or doing wrong?
Upvotes: 0
Views: 112
Reputation: 1499
Implement onTouchEvent()
method. Something like this:
float touched_x, touched_y;
boolean touched = false;
@Override public boolean onTouchEvent(MotionEvent event) {
touchCounter++;
touched_x = event.getX();
touched_y = event.getY();
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
touched = true;
break;
case MotionEvent.ACTION_MOVE:
touched = true;
break;
case MotionEvent.ACTION_UP:
touched = false;
break;
case MotionEvent.ACTION_CANCEL:
touched = false;
break;
case MotionEvent.ACTION_OUTSIDE:
touched = false;
break; default:
}
return true;
}
Upvotes: 1
Reputation: 71
A minimal example with:
public class CustomButton extends ImageButton {
public CustomButton(Context context) {
super(context);
init();
}
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
this.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d(getClass().getSimpleName(), "ButtonPressed");
}
});
}
}
and they layout:
<com.example.bossb.test.CustomButton
android:layout_width="match_parent"
android:layout_height="match_parent" />
works fine for me.
Upvotes: 0