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.
Step-by-Step Implementation of Glow TextView
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: 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 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 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="40sp"
app:glowColor="@color/colorPrimary"
app:glowRadius="16dp" />
<!-- SeekBar to adjust the glow intensity -->
<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" />
<!-- SeekBar to change the glow color -->
<SeekBar
android:id="@+id/seekBarColor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/seekBarGlow"
android:layout_marginBottom="40dp"
android:max="100" />
</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: Main Activity of the Application
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
// 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.
// Getting Glow seekBar reference
SeekBar seekBarGlow = findViewById(R.id.seekBarGlow);
// Getting Color seekBar reference
SeekBar seekBarColor = findViewById(R.id.seekBarColor);
// Setting the max of seekBar to color length - 1
seekBarColor.setMax(colors.length - 1);
// Getting GlowTextView reference
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
// 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.
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
private 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 GlowTextView reference
GlowTextView glowTextView = findViewById(R.id.glowTextView);
// Setting default values
glowTextView.setGlowRadius(16); // Set initial glow radius
glowTextView.setGlowColor(colors[0]); // Set initial glow color
// Getting Glow seekBar reference
SeekBar seekBarGlow = findViewById(R.id.seekBarGlow);
seekBarGlow.setProgress(16); // Set initial progress
// Getting Color seekBar reference
SeekBar seekBarColor = findViewById(R.id.seekBarColor);
seekBarColor.setMax(colors.length - 1);
// 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 GlowTextView
glowTextView.setGlowRadius(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Optional: Do something when touch starts
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Optional: Do something when touch stops
}
});
// 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 GlowTextView
glowTextView.setGlowColor(colors[progress]);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Optional: Do something when touch starts
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Optional: Do something when touch stops
}
});
}
}
Output: