2020.09.01.

Xamarin.Android – Rounded corner of View

By bence960206

Android don’t have rounded corner property, so we have to define a shape in an xml and we have to set this xml as a background of a View.

Let’s define our rounded corner in the Resource/drawable folder: rounded.xml. If we want a different radius or different color, just change the android:radius or android:color property.

<?xml version="1.0" encoding="utf-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape android:shape="rectangle"
           android:alpha="0.2">
      <solid android:color="#F4F4F7"/>
      <corners android:radius="20dp"/>
    </shape>
  </item>
</layer-list>

Next, we have to set this as a background of a View. I defined a LinearLayout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">
    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:gravity="center"
        android:background="@drawable/rounded">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:text="Some text"/>
    </LinearLayout>
</LinearLayout>

And that’s all. We have a rounded LinearLayout in the middle of the page.