Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Creating contact thumbnail from name initials

I've an Android ListView containing some emails. Some of them have contact thumbnails while some of them don't. For unknown contact thumbnails, I want to create a Bitmap, containing the initials of the corresponding email id. How can I achieve that? I've seen some Android apps using this technique to display contact thumbnail like this:

enter image description here

To start with, I'm using the following code:

ImageView iv=(ImageView)findViewById(R.id.imageView1);
    TextView view = (TextView)findViewById(R.id.textView1);

    Bitmap b=Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    view.draw(c);
    iv.setImageBitmap(b);

Where the TextView holds a simple string. Unfortunately, it doesn't work. Can anyone please give me some pointers? Thanks a lot for your help!

like image 894
Vinit Shandilya Avatar asked May 15 '15 19:05

Vinit Shandilya


People also ask

What is quick contact badge in Android?

Android QuickContactBadge is a subclass of ImageView. It displays as a small badge which the user clicks on to quickly create a contact, such as adding a phone number to the contacts, adding an email to the contacts, etc.

How do you display a first letter of name in image layout if there is no image?

You should have a RelativeLayout where you have to place 2 View s: a TextView for a letter and an ImageView for an image(photo/avatar) which has to cover over the TextView completely. When there is no image in ImageView its transparent and thus you gonna see your TextView with the letter thru "image". Save this answer.


1 Answers

Well you're on the right track. Try it something like this.

ImageView iv=(ImageView)findViewById(R.id.imageView1);

Bitmap b=Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.drawText(initials,x,y,paint);
iv.setImageBitmap(b);

You should create your own logic to center the text. You could use some of the following helper method Paint.getTextSize() to find out how much space the text needs.

like image 200
Gent Ahmeti Avatar answered Oct 12 '22 22:10

Gent Ahmeti