Create set multiple color border around TextView in android

How to add different color at Left,Right,Top, Bottom, and background of TextView programmatically.

Textview with multiple color border combination can be easily created using LayerDrawable. With the use of LayerDrawable we can set different type of colors at each position of textview. So here is the complete step by step tutorial for Create set multiple color border around TextView in android.

android-project-download-code-button

How to Create set multiple color border around TextView in android.

Code for MainActivity.java file.

 package com.multiplecolorbordertextview_android_examples.com;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

 TextView textview;
 
 //Creating Multiple ColorDrawable.
 ColorDrawable left = new ColorDrawable(Color.BLUE);
 ColorDrawable top = new ColorDrawable(Color.CYAN);
 ColorDrawable right = new ColorDrawable(Color.RED);
 ColorDrawable bottom = new ColorDrawable(Color.GREEN);
 ColorDrawable background = new ColorDrawable(Color.WHITE);
 
 Drawable[] DrawableArray = new Drawable[]{
 left,
 top,
 right,
 bottom,
 background
 };
 
 LayerDrawable layerdrawable;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 textview = (TextView)findViewById(R.id.textView1);
 
 layerdrawable = new LayerDrawable(DrawableArray);
 
 layerdrawable.setLayerInset(0,0,0,18,0);

 layerdrawable.setLayerInset(1,18,0,0,18);

 layerdrawable.setLayerInset(2,18,18,0,0);

 layerdrawable.setLayerInset(3,18,18,18,0);

 layerdrawable.setLayerInset(4,18,18,18,18);
 
 textview.setBackgroundDrawable(layerdrawable);
 
 textview.setPadding(30, 30, 30, 30);
 }

}

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.multiplecolorbordertextview_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="TextView Sample Text"
 android:gravity="center"
 android:textAppearance="?android:attr/textAppearanceLarge"
 />

</RelativeLayout>

Screenshot:

Create set multiple color border around TextView in android

Click here to download Create set multiple color border around TextView in android project with source code.

Leave a Reply

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