Rounded corner for textview in android
Asked Answered
S

12

233

I have a textview and want its corner to be in round shape. I already know it can be done using android:background="@drawable/somefile". In my case, this tag is already included so cannot use again. e.g android:background="@drawable/mydialogbox" is already there to create image in background

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_gravity="top"
    android:background="@drawable/mydialogbox"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textview_name"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    </LinearLayout>

</RelativeLayout>

so when I want textview(textview_name) also with round corner, how this can be achieved.

Snowfield answered 13/9, 2013 at 8:40 Comment(1)
Google have new framework, new technologies is better [Jetpack Compose][1] [1]: #6055062Shaw
E
525
  1. Create rounded_corner.xml in the drawable folder and add the following content,

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >         
       <stroke
           android:width="1dp"
           android:color="@color/common_border_color" />
    
       <solid android:color="#ffffff" />
    
       <padding
            android:left="1dp"
            android:right="1dp"
            android:bottom="1dp"
            android:top="1dp" />
    
       <corners android:radius="5dp" />
    </shape>
    
  2. Set this drawable in the TextView background property like so:

    android:background="@drawable/rounded_corner"
    

I hope this is useful for you.

Easter answered 13/9, 2013 at 9:7 Comment(6)
The answer is correct just that the guy who posted did not explain it in detail. You need to create an xml [eg. rounded_view.xml]in your drawable folder with the above code. And in your layout surrounding your textview put this as a parameter android:background="@drawable/rounded_view"Ivelisseivens
android:background="@drawable/rounded_corner" do not use extension here!Martainn
Add android:shape="rectangle" if it didnt work for youAlf
And rebuild your project if it didn't work automaticallyEufemiaeugen
The xml code should be enclosed by <shape xmlns:android="http://schemas.android.com/apk/res/android">Disaccord
Helpful but not completeDiversified
G
58

Beside radius, there are some property to round corner like topRightRadius, topLeftRadius, bottomRightRadius, bottomLeftRadius

Example TextView with red border with corner and gray background

bg_rounded.xml (in the drawables folder)

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke
        android:width="10dp"
        android:color="#f00" />

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

    <corners
        android:radius="5dp"
        android:topRightRadius="100dp" />
</shape>

TextView

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_rounded"
    android:text="Text"
    android:padding="20dp"
    android:layout_margin="10dp"
    />

Result

enter image description here

Geerts answered 22/10, 2018 at 6:19 Comment(0)
H
25

With the Material Components Library you can use the MaterialShapeDrawable.

With a TextView:

    <TextView
        android:id="@+id/textview"
        ../>

You can programmatically apply a MaterialShapeDrawable:

float radius = getResources().getDimension(R.dimen.corner_radius);

TextView textView = findViewById(R.id.textview);
ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
        .toBuilder()
        .setAllCorners(CornerFamily.ROUNDED,radius)
        .build();

MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
ViewCompat.setBackground(textView,shapeDrawable);

enter image description here

If you want to change the background color and the border just apply:

shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,R.color.....));
shapeDrawable.setStroke(2.0f, ContextCompat.getColor(this,R.color....));
Hague answered 13/5, 2020 at 7:26 Comment(6)
wish you had defined R.dimen.corner_radius properly for completeness.Stenophagous
I failed to use this in a RecyclerView Adapter.Stenophagous
@Stenophagous It is just a dimen (like 8dp).Hague
Awesome answer ! @Stenophagous textView corner_radius is just a float ; set any value your want : float radius = 12f; for example.Lorettelorgnette
Great one but how do I add the RGB colors? Dynamic one which I received as a String as data.Unbreathed
Would be nice to see the layout xml for this method as wellAntarctic
B
17

Since your top level view already has android:background property set, you can use a <layer-list> (link) to create a new XML drawable that combines both your old background and your new rounded corners background.

Each <item> element in the list is drawn over the next, so the last item in the list is the one that ends up on top.

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <bitmap android:src="@drawable/mydialogbox" />
    </item>
    <item>
        <shape>
            <stroke
                android:width="1dp"
                android:color="@color/common_border_color" />

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

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

            <corners android:radius="5dp" />
        </shape>
    </item>
</layer-list>
Babyblueeyes answered 31/7, 2014 at 14:22 Comment(0)
D
14

There is Two steps

1) Create this file in your drawable folder :- rounded_corner.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
         <corners android:radius="10dp" />  // set radius of corner
         <stroke android:width="2dp" android:color="#ff3478" /> // set color and width of border
         <solid android:color="#FFFFFF" /> // inner bgcolor
</shape>

2) Set this file into your TextView as background property.

android:background="@drawable/rounded_corner"

You can use this drawable in Button or Edittext also

Dmz answered 8/11, 2019 at 10:54 Comment(0)
C
8

create an xml gradient.xml file under drawable folder

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle"  >
            <corners android:radius="50dip" />
            <stroke android:width="1dip" android:color="#667162" />
            <gradient android:angle="-90" android:startColor="#ffffff" android:endColor="#ffffff" />
        </shape>
    </item>
</selector>

then add this to your TextView

android:background="@drawable/gradient"
Cronin answered 16/11, 2015 at 15:58 Comment(0)
C
8

You can use the provided rectangle shape (without a gradient, unless you want one) as follows:

In drawable/rounded_rectangle.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="5dp" />
    <stroke android:width="1dp" android:color="#ff0000" />
    <solid android:color="#00ff00" />
</shape>

Then in your text view:

android:background="@drawable/rounded_rectangle"

Of course, you will want to customize the dimensions and colors.

Caliph answered 10/10, 2017 at 12:0 Comment(1)
how can we add a gradient pleaseStenophagous
C
7
  1. Right Click on Drawable Folder and Create new File
  2. Name the file according to you and add the extension as .xml.
  3. Add the following code in the file
  <?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle">
      <corners android:radius="5dp" />
      <stroke android:width="1dp"  />
      <solid android:color="#1e90ff" />
  </shape>
  1. Add the line where you want the rounded edge android:background="@drawable/corner"
Cytherea answered 5/7, 2018 at 8:56 Comment(0)
G
6
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dp" />
            <solid android:color="#ffffff"/>

        </shape>
    </item>
</layer-list>
Gyrostatic answered 11/10, 2015 at 21:29 Comment(0)
C
4

Put your TextView inside MaterialCardView:

    <com.google.android.material.card.MaterialCardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardCornerRadius="25dp"
        >
        <com.google.android.material.textview.MaterialTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/emergency_sms_template"
            android:padding="6dp"
            android:textSize="11sp"
            />
    </com.google.android.material.card.MaterialCardView>
Carping answered 8/4, 2022 at 12:43 Comment(1)
It could be useful to add: app:strokeWidth="0dp" (to remove stroke edge)Poachy
S
0

You can use SVG for rounding corners and load into an ImageView and use ConstraintLayout to bring ImageView on TextView

I used it for rounded ImageView and rounded TextView

Splendiferous answered 3/7, 2018 at 8:38 Comment(0)
A
-1

Simply using an rounded corner image as the background of that view

And don't forget to have your custom image in drawable folder

android:background="@drawable/my_custom_image"
Alessandraalessandria answered 5/3, 2020 at 14:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.