OK, I know this issue has been covered in different questions but I’m trying a different approach here.
This is my custom View class:
public class MyView extends View {
Button mButton;
public MyView(Context context) {
super(context);
mButton = new Button(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//Sets the size needed.
}
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
//Draws some graphics.
canvas.restore();
canvas.save();
RectF boundRect = new RectF(left,top,right,bottom);
canvas.clipRect(boundRect);
mButton.layout(0,0,canvas.getWidth(),canvas.getHeight());
mButton.draw(canvas);
canvas.restore();
}
}
This draws the button in the correct position and size, but, the button seems half transparent and is not clickable. Does anybody know why and how to fix it?
I don’t know if anyone is following this but I didn’t feel it was right to leave the question open.
It appears that you can do this directly extending the View class but you must also implement some interfaces that ViewGroup implements:
This will make the button clickable.
I also discovered that the transparency originates from the default ICS Holo Dark theme, so the fact the button is a little transparent is desirable.
Eventually, not wanting to reinvent the wheel, I chose to extend ViewGroup instead of View.