Monday, July 15, 2013

Creating Android Custom TextView Rounded Corner Programmatically

To create a rounded corner custom TextView in your android application, follow the below steps.


Step-1: Create a custom xml file named "custom_shape.xml" under /res/drawable folder.

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
 android:shape="rectangle"> 
 <gradient  
     android:angle="270"/>
<corners 
     android:bottomRightRadius="7dp" 
     android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" 
     android:topRightRadius="7dp"/> 
  <solid android:color="@android:color/transparent"/>  
  <stroke 
      android:color="@android:color/black"
      android:width="5sp"/> 
</shape> 

Step-2: Create a custom TextView class "TextViewOutLined.java"

public class TextViewOutLined extends TextView
{
public static int textSize = 30;  //The size of the text can be changed later
public TextViewOutLined(Context context) 
{  
super(context); 
init(); 
}
@Override   
 protected void onDraw(Canvas canvas) {  
super.onDraw(canvas);                 
this.setBackgroundResource(R.drawable.custom_shape);    
} 
private void init(){  
setTextSize(textSize);  
setPadding(5, 5, 5, 5);  //This will set the padding, and by increasing decreasing this 
// parameters the text padding can be changed 
}
}

Step-3: Use the class TextViewOutLined in your android program as:
private final TextViewOutLined tv = new TextViewOutLined(context);
tv.setText("www.appsoid.in");

Thank you.
bodhisattwa

No comments:

Post a Comment