Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set border and background color of textView

I have a TextView defined in XML and i would like to set background color AND border to it. Problem i have is that in XML i already use android:background for setting border resource, so i can't use it once again for background color. Can someone please guide me to right direction?

Border.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
   <solid android:color="#ffffff" />
   <stroke android:width="1dip" android:color="#7F000000"/>
</shape>

TextView

<TextView
        android:id="@+id/editor_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@drawable/title_border"         
        android:padding="5dp"
        android:text="@string/editor_title"               
        android:textAppearance="?android:attr/textAppearanceMedium" />
like image 398
vaske Avatar asked Mar 13 '13 13:03

vaske


People also ask

How do I add background color to TextView?

To change the background color of TextView widget, set the background attribute with required Color value. You can specify color in rgb , argb , rrggbb , or aarrggbb formats. The background color is applied along the width and height of the TextView.

How do you put a border color on android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code we have taken one text view with background as border so we need to create a file in drawable as boarder.

How do you add a border in TextView?

To add a border to Android TextView we need to create an XML containing shape as a rectangle file under the drawable's folder and set it as background to the TextView. <stroke> tag is used to set the border width and color.


1 Answers

You should create a XML drawable for this, which can then be set as your single background. Here is what you are wanting (a rectangle with a different color border - replace gradient with if you don't want that).

This will go in your 'drawable' folder:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke android:width="3dp" android:color="@color/blue_button_border" />
    <gradient
      android:startColor="@color/gradient_end"
      android:endColor="@color/gradient_start"
      android:angle="-90" /> 
</shape>
like image 144
Booger Avatar answered Sep 19 '22 01:09

Booger