Glow TextView in Android
By default Android doesn’t provide a simple way to add the glow to the text view or any other view, so to make our app more attractive and beautiful we can add the glow effect to our TextView. In order to do so, we can use many different external libraries. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Approach
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: Before going to the coding section first do some pre-task
Go to app -> res -> values -> colors.xml file and set the colors for the app.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < resources > < color name = "colorPrimary" >#0F9D58</ color > < color name = "colorPrimaryDark" >#0F9D58</ color > < color name = "colorAccent" >#05af9b</ color > < color name = "white" >#ffffff</ color > </ resources > |
Go to Gradle Scripts > build.gradle (Module: app) section and import the following dependencies and click the “sync Now” on the above pop-up.
// Adding glowtextview
implementation ‘com.riningan.widget:glowtextview:1.0’
Step 3: Designing the UI
In the activity_main.xml remove the default Text View and change the layout to Relative layout and add the GlowTextView and we also add 2 SeekBar to change the color and radius of the glow text view as shown below. Below is the code for the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:app = "http://schemas.android.com/apk/res-auto" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > <!-- Glow Text View --> < com.riningan.widget.GlowTextView android:id = "@+id/glowTextView" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerInParent = "true" android:padding = "40dp" android:text = "Hello world :)" android:textColor = "@android:color/white" android:textSize = "40dp" app:glowColor = "@color/Green" app:glowRadius = "16dp" /> <!-- simple seek bar to change the glow of the GlowTextView --> < SeekBar android:id = "@+id/seekBarGlow" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_alignParentBottom = "true" android:layout_marginBottom = "80dp" android:max = "100" /> <!-- simple seek bar to change the color of the GlowTextView --> < SeekBar android:id = "@+id/seekBarColor" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_alignParentBottom = "true" android:layout_marginBottom = "120dp" /> </ RelativeLayout > |
Properties:
- app:glowRadius: it is used to set the glow Radius [ default value 60f ]
- app:glowColor: it is used to set the glow Color [ default color value WHITE ]
Step 4: Coding Part
Open the MainActivity.java file and inside the class, we create an array of integer to store different colors ( which we use later to change the color of the GlowTextView ) as shown below
Java
// array of different colors int [] colors={Color.RED,Color.GREEN,Color.BLACK,Color.CYAN,Color.DKGRAY,Color.GRAY,Color.LTGRAY,Color.BLUE,Color.WHITE,Color.YELLOW,Color.MAGENTA}; |
Now inside the onCreate() get the reference of the GlowTextView and 2 SeekBar and set the max of the color SeekBar to the colors.length – 1 as shown below.
Java
// getting Glow seekBar reference SeekBar seekBarGlow =(SeekBar)findViewById(R.id.seekBarGlow); // getting Color seekBar reference SeekBar seekBarColor =(SeekBar)findViewById(R.id.seekBarColor); // setting the max of seekBar to color length -1 seekBarColor.setMax(colors.length- 1 ); // getting glowTextView reference GlowTextView glowTextView =(GlowTextView)findViewById(R.id.glowTextView); |
Now create a SeekBarChangeListener of both the SeekBar and inside the onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) set the GlowColor and GlowRadius value to the process as shown below
Java
// seekBar change listener for changing the glow radius seekBarGlow.setOnSeekBarChangeListener( new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // change the glow radius of the glow text view glowTextView.setGlowRadius(progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); // seekBar change listener for changing color seekBarColor.setOnSeekBarChangeListener( new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // change the glow color of the glow text view glowTextView.setGlowColor(colors[progress]); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); |
Below is the complete code of the MainActvity.java file.
Java
import android.graphics.Color; import android.os.Bundle; import android.widget.SeekBar; import androidx.appcompat.app.AppCompatActivity; import com.riningan.widget.GlowTextView; public class MainActivity extends AppCompatActivity { // array of different colors int [] colors = {Color.RED, Color.GREEN, Color.BLACK, Color.CYAN, Color.DKGRAY, Color.GRAY, Color.LTGRAY, Color.BLUE, Color.WHITE, Color.YELLOW, Color.MAGENTA}; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); // getting Glow seekBar reference SeekBar seekBarGlow = (SeekBar) findViewById(R.id.seekBarGlow); // getting Color seekBar reference SeekBar seekBarColor = (SeekBar) findViewById(R.id.seekBarColor); // setting the max of seekBar to color length -1 seekBarColor.setMax(colors.length - 1 ); // getting glowTextView reference GlowTextView glowTextView = (GlowTextView) findViewById(R.id.glowTextView); // seekBar change listener for changing the glow radius seekBarGlow.setOnSeekBarChangeListener( new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // change the glow radius of the glow text view glowTextView.setGlowRadius(progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); // seekBar change listener for changing color seekBarColor.setOnSeekBarChangeListener( new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // change the glow color of the glow text view glowTextView.setGlowColor(colors[progress]); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); } } |
Output: