16

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.

Rahul Gopi
  • 414
  • 1
  • 16
  • 31
Sudhanshu
  • 275
  • 1
  • 2
  • 12
  • what you had tried so far ? – King of Masses Aug 07 '15 at 07:32
  • I was trying to make it custom but looking for a guideline in material design as the app is being made for lollipop. – Sudhanshu Aug 07 '15 at 07:44
  • I'm voting to close this question as off-topic because we're not a code writing service. You should make a best effort attempt to solve your problem yourself, then ask a question when you have a specific issue. – James Webster Aug 07 '15 at 08:19
  • All of these are useful, but the missing component is that they are supposed to show an avatar is its available, and the letter if its not. – Brill Pappin Sep 18 '20 at 14:57

4 Answers4

35

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.

Community
  • 1
  • 1
ImMathan
  • 3,911
  • 4
  • 29
  • 45
  • 1
    Although this is potentially quite a good answer, it is essentially a "link only" answer. You should include some information from your links so that if the information behind the links is ever changed/lost, the answer still makes sense. See [this link](http://meta.stackexchange.com/questions/225370/your-answer-is-in-another-castle-when-is-an-answer-not-an-answer) – James Webster Aug 07 '15 at 08:21
  • Thanks for your suggestions @JamesWebster. – ImMathan Aug 07 '15 at 09:38
  • @ImMathan this is a much better answer. I've reversed my downvote and upvoted – James Webster Aug 07 '15 at 09:39
  • @JamesWebster Great :) – ImMathan Aug 07 '15 at 09:40
  • Thank you bos :) It Helped me, actually i had very less time to do that so it saved my time. Thanks again :) – Sudhanshu Aug 10 '15 at 08:06
  • @Sudhanshu Good to know. If this is the answer you can mark it as an answer :) – ImMathan Aug 10 '15 at 08:11
  • @Sudhanshu and also try to follow this stackoverflow model which will help you ask better next time. http://stackoverflow.com/help/dont-ask – ImMathan Aug 10 '15 at 08:14
  • Thanks, but if somebody don't want to use library, you can create bitmap - http://stackoverflow.com/a/29549014/3467701 i prefer bitmap) – Tigran Sarkisian Nov 10 '16 at 08:07
  • Thank you :) you saved my day. – Fadwa_lmh May 10 '17 at 12:10
  • Adding library for a small feature is not a good idea.. it will increase your app size – HarshitG Oct 07 '20 at 05:08
4

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

Community
  • 1
  • 1
Elmira Frhn
  • 320
  • 4
  • 14
0

Simple and small code to do this -

<com.google.android.material.button.MaterialButton
        android:id="@+id/"
        android:layout_width="40dp"
        android:layout_height="50dp"
        android:text="A"
        android:clickable="false"
        app:backgroundTint="@color/Blue200"
        android:padding="0dp"
        app:cornerRadius="50dp"
        />
HarshitG
  • 2,677
  • 3
  • 16
  • 13
0

By using a Material Card View and a Textview, it would be easy to create such a view.

 <com.google.android.material.card.MaterialCardView
        android:layout_width="50dp"
        android:layout_height="50dp"
        app:cardBackgroundColor="@color/red"
        app:cardCornerRadius="50dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/initialTv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="AB" />

    </com.google.android.material.card.MaterialCardView>
Chintan Parmar
  • 2,375
  • 2
  • 12
  • 22