How do I set the rounded corner radius of a color drawable using xml?

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?


Asked by: Kellan981 | Posted: 20-01-2022






Answer 1

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>

Answered by: Anna808 | Posted: 21-02-2022



Answer 2

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.

Answered by: Briony238 | Posted: 21-02-2022



Answer 3

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

Answered by: Arnold140 | Posted: 21-02-2022



Similar questions

android - Is this a built in drawable?

In this linked image , I see the button on the right quite often in a lot of apps. On my Moto Droid, it is used extensively in the settings app. It is also used as the default AlertDialog icon. Can I use this via a android.r.drawable?


android - How to add a 9patch image to a xml drawable

I have the following drawable which draw a rectangle. Can you please tell me how can I add a 9patch image as the background of this drawable? &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;shape xmlns:android="http://schemas.android.com/apk/res/android" &gt; &lt;stroke android:width="1dp" android:color="#FFFFFFFF" /&gt; &lt;solid android:color="@android:color/transparent"/&gt; &lt;/shape&gt;


android - Is this a built in drawable?

In this linked image , I see the button on the right quite often in a lot of apps. On my Moto Droid, it is used extensively in the settings app. It is also used as the default AlertDialog icon. Can I use this via a android.r.drawable?


How to convert a Bitmap to Drawable in android?

How can I convert a Bitmap image to Drawable ?


Is it possible to set an android icon from a drawable in a separate jar?

I want to set a menu option icon for a drawable that is in another jar inside an xml file. &lt;item android:id="@+id/my_location" android:title="My Location" android:icon="@+drawable/my_location"&gt; Instead of drawable/my_location have something like com.somelib.R.drawable.someDrawable. I can set this in onCreateOptionsMenu but was just wondering if it could be done vi...


Android Drawable question

I am trying to create a drawable in code and change the color based on some criteria. I can get it to work but it doesn't want to let me set the padding on the view. Any help would be appreciated. &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;ImageView android:id="@+id/icon" android:layout_width="50px" android:layout_height="fill_parent" /&gt; &lt;TextView ...


android - How to add a 9patch image to a xml drawable

I have the following drawable which draw a rectangle. Can you please tell me how can I add a 9patch image as the background of this drawable? &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;shape xmlns:android="http://schemas.android.com/apk/res/android" &gt; &lt;stroke android:width="1dp" android:color="#FFFFFFFF" /&gt; &lt;solid android:color="@android:color/transparent"/&gt; &lt;/shape&gt;


android - How to get a byte array from a drawable resource ?

I would like to get a byte array from an jpeg image located in my res/drawable file ? Does anyone know how to do that please ?


android - How to draw a Drawable over a View

I have several Views in my Activity. I want to draw a Drawable over these views. The Drawable should be on top of the views (that means it hides the views below it. The Drawable may appear over several views (that is I can't just draw it in one view). How can this be done? Thank you


android - How to convert a Drawable to a Bitmap?

I would like to set a certain Drawable as the device's wallpaper, but all wallpaper functions accept Bitmaps only. I cannot use WallpaperManager because I'm pre 2.1. Also, my drawables are downloaded from the web and do not reside in R.drawable.


android - Is there any way to tell what folder a drawable resource was inflated from?

Does anyone know of a way to tell which folder Android system will resolve to when I request a particular drawable from the Resources (drawable-long vs. drawable-notlong, for example)? I want to make sure it's loading resources the way I would expect it to (for instance, to make sure the Tattoo is really ldpi and not mdpi since it's right on the borderline). I co...


android - Select Drawable file path

I am developing an android application that receives data about events from a server. In a ListView I put the name and category of the event and a picture. My problem is: each category should have a specific picture. How do I choose which of the pictures in the drawable folder should be displayed? View v; /**I get this view from the parameters.**/ ImageView iconCategory = (ImageView) v .findViewById...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top