217

I want to create custom button and I need it to be circle. How can I create a circle button? I do not think that be possible with draw9patch.

Also I do not know how to make custom button!

Do you have any suggestion?

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Ata
  • 12,126
  • 19
  • 63
  • 97
  • you have to extend button class and override the onDraw method. – Ashwin N Bhanushali Mar 27 '12 at 06:14
  • 2
    but problem is here if I extends that , and user touches out of circle , Onlcick will be called , how to solve this . – Ata Mar 27 '12 at 06:17
  • why dont you just set a round shape image as background to your button? – MKJParekh Mar 27 '12 at 06:42
  • I did simple OnDrawn , it shows on Design view , but when I want to run application , it makes this problem on loading : android.view.InflateException: Binary XML file line #52: Error inflating class com.inaros.magicbox._customButtonPlay – Ata Mar 27 '12 at 06:51

11 Answers11

375

Use xml drawable like this:

Save the following contents as round_button.xml in drawable folder

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="oval">
            <solid android:color="#fa09ad"/>
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape android:shape="oval">
            <solid android:color="#c20586"/>
        </shape>
    </item>
</selector>

Android Material Effect: Although FloatingActionButton is a better option, If you want to do it using xml selector, create a folder drawable-v21 in res and save another round_button.xml there with following xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#c20586">
    <item>
        <shape android:shape="oval">
            <solid android:color="#fa09ad"/>
        </shape>
    </item>
</ripple>

And set it as background of Button in xml like this:

<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="hello"
android:textColor="#fff" />

Important:

  1. If you want it to show all these states (enabled, disabled, highlighted etc), you will use selector as described here.
  2. You've to keep both files in order to make the drawable backward-compatible. Otherwise, you'll face weird exceptions in previous android version.
Community
  • 1
  • 1
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
  • By drawable folder you mean to open a folder named 'drawable', or to save it in drawable-mdpi, hdpi, etc...? – Jjang Nov 12 '15 at 10:17
  • @Jjang no, you just need to save it to 'drawable' folder, any xml resource for drawable is typically saved in 'drawable', not in mdpi, hdpi etc. – Adil Soomro Nov 12 '15 at 10:22
  • K thx, I had no drawable folder until now (only mdpi, hdpi... and -v21) :) – Jjang Nov 12 '15 at 10:32
  • P.S How to give the button a default ripple color? Like the grey one that is coloring all the buttons in the app by default. – Jjang Nov 12 '15 at 10:35
  • @AdilSoomro i need your email address pls or mail me [adilm717@gmail.com](https://plus.google.com/u/0/+AdilMadiii),adil from pak – Adiii Jul 04 '16 at 02:17
  • @Adil Soomro: Is it possible to create inner circle of different radius? and also is it possible to drop shadow? Thanks – user1090751 Mar 03 '20 at 04:56
  • @user1090751 yes, you want to look at [layer-list](https://developer.android.com/guide/topics/resources/drawable-resource#LayerList). – Adil Soomro Mar 03 '20 at 21:39
69

Markushi wrote a circle button widget with amazing effects. Click here!

enter image description here

LOG_TAG
  • 19,894
  • 12
  • 72
  • 105
Jerome
  • 1,749
  • 1
  • 14
  • 40
  • How to use this with a non circular image? – tomsoft Aug 22 '15 at 15:57
  • 5
    Just a heads up: the library linked in this answer is now deprecated. The author (Markushi) recommends using a [Floating Action Button](https://developer.android.com/reference/android/support/design/widget/FloatingActionButton.html) instead. – Reinstate Monica Sep 15 '15 at 13:06
  • @ABoschman Can we put text in the circle button widget? Or just for drawables – Ruchir Baronia Mar 26 '16 at 03:18
  • @RuchirBaronia I think you really should consider the Android FAB. How much prose do you want to place inside a small round button? It seems made for icons. Check out: https://www.google.com/design/spec/components/buttons-floating-action-button.html# – Reinstate Monica Mar 26 '16 at 05:13
  • @ABoschman Hey, but I really like the effect of this button in this library. Is there any way? – Ruchir Baronia Mar 26 '16 at 05:33
  • @RuchirBaronia Well, it's up to you. The library is still there, you could possibly use it. Remember though that it's deprecated and no longer being developed. This might become a serious issue in the future. – Reinstate Monica Mar 26 '16 at 06:06
  • can i use it without effects? – Fernando Torres Dec 16 '19 at 02:32
  • @ReinstateMonica: And FAB too is now deprecated, it is not recommended to use AndroidX. – Jack Aidley Jun 12 '20 at 12:06
34

With the official Material Components library you can use the MaterialButton applying a Widget.MaterialComponents.Button.Icon style.

Something like:

   <com.google.android.material.button.MaterialButton
            android:layout_width="48dp"
            android:layout_height="48dp"
            style="@style/Widget.MaterialComponents.Button.Icon"
            app:icon="@drawable/ic_add"
            app:iconSize="24dp"
            app:iconPadding="0dp"
            android:insetLeft="0dp"
            android:insetTop="0dp"
            android:insetRight="0dp"
            android:insetBottom="0dp"
            app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.MyApp.Button.Rounded"
            />

Currently the app:iconPadding="0dp",android:insetLeft,android:insetTop,android:insetRight,android:insetBottom attributes are needed to center the icon on the button avoiding extra padding space.

Use the app:shapeAppearanceOverlay attribute to get rounded corners. In this case you will have a circle.

  <style name="ShapeAppearanceOverlay.MyApp.Button.Rounded" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">50%</item>
  </style>

The final result:

enter image description here


With jetpack compose you can use:

    Button(
        onClick = { /* Do something! */ },
        modifier = Modifier.width(48.dp).height(48.dp),
        shape = CircleShape
    ) {
        Icon(Icons.Filled.Add, "")
    }

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 1
    Thanks @Gabriele Mariotti. Worth highlighting the need for `app:iconPadding="0dp"` to ensure the icon is centered. I burnt a bunch of time trying center until I found this. It seems an oversight that app:iconGravity is missing a "center" value. – scottyab Apr 03 '20 at 15:09
  • 1
    Thanks @Gabriele Mariotti. Work fine for me! – Androidz May 13 '20 at 20:08
  • 1
    To remove the elevation from the button, one can use the following style: Widget.MaterialComponents.Button.UnelevatedButton.Icon – Punpuf Aug 09 '20 at 20:03
23

AngryTool for custom android button

You can make any kind of custom android button with this tool site... i make circle and square button with round corner with this toolsite.. Visit it may be i will help you

Pranav P
  • 1,755
  • 19
  • 39
7

For a FAB looking button this style on a MaterialButton:

<com.google.android.material.button.MaterialButton
    style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
    app:cornerRadius="28dp"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:text="1" />

Result:

enter image description here

If you change the size be careful to use half of the button size as app:cornerRadius.

Roberto Leinardi
  • 10,641
  • 6
  • 65
  • 69
7

You can use MaterialButton from AndroidX material library

<com.google.android.material.button.MaterialButton
     android:layout_width="75dp"
     android:layout_height="75dp"
     android:layout_margin="10dp"
     android:insetLeft="0dp"
     android:insetTop="0dp"
     android:insetRight="0dp"
     android:insetBottom="0dp"
     app:cornerRadius="50dp"
     app:icon="@drawable/ic_camera"
     app:iconGravity="textStart"
     app:iconPadding="0dp"
     app:iconSize="35dp" />

and it will be like this

Button preview

AmrDeveloper
  • 3,826
  • 1
  • 21
  • 30
2

if you want use VectorDrawable and ConstraintLayout

<FrameLayout
            android:id="@+id/ok_button"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:foreground="?attr/selectableItemBackgroundBorderless"
            android:background="@drawable/circle_button">
                <android.support.constraint.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <ImageView
                        android:id="@+id/icon_of_button"
                        android:layout_width="32dp"
                        android:layout_height="32dp"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintRight_toRightOf="parent"
                        app:layout_constraintLeft_toLeftOf="parent"
                        app:srcCompat="@drawable/ic_thumbs_up"/>
                    <TextView
                        android:id="@+id/text_of_button"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        app:layout_constraintTop_toBottomOf="@+id/icon_of_button"
                        app:layout_constraintRight_toRightOf="parent"
                        app:layout_constraintLeft_toLeftOf="parent"
                        android:layout_marginTop="5dp"
                        android:textColor="@android:color/white"
                        android:text="ok"
                        />
                </android.support.constraint.ConstraintLayout>
            </FrameLayout>

circle background: circle_button.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="1000dp" />
<solid android:color="#41ba7a" />
<stroke
    android:width="2dip"
    android:color="#03ae3c" />
<padding
    android:bottom="4dp"
    android:left="4dp"
    android:right="4dp"
    android:top="4dp" />
</shape>
Milad ranjbar
  • 569
  • 1
  • 8
  • 17
2

Unfortunately using an XML drawable and overriding the background means you have to explicitly set the colour instead of being able to use the app style colours.

Rather than hardcode the button colours for every behaviour I opted to hardcode the corner radius, which feels marginally less hacky and retains all the default button behaviour (changing colour when it's pressed and other visual effects) and uses the app style colours by default:

  1. Set android:layout_height and android:layout_width to the same value

  2. Set app:cornerRadius to half of the height/width

    (It actually appears that anything greater than or equal to half of the height/width works, so to avoid having to change the radius every time you update the height/width, you could instead set it to a very high value such as 1000dp, the risk being it could break if this behaviour ever changes.)

  3. Set android:insetBottom and android:insetTop to 0dp to get a perfect circle

For example:

<Button
    android:insetBottom="0dp"
    android:insetTop="0dp"
    android:layout_height="150dp"
    android:layout_width="150dp"
    app:cornerRadius="75dp"
    />
bmaupin
  • 14,427
  • 5
  • 89
  • 94
1

here is how you can perform simply, make a drawable resource file in drawable.xml. Say round_button.xml and then paste the following code.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
       <shape
           android:shape="oval">

           <solid
               android:color="@color/button_start_gradient_color"/>
       </shape>
    </item>
    <item
        android:drawable="@drawable/microphone"/>
</layer-list>

Note:- use your own color and drawable resource as i have used @drawable/microphone

Following is the result [1]: https://i.stack.imgur.com/QyhdJ.png

Shashank Pandey
  • 683
  • 6
  • 14
1

If you want to do with ImageButton, use the following. It will create round ImageButton with material ripples.

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_settings_6"
android:background="?selectableItemBackgroundBorderless"
android:padding="10dp"
/>
karatuno
  • 365
  • 5
  • 18
0

Create a new vector asset in the drawable folder.

You can import your PNG image as well, and convert the file to SVG online at https://image.online-convert.com/convert-to-svg. The higher the resolution, the better the conversion will be.

Next, create a new vector asset from that SVG file.

This is a sample vector circle image you can use. Copy the code to an xml file in the drawables folder.

ic_check.xml:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportHeight="256"
    android:viewportWidth="256">
    <path
        android:fillColor="#2962FF"
        android:pathData="M111,1.7c-7.2,1.1 -22.2,4.8 -27.9,7 -33.2,12.5 -61.3,40.3 -74.1,73.3 -8.7,22.6 -10.5,55.3 -4.4,78 10.9,40 39.7,72.4 77.4,87 22.6,8.7 55.3,10.5 78,4.4 45.3,-12.3 79.1,-46.1 91.4,-91.4 2.9,-10.7 3.9,-21.9 3.3,-37.4 -0.7,-21.2 -4.6,-35.9 -14,-54.1 -18.2,-35 -54,-60.5 -93.4,-66.4 -6.7,-1 -30.7,-1.3 -36.3,-0.4zM145,23.1c21.8,3.3 46.5,16.5 61.1,32.8 20.4,22.6 30.1,51.2 27.7,81.1 -3.5,44.4 -35.9,82.7 -79.6,94 -21.6,5.6 -46.6,3.7 -67.8,-5.1 -10.4,-4.3 -24.7,-14.1 -33.4,-22.9 -41.6,-41.5 -41.6,-108.4 0,-150 24.3,-24.3 57.6,-35.1 92,-29.9z"
        android:strokeColor="#00000000" />
    <path
        android:fillColor="#2962FF"
        android:pathData="M148.4,113c-24.6,26 -43.3,44.9 -44,44.6 -0.7,-0.3 -8.5,-6.1 -17.3,-13 -8.9,-6.9 -16.5,-12.6 -17,-12.6 -1.4,-0 -25.6,19 -25.8,20.3 -0.3,1.4 62.7,50.2 64.8,50.2 1.7,-0 108.4,-112.3 108.4,-114.1 0,-1.3 -23.8,-20.4 -25.4,-20.4 -0.6,-0 -20.2,20.3 -43.7,45z"
        android:strokeColor="#00000000" />
</vector>

Use this image in your button:

<ImageButton
    android:id="@+id/btn_level1"
    android:layout_width="36dp"
    android:layout_height="36dp"
    android:background="@drawable/ic_check"
/>

Your button will be a circle button.

enter image description here

live-love
  • 48,840
  • 22
  • 240
  • 204