4

I have a TextView and i want to put a different color on the top border (let's say white) I try something like this but doesn't work (put white border to all margins - left,right,top,bottom)

<TextView 
android:layout_height="50dip" 
android:text="@string/total" 
android:background="@drawable/border_top_textview"
android:textColor="#FFFFFF" 
android:id="@+id/totalCash" 
android:layout_width="match_parent" 
android:gravity="center_vertical" 
android:textSize="26dip" 
android:layout_weight="0.3" 
android:textStyle="bold"></TextView>

and the border_top_textview.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
  <shape android:shape="rectangle">
       <stroke android:width="2dp" android:height="2dp" android:color="#FFFFFF" />

        <solid android:color="#000000" />
            <padding android:top="1dp" android:bottom="1dp" />
   </shape></item></layer-list>
tinti
  • 1,455
  • 8
  • 23
  • 39
  • Have a Look at this post [Is there an easy way to add a border to the top and bottom of an Android View][1] [1]: http://stackoverflow.com/questions/1598119/is-there-an-easy-way-to-add-a-border-to-the-top-and-bottom-of-an-android-view – mH16 Nov 30 '11 at 10:45
  • I look at the post you mention ... still same problem, pt border around all textview, not only on top – tinti Nov 30 '11 at 10:50
  • http://stackoverflow.com/questions/3263611/border-for-an-image-view-in-android – mH16 Nov 30 '11 at 10:55
  • have u checked this , this is there in the firs link – mH16 Nov 30 '11 at 10:55
  • Actually someone test the code and then add it here? Because is easy to copy-paste from some older topics that doesn't work as i want it. Anyway thanks for the help! – tinti Nov 30 '11 at 11:35
  • The best solution is here http://stackoverflow.com/a/23754336/1533285 using values for every border angle, works perfect – jlbofh Sep 08 '14 at 01:37

3 Answers3

4

Simplest way is to use a 9 patch image as background with desired border.

or

try this, may be it will help you ,it will create a LINE at the TOP ,which will look like a border:

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

        <padding android:top="25dp" />

    </shape>
</item>
</layer-list>
Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
Nibha Jain
  • 7,742
  • 11
  • 47
  • 71
  • I don't want to use a 9 patch image ... i think is must be a solution using only xml or java class. Regarding you code is a "hack" you put a line in the middle of the textview and place the text just below this line. Is not really what i want. – tinti Nov 30 '11 at 12:14
  • tinti , i think this will do the job for you. – mH16 Nov 30 '11 at 12:17
2

Here you can put this lines within the shape tag,

<stroke android:width="4dp" android:color="#FFFFFF" />
<padding android:left="0dp" android:top="7dp"
android:right="0dp" android:bottom="0dp" />

put top padding atleast 5dp and make others 0dp

Neetesh
  • 917
  • 1
  • 6
  • 16
2
public class MyTextView extends TextView {

      public MyTextView(Context context, AttributeSet attrs, int defStyle) {
           super(context, attrs, defStyle);
      }
      public MyTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
      }
      public MyTextView(Context context) {
            super(context);
      }
      @Override
      protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Rect rect = new Rect();
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(Color.WHITE);
            paint.setStrokeWidth(3);
            getLocalVisibleRect(rect);
           canvas.drawRect(rect, paint);       
      }
}

xml File:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical" 
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
        <samples.test.MyTextView 
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="@string/hello" />
</LinearLayout>

See here

Try this tricky way to use an image as a border and use this code , it will work fine.

enter code here

  <TextView
    android:id="@+id/text1" 
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:drawableTop="@drawable/header1" 
    android:gravity="center" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:textStyle="bold"             
    android:textSize="20sp"
    android:textColor="@drawable/selector_icon_text_color"
    android:text="Hussain">
    </TextView>
Community
  • 1
  • 1
mH16
  • 2,356
  • 4
  • 25
  • 35
  • Perhaps i wasn't clear. I want ONLY TOP BORDER to be white. This example make all borders white. – tinti Nov 30 '11 at 11:45