5

I want to add rounded corners and borders to a textview. But only the top corners should be rounded and the bottom should be without border. Already found this:

https://www.android-examples.com/add-rounded-border-to-textview-programmatically/

But then I have rounded corners at the bottom too.

How can I change this?

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841

3 Answers3

3

Create a drawable file like this :

    <?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetBottom="-4dp">

    <shape android:shape="rectangle">
        <solid android:color="#FFFFFF" />
        <stroke android:width="4dp" android:color="#000000" />
        <corners android:radius="4dp" />
    </shape>

</inset>

And then apply it as an background of any control and there you go, it's done.

Karan Mehta
  • 1,442
  • 13
  • 32
2

With the Material Components Library you can use the MaterialShapeDrawable to draw custom shapes.

With a TextView you can do:

<TextView
    android:id="@+id/tv_rounded"
    android:paddingLeft="8dp"
    ../>

Then create a MaterialShapeDrawable. Something like:

    TextView textview = findViewById(R.id.tv_rounded);
    ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
        .toBuilder()
        .setAllCorners(CornerFamily.ROUNDED,radius)
        .setBottomRightCorner(CornerFamily.ROUNDED,0)
        .setBottomLeftCorner(CornerFamily.ROUNDED,0)
        .build();
    MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
    shapeDrawable.setStroke(2.0f, ContextCompat.getColor(this,R.color.xxxx));
shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,R.color.xxx));    
    ViewCompat.setBackground(textview,shapeDrawable);

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

Create Draweable rounded_border.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <padding
        android:bottom="3dp"
        android:left="3dp"
        android:right="3dp"
        android:top="3dp" />

    <stroke
        android:width="1dp"
        android:color="#FFFFFF" />

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

    <size
        android:width="110dp"
        android:height="110dp" />
</shape>

Then Set this Drawable in view's background property

 <TextView
    android:id="@+id/tv_register"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tv_are_you_new_user"
    android:layout_margin="5dp"
    android:background="@drawable/rounded_border"
    android:fontFamily="@font/montserrat"
    android:gravity="center_horizontal"
    android:padding="10dp"
    android:text="Register"
    android:textAllCaps="true"
    android:textColor="@color/blue"
    android:textSize="15sp" />

Also helpful this solution to you: Click Me

Hope this may help you

InsaneCat
  • 2,115
  • 5
  • 21
  • 40