I would like to display a count inside a red circle that displays the news entries for that item. Like email count, know?
Is there anyway that i can do it or the best way is to create a new TextView and implement it?
Thank in advance.
Use a TextView with a background image like so:
<TextView
android:id="@+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/mybackground"/>
The background image is preferably a 9-patch drawable (http://developer.android.com/tools/help/draw9patch.html) or it could be something like this:
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FF0000"/>
<corners android:radius="4dp"/>
</shape>
This one would draw a red rectangle with rounded corners but you can create any shape you like (http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape)
If you don't want the text to be centered (like at the right bottom corner) you have two options:
Option 1
Make the TextView larger than it would be when just wrapping around the text (which is the default). So either use match_parent for android:layout_width and android:layout_height or set an absolute size (in dp). Also use the gravity parameter to position it at the right bottom corner:
<TextView
android:id="@+id/count"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="right|bottom"
android:background="@drawable/mybackground"/>
Option 2
Use padding to move the text in the direction you need it to move:
<TextView
android:id="@+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:background="@drawable/mybackground"/>
Using two views on top of each other is of course possible too but inefficient. You'd need at least an extra ViewGroup (e.g. FrameLayout) around the TextView (for the counter) and the ImageView (for the background) to make sure they are positioned above each other, meaning you'd replace one TextView by three other views. Also you'd still have to set the size of the enclosing ViewGroup to something bigger than the TextView so there's nothing gained with that solution.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments