Android First Letter Avatar Image Generator โœ…

Korir Amos
2 min readJan 26, 2020

Generating the placeholder image for profile name. Takes the first character of a name.

( AvatarImageGenerator Android Library) ๐Ÿš€ Has over 1500 downloads in Bintray ๐Ÿšฃ

I like playing on the canvas with the canvas, painting here and there. ๐ŸŒ… (I will soon share an article on google maps with canvas) ๐Ÿ˜ƒ

With android canvas, one can create beautiful custom view either to increase the visual look or to add a functionality. Recently i was working on a project and I needed an avatar image with the first letter .This is a common feature found in contacts applications and email applications. I had never done this before. To achieve this, I needed a way to generate one or use a custom view, there is an option of Avatarview (android library), but this requires one to replace an Imageview with Avatarview, but i didnโ€™t want to do that.

I wanted a solution that would work with Imageview using Picasso or Glide.
Both Picasso and Glide are the most used image library and they both support placeholder images. This is where the avatar generator would come in.

This is how I implemented it,

To achieve this, we need three classes

Random Colour picker classes

The above class is used to choose the colour randomly
Constants class , just keeping the Constants clean

The final AvatarGenerator class , where the actual drawing on the canvas happens, I hope it does not look scary, It is step by step , creating all the resources needed and finally painting on the canvas. The avatarImage function returns an BitmapDrawable that can be used in an imageView.

With the above classes we can now set an avatar to an ImageView using Picasso and Glide.

Shows how Avatar generator can be use

The avatar generator supports two shapes, a circle and square. An avatar is created by passing the context, width , shape and the name just as in the above examples.

If you need a quick solution, then you can use the library I created basing on the above classes, AvatarImageGenerator . You are welcome to contribute. ๐Ÿ˜ƒ

--

--

Korir Amos

Senior Android Engineer- Mobile Application Developer