Bera
Bera

Reputation: 693

Android Circle button

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.

Upvotes: 4

Views: 4541

Answers (4)

numan salati
numan salati

Reputation: 19494

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"
/>

Upvotes: 4

Ewoks
Ewoks

Reputation: 12445

there is ImageButton class which can serve ur purpose..

Upvotes: 0

Yash
Yash

Reputation: 1751

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.

Upvotes: 0

Gautam
Gautam

Reputation: 7958

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

Upvotes: 0

Related Questions