173

I need to draw a rounded rectangle in the Android UI. Having the same rounded rectangle for TextView and EditText would also be helpful.

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
M.A.Murali
  • 9,988
  • 36
  • 105
  • 182
  • Have a look at this discussion http://stackoverflow.com/questions/3646415/how-to-create-edittext-with-rounded-corners – Kartik Domadiya Apr 11 '11 at 08:08
  • atleast you should put the picture.. because if someone is looking for same question than it will be easy to understand. – Himanshu Mori Mar 19 '18 at 12:03
  • if you want addionnal accuracy, go [there](https://stackoverflow.com/questions/65450741/to-draw-rounded-rectangle-in-android). – jujuf1 Dec 25 '20 at 20:05

10 Answers10

267

In your layout xml do the following:

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

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

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

</shape>

By changing the android:radius you can change the amount of "radius" of the corners.

<solid> is used to define the color of the drawable.

You can use replace android:radius with android:bottomLeftRadius, android:bottomRightRadius, android:topLeftRadius and android:topRightRadius to define radius for each corner.

Abhimanyu
  • 11,351
  • 7
  • 51
  • 121
Andreass
  • 3,218
  • 1
  • 21
  • 25
  • They didn't ask for a gradient. Don't know why this is accepted answer. – Jerry Destremps Aug 06 '19 at 21:18
  • I guess it was accepted because it was there several months before most of the other answers. I haven't done any Android development for years, so I have no idea how the answer could be changed or updated now to remove the gradient, although I'm guessing the "solid" tag as used in Noundla Sandeep's reply below would probably do the trick. – Andreass Aug 08 '19 at 10:36
128

I think, this is you exactly needed.

Here drawable(xml) file that creates rounded rectangle. round_rect_shape.xml

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

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

    <corners
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"
        android:topLeftRadius="8dp"
        android:topRightRadius="8dp" />

</shape>

Here layout file: my_layout.xml

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/round_rect_shape"
    android:orientation="vertical"
    android:padding="5dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Something text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ff0000" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>
</LinearLayout>

-> In the above code, LinearLayout having the background(That is the key role to place to create rounded rectangle). So you can place any view like TextView, EditText... in that LinearLayout to see background as round rectangle for all.

Eugene Brusov
  • 17,146
  • 6
  • 52
  • 68
Noundla Sandeep
  • 3,334
  • 5
  • 29
  • 56
  • 1
    Is there a way to abstract this? I want to be able to use `android:background="@drawable/round_rect_shape"` in my styles.xml, but to use different background colors by setting another property. Is there any option except creating an identical drawable for each color? – karl Dec 17 '13 at 17:52
  • You can make any shape rounded with this way! – Sabri Meviş Jun 09 '18 at 15:22
22

In monodroid, you can do like this for rounded rectangle, and then keeping this as a parent class, editbox and other layout features can be added.

 class CustomeView : TextView
    {
       public CustomeView (Context context, IAttributeSet ) : base (context, attrs)  
        {  
        }
       public CustomeView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)  
        {  
        }
       protected override void OnDraw(Android.Graphics.Canvas canvas)
       {
           base.OnDraw(canvas);
           Paint p = new Paint();
           p.Color = Color.White;
           canvas.DrawColor(Color.DarkOrange);

           Rect rect = new Rect(0,0,3,3);

           RectF rectF = new RectF(rect);


           canvas.DrawRoundRect( rectF, 1,1, p);



       }  
    }
}
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
learn_andrd
  • 1,046
  • 1
  • 12
  • 15
4
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:padding="10dp"
  android:shape="rectangle">
    <solid android:color="@color/colorAccent" /> 
    <corners
      android:bottomLeftRadius="500dp"
      android:bottomRightRadius="500dp"
      android:topLeftRadius="500dp"
      android:topRightRadius="500dp" />
</shape>

Now, in which element you want to use this shape just add: android:background="@drawable/custom_round_ui_shape"

Create a new XML in drawable named "custom_round_ui_shape"

martennis
  • 882
  • 11
  • 20
Uttam Meerwal
  • 324
  • 2
  • 12
3

Right_click on the drawable and create new layout xml file in the name of for example button_background.xml. then copy and paste the following code. You can change it according your need.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="14dp" />
<solid android:color="@color/colorButton" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
<size
android:width="120dp"
android:height="40dp" />
</shape>

Now you can use it.

<Button
android:background="@drawable/button_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
M Karimi
  • 1,991
  • 1
  • 17
  • 34
2

You could just define a new xml background in the drawables folder

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

After this just include it in your TextView or EditText by defining it in the background.

<TextView
 android:id="@+id/textView"
 android:layout_width="0dp"
 android:layout_height="80dp"
 android:background="YOUR_FILE_HERE"
 Android:layout_weight="1"
 android:gravity="center"
 android:text="TEXT_HERE"
 android:textSize="40sp" />
ibrhm117
  • 387
  • 3
  • 25
1

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.

Dhruv Patel
  • 519
  • 4
  • 5
0
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/white" />
    <corners android:radius="4dp" />
</shape>
Artemiy
  • 2,702
  • 1
  • 19
  • 13
0

I tried several methods and can say that you should also add View.setClipToOutline (API >= 21):

textView.clipToOutline = true

In this case you won't get a rectangular selection below TextView (it occurs on some devices) like below:

enter image description here

Some people wrap a rectangle with CardView. It works, but on some old devices it shows wrong corners, if radius is large enough:

enter image description here

In TextView you can also add this attribute:

android:theme="@style/Theme.MaterialComponents.Light"

It will show darker gray selection when click light gray text labels.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
-1
    paint.apply {
        strokeWidth = lineWidth.toFloat()
        style = Paint.Style.STROKE
        color = lineColor
        ***strokeCap = Paint.Cap.ROUND***
    }
Alireza Nazari
  • 141
  • 1
  • 10