135

On the android website, there is a section about color drawables. Defining these drawables in xml looks like this:

<resources>
    <drawable name="solid_red">#f00</drawable>
    <drawable name="solid_blue">#0000ff</drawable>
    <drawable name="solid_green">#f0f0</drawable>
</resources>

In the java api, they have thr following method to define rounded corners:

setCornerRadius(float radius)

Is there a way to set the rounded corners in the xml?

Jay Askren
  • 10,282
  • 14
  • 53
  • 75
  • To set corners from code see (Gradient Drawables): http://stackoverflow.com/questions/8709595/how-to-set-corner-radiuses-for-the-button-in-java-code – samus Nov 13 '13 at 21:06

3 Answers3

364

Use the <shape> tag to create a drawable in XML with rounded corners. (You can do other stuff with the shape tag like define a color gradient as well).

Here's a copy of a XML file I'm using in one of my apps to create a drawable with a white background, black border and rounded corners:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#ffffffff"/>    
             
    <stroke android:width="3dp"
            android:color="#ff000000" />

    <padding android:left="1dp"
             android:top="1dp"
             android:right="1dp"
             android:bottom="1dp" /> 
             
    <corners android:radius="7dp" /> 
</shape>
artem
  • 16,382
  • 34
  • 113
  • 189
Mark B
  • 183,023
  • 24
  • 297
  • 295
  • 1
    where to save this file and how to get it in my java code?thanks – shyam Sep 29 '11 at 07:43
  • 7
    save it as a xml file in the drawable directory, then use it like you would use any drawable (icon or resource file) using its resource name (R.drawable.your_xml_name) – Guillaume Nov 29 '11 at 10:39
  • 30
    in this particular case all radii are the same, so you could have used android:radius="7dp" – Will Kru Mar 14 '12 at 23:16
  • 2
    Also, the layout renderer in Android Studio won't be able to render it if you define the radius seperately (even with the same values) and would give you a warning "Pat.isConvex is not supported". Just use – Francesco Ambrosini Oct 31 '16 at 10:35
  • @shyam You can set it as the "background". If you use it on a TextView you'll need to remember to add padding for the start and end so the text doesn't get crowded by the rounded edge – RowanPD Jun 04 '17 at 15:49
20

Try below code

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners
    android:bottomLeftRadius="30dp"
    android:bottomRightRadius="30dp"
    android:topLeftRadius="30dp"
    android:topRightRadius="30dp" />
<solid android:color="#1271BB" />

<stroke
    android:width="5dp"
    android:color="#1271BB" />

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

Out put

Mujahid Khan
  • 1,712
  • 1
  • 18
  • 24
19

mbaird's answer works fine. Just be aware that there seems to be a bug in Android (2.1 at least), that if you set any individual corner's radius to 0, it forces all the corners to 0 (at least that's the case with "dp" units; I didn't try it with any other units).

I needed a shape where the top corners were rounded and the bottom corners were square. I got achieved this by setting the corners I wanted to be square to a value slightly larger than 0: 0.1dp. This still renders as square corners, but it doesn't force the other corners to be 0 radius.

John Pisello
  • 1
  • 1
  • 2
  • did you just write 0.1 dp ? does it work, i also need upper rounded corners and lower square ones, same issue as you just i used 1 dp, on the square corners and 10dp on the round ones, u are right it's still noticeable, but gives me 90% what i want, according to the documentation setting 0 on the unround corners should have worked. – codeScriber Jan 16 '11 at 09:13
  • Actually it's not a bug, it's in the documentation: https://developer.android.com/guide/topics/resources/drawable-resource.html#Shape – Tsuharesu Mar 09 '18 at 23:49