enter image description here

How to make buttons of complex shape, let's say the borders of this map in android

  • 6
    you have a strange map :) - mit

1 answer 1

Create your own custom button, for example in the form of a rectangle (in fact, you can draw anything in onDraw ):

 public class RectButton extends View { public RectButton(Context context) { super(context); } public RectButton(Context context, AttributeSet attrs) { super(context, attrs); } public RectButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint = new Paint(); paint.setColor(Color.RED); canvas.drawRect(100, 200, 150, 250, paint); } } 

Add it to the activity programmatically or via an xml file, and then hang up on it the click handler:

 RectButton button = (RectButton) findViewById(R.id.rect_button); button.setOnTouchListener((view, event)-> { switch (event.getAction()) { case MotionEvent.ACTION_UP: //some code break; case MotionEvent.ACTION_DOWN: //some code break; } });