This light-weight library provides drawable with letter/text just like the Gmail/Contacts app. It can be used with existing/custom/network ImageView
classes.
MaterialTextDrawable requires Android Jellybean (API level 16) or higher.
To add this library in your project follow these steps:
-
Step 1. Add the JitPack repository to your root
build.gradle
file.allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
-
Step 2. Add the following dependency to your module
build.gradle
file.dependencies { implementation 'com.github.adwardstark:materialtextdrawable-for-android:{latest_version}' }
Simple use cases will look something like this:
MaterialTextDrawable.with(context)
.text("Hello")
.into(imageView)
You can use this library with any other image-loading library like Glide or Picasso in one easy step, first define an object of MaterialTextDrawable
like this:
val textDrawable = MaterialTextDrawable.with(context)
.text("Hello")
.get()
and then use this textDrawable
object with the image-loading-library.
Glide.with(context)
.load("https://some_url")
.placeholder(textDrawable)
.into(imageView)
You can configure the look/shape/size
of the text-drawable generated by MaterialTextDrawable using following configuration methods:
-
with(context: Context): Used to create the
MaterialTextDrawable
instance by passing theActivity
orFragment
context. -
text(text: String): This method is used to generate the initial character of the text-drawable, the text has to be passed using this method before calling the
get()
orinto()
methods else aNullPointerException
will be thrown. -
textSize(size: Int): Define the height and width of the text-drawable, by default it uses a size of
150
. ExampletextSize(200)
means200x200
. -
shape(shape: MaterialShape): Define the shape of drawable using the
MaterialShape
enum, by default it usesMaterialShape.CIRCLE
which returns a circular text-drawable. -
colorMode(mode: MaterialColorMode): Sets a random-color on drawable using the
MaterialColorMode
enum, by default it usesMaterialColorMode.MEDIUM
for random-color. -
get(): Returns a
BitmapDrawable
generated by the builder which can then be used to set the drawable on anImageView
. -
into(view: ImageView): Apply the text-drawable to an
ImageView
directly, no need to callget()
when using this method. This method needs to be called on theMainThread
else anIllegalArgumentException
will be thrown. -
into(view: ImageView, scale: ImageView.ScaleType): Apply the text-drawable to an
ImageView
directly along with scaling option of theImageView
class, no need to callget()
when using this method. This method needs to be called on theMainThread
else anIllegalArgumentException
will be thrown.
Following are the enums provided by the library:
- MaterialShape
CIRCLE
RECTANGLE
- MaterialColorMode
LIGHT // Material 900
MEDIUM // Material 700
DARK // Material 400
- AmosKorir for the AvatarImageGenerator implementation MaterialTextDrawable's foundation is based on.
- Everyone who has contributed or will contribute code and report/reported issues!
Apache 2.0. See the LICENSE file for details.