Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 334
Jay Askren Avatar asked Jan 23 '10 05:01

Jay Askren


People also ask

How do I round corners in XML?

To do that right click on drawable (under resources), choose New, choose Drawable Resource File. Name the resource rounded_blue_border and make the root element shape. We will use the attributes stroke, corners and padding. The stroke determines the width and color of the border.

How do I change the color of a drawable in XML?

Use app:drawableTint="@color/yourColor" in the xml instade android:drawableTint="@color/yourColor" . It has the backward compatibility.

How do you make rounded corners on Android?

xml file and add an attribute to that TextView, for which you want to add rounded corners. The attribute is android: background=”@drawable/rounded_corner_view”.


1 Answers

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> 
like image 118
Mark B Avatar answered Sep 18 '22 14:09

Mark B