Create gradient background for textview android programmatically

Creating gradient effect drawable at app run time and set as textview background on button click dynamically.

In this tutorial we are creating a gradient effect inside activity run time using GradientDrawable method and setting up that gradient as TextView background. So here is the complete step by step tutorial for Create gradient background for textview android programmatically.

Note: Please make sure that you have using emulator or your real device to test this project more then API level 16, because some methods of GradientDrawable cannot run below api 16. That methods are setColors() , setBackground() .

android-project-download-code-button

How to Create gradient background for textview android programmatically.

Code for MainActivity.java file.

 package com.gradientbackgroundtextview_android_examples.com;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

@SuppressLint("NewApi") public class MainActivity extends Activity {

 Button Effect;
 TextView textview;
 GradientDrawable gradientdrawable;
 int colors[] = {
 Color.BLUE,
 Color.MAGENTA,
 Color.GRAY };

 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 Effect = (Button)findViewById(R.id.button1);
 
 textview = (TextView)findViewById(R.id.textView1);
 
 gradientdrawable = new GradientDrawable();
 
 gradientdrawable.setShape(GradientDrawable.RECTANGLE);
 
 gradientdrawable.setColors(colors);
 
 gradientdrawable.setStroke(3, Color.BLACK);
 
 gradientdrawable.setCornerRadius(25.0f);
 
 Effect.setOnClickListener(new View.OnClickListener() {
 
 @SuppressLint("NewApi") @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 textview.setBackground(gradientdrawable);
 }
 });
 }
}

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.gradientbackgroundtextview_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="250dp"
 android:layout_height="100dp"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="170dp"
 android:text="TextView Text"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

 <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="41dp"
 android:text="Create gradient background for textview android programmatically" />

</RelativeLayout>

Screenshots:

textview-background

Create gradient background for textview android programmatically

Click here to download Create gradient background for textview android programmatically project.

2 Comments

  1. showing error in:-
    textview.setBackground(gradientdrawable);

Leave a Reply

Your email address will not be published. Required fields are marked *