Android - Way to Appear Bordered Text on the Textview

Android textview outline text

You can put a shadow behind the text, which can often help readability. Try experimenting with 50% translucent black shadows on your green text. Details on how to do this are over here: Android - shadow on text?

To really add a stroke around the text, you need to do something a bit more involved, like this:
How do you draw text with a border on a MapView in Android?

How to outline a TextView?

1) create your textview object extends TextView

public class YourTextView extends TextView { .........

2) Do this on its draw method

@Override
public void draw(Canvas canvas) {
for (int i = 0; i < 5; i++) {
super.draw(canvas);
}
}

3) set textview's xml side as below

android:shadowColor="@color/white"
android:shadowRadius="5"

How to make border for text in TextView?

public class CoustomTextView extends TextView {

private float strokeWidth;
private Integer strokeColor;
private Paint.Join strokeJoin;
private float strokeMiter;

public CoustomTextView(Context context) {
super(context);
init(null);
}

public CoustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}

public CoustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}

public void init(AttributeSet attrs) {

if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CoustomTextView);

if (a.hasValue(R.styleable.CoustomTextView_strokeColor)) {
float strokeWidth = a.getDimensionPixelSize(R.styleable.CoustomTextView_strokeWidth, 1);
int strokeColor = a.getColor(R.styleable.CoustomTextView_strokeColor, 0xff000000);
float strokeMiter = a.getDimensionPixelSize(R.styleable.CoustomTextView_strokeMiter, 10);
Paint.Join strokeJoin = null;
switch (a.getInt(R.styleable.CoustomTextView_strokeJoinStyle, 0)) {
case (0):
strokeJoin = Paint.Join.MITER;
break;
case (1):
strokeJoin = Paint.Join.BEVEL;
break;
case (2):
strokeJoin = Paint.Join.ROUND;
break;
}
this.setStroke(strokeWidth, strokeColor, strokeJoin, strokeMiter);
}
}
}

public void setStroke(float width, int color, Paint.Join join, float miter) {
strokeWidth = width;
strokeColor = color;
strokeJoin = join;
strokeMiter = miter;
}

@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);

int restoreColor = this.getCurrentTextColor();
if (strokeColor != null) {
TextPaint paint = this.getPaint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(strokeJoin);
paint.setStrokeMiter(strokeMiter);
this.setTextColor(strokeColor);
paint.setStrokeWidth(strokeWidth);
super.onDraw(canvas);
paint.setStyle(Paint.Style.FILL);
this.setTextColor(restoreColor);
}
}
}

Usage:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

CoustomTextView coustomTextView = (CoustomTextView) findViewById(R.id.pager_title);
}
}

Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@mipmap/background">

<pk.sohail.gallerytest.activity.CoustomTextView
android:id="@+id/pager_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="@string/txt_title_photo_gallery"
android:textColor="@color/white"
android:textSize="30dp"
android:textStyle="bold"
app:outerShadowRadius="10dp"
app:strokeColor="@color/title_text_color"
app:strokeJoinStyle="miter"
app:strokeWidth="2dp" />

</RelativeLayout>

attars:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="CoustomTextView">

<attr name="outerShadowRadius" format="dimension" />
<attr name="strokeWidth" format="dimension" />
<attr name="strokeMiter" format="dimension" />
<attr name="strokeColor" format="color" />
<attr name="strokeJoinStyle">
<enum name="miter" value="0" />
<enum name="bevel" value="1" />
<enum name="round" value="2" />
</attr>
</declare-styleable>

</resources>

Programmatically:

CoustomTextView mtxt_name = (CoustomTextView) findViewById(R.id.pager_title);

Use setStroke(); method before calling setText();

Android textview - possible to border each letter (like a table border)

The code provided will lead to a view as shown in the picture. If it is what you want you can follow along or you can customize it to what you want!

The code provided will lead to a view as shown in the picture. If it is what you want you can follow along or you can customize it to what you want!:

First you need to make a bordering xml drawable. This will be used as a background to each part of your text. And around each letter or number in your text.
Make a resource file inside drawable folder lets call it border.xml (full name res/drawable/border.xml).Copy and paste this inside it:

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#00ffffff" />
<stroke android:width="1dip" android:color="#4fa5d5"/>
<padding android:left="4dp" android:right="4dp"
android:bottom="1dp" android:top="1dp"/>
</shape>

This will bring a bordering effect on your text, You can also edit color and padding to fit your needs.
The we need to create a layout with TextView as the root view. So in your layout folder make a file lets call it border_text_view.xml(full name res/layout/border_text_view.xml). Copy and Paste the code below in it:

    <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border"/>

Note: The background is set to the previous declared border.xml from the drawable folder.

Then in your layout where you plan to display the bordering text lets say its activity_main (it can be any layout you want to display the view).

Add this to that layout:

    .......
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/layout_border"/>
.......

Then in your layout's Java Class(Activity or Fragment) get a reference to the Linear layout added above and call the method as follows:

    LinearLayout linearLayout=(LinearLayout)findViewById(R.id.layout_border);  
addBorderedView(this, linearLayout, "8000LP");

Now make the method addBorderedView as follows:

    private void addBorderedView(Context context, LinearLayout layout, String string_to_display) {
String[] array = string_to_display.split("");
for (int i = 1; i < array.length; i++) {
TextView borderedTextView = (TextView) LayoutInflater.from(context).inflate(R.layout.border_text_view, null);
borderedTextView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
borderedTextView.setGravity(Gravity.CENTER);
borderedTextView.setText(array[i]);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) borderedTextView.getLayoutParams();
params.setMargins(2, 0, 2, 0); //substitute parameters for left, top, right, bottom
borderedTextView.setLayoutParams(params);
layout.addView(borderedTextView);
}
}

To improve performance you may need to make a view holder if you plan to display large sentences this way, if you are not then you are good to go!

Android: how to configure the textView outline black border in xml layout properly

For outline to characters you can use below code

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/test1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:text="TEST"
android:textColor="#000000"
android:textSize="25sp" />
<TextView
android:id="@+id/test2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:text="TEST"
android:textColor="#FFC107"
android:textSize="25sp" />
</FrameLayout>

In .java file put these lines to give border to TextView characters

TextView textViewShadow = (TextView) view.findViewById(R.id.test1);
textViewShadow.getPaint().setStrokeWidth(5);
textViewShadow.getPaint().setStyle(Paint.Style.STROKE);

How to put a border around an Android TextView?

You can set a shape drawable (a rectangle) as background for the view.

<TextView android:text="Some text" android:background="@drawable/back"/>

And rectangle drawable back.xml (put into res/drawable folder):

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="@android:color/white" />
<stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>

You can use @android:color/transparent for the solid color to have a transparent background.
You can also use padding to separate the text from the border.
for more information see: http://developer.android.com/guide/topics/resources/drawable-resource.html



Related Topics



Leave a reply



Submit