Hi, I would like to create a custom button and make it interactive by adding a click or touch listener.
I created a RoundButton class the following way: public class RoundButton extends View { private String buttonText; private static final int buttonRadius = 36; private static final float textSize = 16; private int px; private int py; private int buttonState; private int textHeight; private Paint circlePaint; private Paint textPaint; public void SetPosition(int px, int py) { this.px = px; this.py = py; } @Override protected void onDraw(Canvas canvas) { if(px == 0 || py == 0) { px = 50; py = 50; } //int radius = Math.min(px, py); canvas.drawCircle(px, py, buttonRadius, circlePaint); canvas.save(); int textWidth = (int)textPaint.measureText(buttonText); int cardinalX = px - textWidth/2; int cardinalY = py - textHeight/2; canvas.drawText(buttonText, cardinalX, cardinalY, textPaint); canvas.restore(); //super.onDraw(canvas); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int measuredWidth = measure(widthMeasureSpec); int measuredHeight = measure(heightMeasureSpec); int d = Math.min(measuredWidth, measuredHeight); setMeasuredDimension(d, d); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } private int measure(int measureSpec) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if(specMode == MeasureSpec.UNSPECIFIED) { result = 200; } else { result = specSize; } return result; } public RoundButton(Context context) { super(context); initRoundButton(); } public RoundButton(Context context, AttributeSet attrs) { super(context, attrs); initRoundButton(); } protected void initRoundButton() { setFocusable(true); circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG); circlePaint.setColor(R.color.roundButtonUpColor); circlePaint.setStyle(Paint.Style.FILL_AND_STROKE); Resources r = this.getResources(); textPaint = new Paint(Paint.ANTI_ALIAS_FLAG); textPaint.setColor(r.getColor(R.color.roundButtonTextColor)); textPaint.setTextSize(textSize); if(buttonText == null) { textHeight = (int)textPaint.measureText("Yy"); } else { textHeight = (int)textPaint.measureText(buttonText); } } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub return super.onTouchEvent(event); } public void setButtonText(String buttonText) { this.buttonText = buttonText; } public String getButtonText() { return buttonText; } protected class ButtonState { public static final int BUTTON_STATE_READY = 0; public static final int BUTTON_STATE_PRESSED = 1; } public void test() { int i = 0; } } Then I added this custom view to the main layout and bound the control the following way: rbActionButton = (RoundButton)findViewById(R.id.actionButton); rbActionButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { onActionButton(); } }); but the onClick() doesn't execute when I touch the button. Any ideas what could be wrong here? Thanks! Niko --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~----------~----~----~----~------~----~------~--~---