40

I want to make a button exactly like in this image

Capsule button

I want to use a xml file that will used to produce such button. Can anyone tell me how to do that?

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
user3393926
  • 1,169
  • 1
  • 8
  • 17
  • You can check over here for possible answer of yours.http://stackoverflow.com/questions/7606995/android-how-apply-shape-and-selector-simultaneously-for-button – virendrao Dec 30 '14 at 10:27

7 Answers7

67

finally I found the way to do it with xml file. here is the code of the xml file that gave me the capsule shape button.

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

  <corners
    android:bottomLeftRadius="30dp"
    android:bottomRightRadius="30dp"
    android:radius="60dp"
    android:topLeftRadius="30dp"
    android:topRightRadius="30dp" />

  <solid android:color="#CFCFCF" />

  <padding
    android:bottom="0dp"
    android:left="0dp"
    android:right="0dp"
    android:top="0dp" />

  <size
    android:height="60dp"
    android:width="270dp" />

</shape>    
user3393926
  • 1,169
  • 1
  • 8
  • 17
  • 1
    The `radius` attribute will get ignored because you also set the individual radius (e.g. `bottomLeftRadius`). To quote the [docs](http://developer.android.com/guide/topics/resources/drawable-resource.html), "The radius for all corners, as a dimension value or dimension resource. This is overridden for each corner by the following attributes." – tir38 Nov 17 '15 at 23:23
  • I have removed the static size attribute and increased the common radius to 50 dp(after removing all the top,bottom,left and right radius) – Ravi Yadav Jun 29 '16 at 05:06
  • If you want the shape to stay correct for any view size, just set the radius to 10000dp – Carson Holzheimer Apr 12 '18 at 03:32
  • 1
    So complex for a solution. See https://stackoverflow.com/a/51947130/3940133 for more **simple** and **elegant** solution. – HendraWD Aug 21 '18 at 10:58
  • This removed the ripple effect. – Muhammed Aydogan Jul 27 '22 at 13:20
12

Just use a MaterialButton using the cornerRadius attribute and your favorite width.

 <com.google.android.material.button.MaterialButton
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            app:cornerRadius="18dp"
            android:text="BUTTON"
            />

enter image description here

You can also use the shapeAppearanceOverlay attribute:

<com.google.android.material.button.MaterialButton
        app:shapeAppearanceOverlay="@style/buttomShape"
        .../>

with:

  <style name="buttomShape">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">50%</item>
  </style>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 3
    This should be the new accepted answer with modern android development using Material Components – Rinav Aug 12 '20 at 21:35
  • How do you tell this "smart" widget to be always capsule no matter what height it gets without crappy workarounds like giving it `app:cornerRadius="GOOGOLdp"`? – Farid Nov 23 '21 at 10:59
  • 1
    @Farid I've updated the answer. You can use the shapeAppearanceOverlay attribute using a cornerSize of 50%. – Gabriele Mariotti Nov 23 '21 at 11:04
9

I haven't tried all of the answers here, but I believe some/all of them are works. The accepted answer is also works, but it can be simplified. Since I love elegant and simple solution, I come up with my own solution. Basically, we just need to add radius big enough compared with your View width and height, so it will create a round corner. In this case I put 1000dp to make it safe. We don't even need to add android:shape="rectangle" at all.

Just follow these 2 simple steps:

  1. Create an XML file in your drawable folder. For example let's name it bg.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="1000dp"/>
    
        <solid android:color="@color/yourPreferredColor"/>
    </shape>
    
  2. Then you can use it in layout XML file as your View property android:background="@drawable/bg" or directly in the code view.setBackgroundResource(R.drawable.bg)

HendraWD
  • 2,984
  • 2
  • 31
  • 45
3

It's called a Chip in Material and can be used like so:

<com.google.android.material.chip.Chip
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"/>

More info can be found here and here.

To use Material components in your project, you'll to add the appropriate lib to your build.gradle:

dependencies {
    // ...
    implementation 'com.google.android.material:material:1.0.0-beta01'
    // ...
  }

More info on adding Material to your project can be found here.

Alternatively, you can use the latest version of the support design lib:

<android.support.design.chip.Chip
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:chipText="@string/hello_world"/>

Also pull in the appropriate libs:

dependencies {
    // ...
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
    implementation 'com.android.support:design:28.0.0-alpha1'
    // OR
    implementation 'com.android.support:design-chip:28.0.0-alpha1'
    // ...
}

See this answer for more on the latter approach.

Big McLargeHuge
  • 14,841
  • 10
  • 80
  • 108
  • Nice! I believe this can simplify Android development. But IMHO, it's too overkill, since it has so many functionalities that we don't even need to create a simple capsule-shape button. – HendraWD Nov 15 '18 at 06:44
2

consider customizing a shape to it and use corners inside that shape:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="10dp"/> <!-- increasing the value, increases the rounding. And as  TTransmit said, to make it like a capsule make the radius half of your button height -->
    <solid android:color="#AAAAAA"/> <!-- the button color -->

</shape>

So, save that shape in your /drawable folder, let's say it will be saved as "button_bg.xml", so when declaring the Button in your layout xml:

<Button
    android:background="@drawable/button_bg"
    android:layout_height="20dp"
                .
                .                          />
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • 1
    The trick to get the capsule shape is to additionally specify the height of the button element as twice the corner radius i.e. 20dp in this case. Making a style using the height and the drawable background together would be a good option. – TTransmit Dec 30 '14 at 10:39
  • I tried the above mention ways using both rectangle and oval shape but nothing seems to give the capsule shape i want. – user3393926 Dec 30 '14 at 10:48
  • @user3393926 So, what you got ? – Muhammed Refaat Dec 30 '14 at 10:49
  • @user3393926 if you got a rounded shape but not rounded enough to look like a capsule, try what TTransmit suggest of manually set the height of the Button as twice the radius value or try to combine them together in a style if you don't have the value of the button fixed – Muhammed Refaat Dec 30 '14 at 10:52
  • Feel free to add `android:height="20dp"` to the `Button` in your answer. It should make it clearer for other users. – TTransmit Dec 30 '14 at 10:52
  • Would this cause so much problem when supporting multiple screen? I always avoid specifying everything in dp. Specifying dp in rounded corners is forgivable since this is the only value I have to support on larger screen. But height on a view in dp units? This is bad. Imagine I have to specify the best dp units on different screens. – Neon Warge Nov 03 '16 at 01:03
  • 1
    @NeonWarge specifying values in dp is the best practice in specifying size values in Android, and in a case like a small button it won't harm to specify a fixed value, however in the case of full layouts, the best practice to specify dp values is to specify values for each density or values for each size, for example name the value "dp_test" and specify it with a slight different changes in values-sw600dp/dimens and values-sw720dp/dimens – Muhammed Refaat Nov 04 '16 at 14:46
1

Here is code to create button in xml,but if you want to create button as a capsule shaped you have to add the background

        <Button
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            **android:background="@drawable/button_background"**
            android:text="@string/image" >
        </Button>

create button_background.xml in drawable folder,write the following code in button_background.xml

         <?xml version="1.0" encoding="UTF-8"?>

          <shape xmlns:android="http://schemas.android.com/apk/res/android"

             android:shape="rectangle" android:padding="40dp">

          <!-- you can use any color you want I used here gray color-->

         <solid android:color="#01A9DB"/>

          <corners

          android:bottomRightRadius="20dp"

          android:bottomLeftRadius="20dp"

          android:topLeftRadius="20dp"

          android:topRightRadius="20dp"/>

      </shape>
0

Try below, works with any view size:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners android:radius="1000dp" />

    <solid android:color="@android:color/black" />

</shape>
Kevin Crain
  • 1,905
  • 1
  • 19
  • 28