Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a pill shaped button in android?

I am not sure what to call this... pill shaped maybe. Googling for rounded corners turns up lots of posts of people wanting a rectangular button with rounded corners. This is a little different but more like 2 circles and a rectangle which I tried to draw it as.

I would like to make a button shaped something like the first image below but with text and an icon image in it by using an xml drawable background on an android :

I have tried this which looks ok but if the button length varies it does not scale and you end up with a rectangle and some other strange stuff.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:left="40dp" android:top="0dp" android:bottom="0dp">

        <shape android:shape="oval" >
            <solid android:color="#666666"/>
            <size android:width="40dp" android:height="40dp"></size>

        </shape>

    </item>

    <item android:left="20dp" android:right="20dp">
        <shape android:shape="rectangle" >
            <solid android:color="#666666"/>
            <size android:width="20dp" android:height="20dp"></size>
        </shape>


    </item>

    <item android:right="40dp">
        <shape android:shape="oval" >
            <solid android:color="#666666"/>
            <size android:width="40dp" android:height="40dp"></size>

        </shape>

    </item>


</layer-list>

enter image description here

I have tried to create my xml drawable like this also:

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

            <corners android:radius="20dp"/>
            <size android:height="20dp" android:width="60dp"></size>
        </shape>

which looks like this:

enter image description here

I have looked at this persons example but when I do what he does my results are not completely rounded at the corners. They are more like the second image above.

like image 925
startoftext Avatar asked Aug 08 '14 19:08

startoftext


People also ask

How do you make a rounded rectangle on Android?

Use CardView for Round Rectangle. CardView give more functionality like cardCornerRadius, cardBackgroundColor, cardElevation & many more. CardView make UI more suitable then Custom Round Rectangle drawable.


2 Answers

You can try to save the following code to a drawable(e.g. pill_bg.xml):

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

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

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

    <stroke
        android:width="1dp"
        android:color="#efefef" />

    <size
        android:width="300dp"
        android:height="120dp" />

</shape>

Take a look at size part and radius. Then apply the drawable to your view, for example:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="250dp"
    android:layout_height="60dp"
    android:background="@drawable/pill_bg" />

As you can see, your view can have different sizes, the background will be scaled.

like image 171
Pahomi Avatar answered Oct 24 '22 01:10

Pahomi


As of v1.1.0-beta01 of the Material Components Android library you can use MaterialButton and a shapeAppearance style to easily define a pill button that doesn't require you to know the height of the button beforehand or rely on guessing a radius value high enough to go halfway down the side of the button.

Simply define a style like this:

<style name="PillShapeAppearance">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">50%</item>
</style>

and set it on your MaterialButton like this:

<com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ...
        app:shapeAppearanceOverlay="@style/PillShapeAppearance"
        />

The key that every other answer I've looked at leaves out is to set cornerSize with a percent value so that the fraction value it supports is properly applied.

like image 40
pumpkinpie65 Avatar answered Oct 24 '22 01:10

pumpkinpie65