Set textView text Shadow effect programmatically android

Convert Normal text style to drop shadow blur radius effect on button click in android.

Shadow effect text can be easily generate through MainActivity.java programming file through setShadowLayer() function. So here is the complete step by step tutorial for how to Set textView text Shadow effect programmatically android.

How to Set textView text Shadow effect programmatically android.

android-project-download-code-button

setShadowLayer() function demands float radius, float dx, float dy and int color parameters. This function gives the text a shadow of the specified blur radius and color, the specified distance from its drawn position.

setshadowlayer

List of Parameters that pass into this function :

  • Radius
  • dx
  • dy
  • color

Code for MainActivity.java file.

package com.textviewshadoweffectprogrammatically_android_examples.com;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {

 TextView txt1;
 Button bt1;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 txt1 = (TextView)findViewById(R.id.textView1);
 bt1 = (Button)findViewById(R.id.button1);
 
 bt1.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 txt1.setShadowLayer(1.3f, 4.0f, 4.0f, Color.parseColor("#fdab52"));
 
 }
 });
 }
}

Code for activity_main.xml layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.textviewshadoweffectprogrammatically_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Sample TextView Text"
 android:textStyle="bold"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="24dp"
 android:text="Click Here to Set Shadow effect on TextView" />

</RelativeLayout>

Screenshots:

without effect

Set textView text Shadow effect programmatically android

Click Here to Download Set textView text Shadow effect programmatically android project.