I am trying to make a notification count indicator, where a TextView
would be showing the number of notifications present. The TextView would be placed on top of an ImageView
that has a bell icon. The problem is, when I set the background of the TextView using a drawable resource, I get a round shape for numbers with lesser digits, say 1. But when larger numbers are being displayed in the TextView it won't have a round shape anymore. How would I make the TextView have a round-shaped with a colored background, no matter the text and its length?
When it displays the number "1", it is almost round.
But when I put the number "99", it has rounded edges only. It's not fully round.
This is my code for the drawable resource I used for the TextView:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FF173C" />
<size android:height="10dp" android:width="10dp"/>
<corners
android:bottomLeftRadius="2000dp"
android:bottomRightRadius="2000dp"
android:topLeftRadius="2000dp"
android:topRightRadius="2000dp" />
<padding android:right="5dp" android:left="5dp"/>
</shape>
Then I used it in TextView like this:
<TextView
android:id="@+id/notification_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:background="@drawable/bg_notification"
android:textColor="@android:color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
You can wrap_content of the TextView
width and constraint the height so that it can equal to the width using app:layout_constraintDimensionRatio = "1"
But an issue will come up when the text is short (1 character), as the width now will be short; so a part of the text will be hidden, you can fix this by constraining a min width/height using app:layout_constraintWidth_min
& app:layout_constraintHeight_min
respectively.
<TextView
android:id="@+id/notification_count"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="@drawable/bg_notification"
android:gravity="center"
android:padding="8dp"
android:text="3"
app:layout_constraintWidth_min="40dp"
app:layout_constraintHeight_min="40dp"
app:layout_constraintDimensionRatio="1"
android:textColor="@android:color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Preview: