How to Create Text Stickers in Android?
Not only a picture is worth a thousand words but also, they are cooler than monotonous text. In this article, we will learn how to create stickers from the TextView. This will be helpful if you are creating an app in which you want to add some text overlay functionality, for example, an image editing app. Also, we can see a very good implementation of this in the Hike App.
How Does it Work?
The main concept we are using here is that we are converting the TextView into Bitmap and saving that bitmap in the phone storage. Now, we can do “n” number of manipulations in that TextView, like we can change the color, font, size, style, the appearance of the, etc of the TextView. Any changes we make in the TextView reflect in the sticker(bitmap) formed. Keeping this idea in mind, let’s begin creating the App. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Adding a dependency to the build.gradle file
We will be using a library for picking up the color at runtime and changing the color of TextView accordingly. So add this dependency in build.gradle file.
Step 3: Adding font files
To make our sticker more beautiful we will add some font to the TextView. To do so we will require the font files in .ttf or .otf format. You can download any font file you like from the internet. Now once you have downloaded the files, go to the app > res > right-click > New > Folder > Font Folder and create a new Android Resources folder of the font in android studio and place the font files in that folder.
We will be using six font files for example and you can download these files from here.
Step 4: Working with the activity_main.xml file
Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file.
Step 5: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Output: Run on Physical Device
Note about auto-scaling in compatibility mode: When auto-scaling is not enabled, this method will create a bitmap of the same size as this view. You should avoid calling this method when hardware acceleration is enabled. If you do not need the drawing cache bitmap, calling this method will increase memory usage and cause the view to be rendered in software once, thus negatively impacting performance.
Important Methods used in this Example
- buildDrawingCache: Forces the drawing cache to be built if the drawing cache is invalid.
- destroyDrawingCache: Frees the resources used by the drawing cache. If you call buildDrawingCache() manually without calling setDrawingCacheEnabled(true), you should clean up the cache with this method afterward.
- getDrawingCache: Returns the bitmap in which this view drawing is cached. The returned bitmap is null when caching is disabled. If caching is enabled and the cache is not ready, this method will create it. There is an overloaded function of this with a boolean parameter which indicates whether the generated bitmap should be scaled based on the current density of the screen when the application is in compatibility mode. The zero parameter method works the same as getDrawingCache(false).
- setDrawingCacheEnabled: Enables or disables the drawing cache. When the drawing cache is enabled, the next call to getDrawingCache() or buildDrawingCache() will draw the viewer in a bitmap.
- You can add the functionality of increasing or decreasing the TextView size at runtime.
- You can add a canvas to your app, this will allow dragging, stretching, rotating the TextView which would be more interactive.
- You can also try to implement this in a Chat App, where the user can generate a sticker from the text typed.
- Try typing in different languages and create the sticker.
1. We don’t require read write permissions here because the stickers or the bitmaps which we are saving in phone storage are owned by our own app.
2. From Android 10, method for accessing files from device storage has been changed. If you use earlier methods you might get absurd results.
3. If you call buildDrawingCache() manually without calling setDrawingCacheEnabled(true), you should cleanup the cache by calling destroyDrawingCache() afterwards.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.