How to Put a Border Around an Android Textview

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

How to put border around textview?

I think you're wrong in border.xml file.

Try doing a test with this:

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

if it works it means that something wrong in the syntax. (maybe badly used the selector)

Android : adding border around textview

You can try this layout, its reflecting as per your requirement

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:padding="15dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Label 1: Value 1"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Label 2: Value 2"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Label 3: Value 3"/>

</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text=" Details "
android:layout_marginLeft="15dp"
android:background="#ffffff"
android:textSize="17sp" />

</RelativeLayout>

xml of border.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="2dp"
android:color="#cdcdcd" />
</shape>

Hope this helps you somehow.

Is there an easy way to add a border to the top and bottom of an Android View?

In android 2.2 you could do the following.

Create an xml drawable such as /res/drawable/textlines.xml and assign this as a TextView's background property.

<TextView
android:text="My text with lines above and below"
android:background="@drawable/textlines"
/>

/res/drawable/textlines.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="#FF000000" />
<solid android:color="#FFDDDDDD" />

</shape>
</item>

<item android:top="1dp" android:bottom="1dp">
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="#FFDDDDDD" />
<solid android:color="#00000000" />
</shape>
</item>

</layer-list>

The down side to this is that you have to specify an opaque background colour, as transparencies won't work. (At least i thought they did but i was mistaken). In the above example you can see that the solid colour of the first shape #FFdddddd is copied in the 2nd shapes stroke colour.

How do I set the border colour of my TextView programmatically?

You are changing the color of the drawable that you load but not the one used in the TextView. Add the following line to your code to set the background drawable in the TextView:

findViewById(R.id.textView1).setBackground(layerDrawable);

You can also get the background for the TextView directly with

LayerDrawable layerDrawable = (LayerDrawable) findViewById(R.id.textView1).getBackground();

Here is a set of code to change the background and the stroke color:

LayerDrawable layerDrawable = (LayerDrawable) findViewById(R.id.textView1).getBackground();
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable
.findDrawableByLayerId(R.id.textbox_shape);
// Change background color
gradientDrawable.setColor(Color.parseColor("#DA850B"));
// Change stroke color. (Assumes 5px stroke width.)
gradientDrawable.setStroke(5, Color.parseColor("#FF0000"));

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);


Related Topics



Leave a reply



Submit