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.gradlefile.allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
-
Step 2. Add the following dependency to your module
build.gradlefile.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
MaterialTextDrawableinstance by passing theActivityorFragmentcontext. -
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 aNullPointerExceptionwill 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
MaterialShapeenum, by default it usesMaterialShape.CIRCLEwhich returns a circular text-drawable. -
colorMode(mode: MaterialColorMode): Sets a random-color on drawable using the
MaterialColorModeenum, by default it usesMaterialColorMode.MEDIUMfor random-color. -
get(): Returns a
BitmapDrawablegenerated by the builder which can then be used to set the drawable on anImageView. -
into(view: ImageView): Apply the text-drawable to an
ImageViewdirectly, no need to callget()when using this method. This method needs to be called on theMainThreadelse anIllegalArgumentExceptionwill be thrown. -
into(view: ImageView, scale: ImageView.ScaleType): Apply the text-drawable to an
ImageViewdirectly along with scaling option of theImageViewclass, no need to callget()when using this method. This method needs to be called on theMainThreadelse anIllegalArgumentExceptionwill 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.
