Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing contact name with initial letter in circle

Tags:

android

I need to display the contact name initial letter inside circle like in lollipop contacts. I didn't find any solution.

The circle should different colour for each user.

like image 526
Sudhanshu Avatar asked Aug 07 '15 07:08

Sudhanshu


2 Answers

You can follow this. If you wanna save time you can use this library.

Edit

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

repositories{
  maven {
    url 'http://dl.bintray.com/amulyakhare/maven'
  }
}

dependencies {
  compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
}    

You can add which ever methodology you are following.

Basically you have to add one ImageView with same height and width and add this TextDrawable as a 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 has support for circle,square and rectangle drawables.

like image 172
ImMathan Avatar answered Nov 17 '22 10:11

ImMathan


I didn't see any library for this but my solution is draw a circle in run time in a predefined layout in your xml use this : How to draw circle by canvas in Android?

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

UPDATE: I recently used very easy approach for this in one of my apps, you should make a layout like this in your 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 shape in drawable folder as 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 listView or recyclerview use this as the item to inflate.like this enter image description here

like image 4
Elmira Frhn Avatar answered Nov 17 '22 09:11

Elmira Frhn