4

How I can to make custom circle button? (only make clicks on circle )

Is any way to make it with circle png file?

I tried with imageView override onTouch method but it works very badly because view.getWidth(), view.getHeight() and view.getTop... methods works very bad

public boolean inCircle(MotionEvent e, int radius, int x, int y) {
    int dx = (int) (e.getX() - x);
    int dy = (int) (e.getY() - y);
    double d = Math.sqrt((dx * dx) + (dy * dy));
    if (d < radius)
        return true;
    return false;
}

Thanks.

Palash Dange
  • 127
  • 2
  • 10
Bera
  • 673
  • 2
  • 12
  • 31

4 Answers4

4

Its very simple. Create a custom shape drawable and set that as the background of your view. Example:

round_button_drawable.xml in drawable/

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >
    <solid android:color="@android:color/holo_orange_dark"/>
</shape>

set this drawable as the background of any view.

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/round_button_drawable"
    android:text="btn"
/>
numan salati
  • 19,394
  • 9
  • 63
  • 66
0

Another way would be to extend Button and override its onDraw method You can then draw a circle on the canvas using canvas.drawCircle method.

You can also draw the circle.png file on the canvas using Drawable.draw method

Gautam
  • 7,868
  • 12
  • 64
  • 105
0

there is ImageButton class which can serve ur purpose..

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Ewoks
  • 12,285
  • 8
  • 58
  • 67
  • Somebody who went mad and voted negative on all answers should comment what is the problem – Ewoks Dec 30 '15 at 08:18
0

Dont go for complex logic just select any rounded image and set that image as a background of your simple button it will look like simple round button and it will accept click only on that round shape.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
Yash
  • 1,751
  • 13
  • 14