Display a contact name with an initial letter in a circle

I need to display the initial letter of the name of the contact inside the circle, as in contacts with candy. I did not find a solution.

The circle should be different for each user.

+6
source share
2 answers

You can follow this. If you want to save time, you can use this library.

Edit

The link may be invalid at any time. So I want to add the details below.

repositories{ maven { url 'http://dl.bintray.com/amulyakhare/maven' } } dependencies { compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1' } 

You can add which methodology you follow.

Basically you need to add one ImageView with the same height and width and add this TextDrawable as the background of your view.

 TextDrawable drawable = TextDrawable.builder() .buildRect("A", Color.RED); ImageView image = (ImageView) findViewById(R.id.image_view); image.setImageDrawable(drawable); 

The library supports circle , square and rectangle drawings.

+25
source

I have not seen any library for this, but my solution is to draw a circle at runtime in a predefined layout in your xml using this: How to draw a circle on canvas in Android?

Then, in your list adapter, select the first letter of each line of the name using the substring (0,1), and with setText () you can set the textView list item to the first letter. If you need source code, let me know to put it for you.

UPDATE: Recently, I used a very simple approach for this in one of my applications, you should make such a layout in the layout folder:

  <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rlWeekDay" android:layout_width="45dp" android:layout_height="45dp" android:layout_gravity="right" android:layout_margin="3dp" android:background="@drawable/circle_shape"> <TextView android:id="@+id/tvWeekDayFirstLetter" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text=" E " android:textStyle="bold" android:textColor="@color/colorPrimary" android:textSize="20sp" /> </RelativeLayout> 

And your form in the dropdown is like circle_shape.xml:

  <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="oval" android:dither="true"> <corners android:bottomRightRadius="100dp" android:bottomLeftRadius="100dp" android:topLeftRadius="100dp" android:topRightRadius="100dp"/> <solid android:color="#ccc" /> </shape> </item> <item android:bottom="3dp"> <shape android:shape="oval" android:dither="true"> <solid android:color="@color/white"/> <!-- this one is ths color of the Rounded Button --> <corners android:bottomRightRadius="100dp" android:bottomLeftRadius="100dp" android:topLeftRadius="100dp" android:topRightRadius="100dp"/> </shape> </item> </layer-list> 

Then in your View or recyclerview list, use this as an inflate item. Like this enter image description here

+3
source

All Articles