JustAMartin
JustAMartin

Reputation: 13723

How to create a border on one edge of a view?

I have found that to add borders around views I can use the following code as a background for the view:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<stroke android:width="1dp" android:color="#999999" />
<padding android:left="1dp" android:top="1dp" android:right="1dp"
    android:bottom="1dp" />
</shape>

But what if I want a border just for bottom, or top or right or left? I tried the following:

<shape
  xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
<stroke android:color="#000"/>
</shape>

but when I set it as background

android:background="@drawable/my_border"

it draws the line at the middle of the view, so I have a strike-through effect. Is there any way I can draw this line at the bottom or at the top or make it vertical?

In WPF we have a relative coordinates for shapes, so I can offset lines as needed. Is there something similar on Android? If we don't have borders, at least we should have good line shape drawing tools, so we can draw borders as needed. Do Android developers plan to do something about it?

Upvotes: 12

Views: 23019

Answers (2)

Fay007
Fay007

Reputation: 2917

Are u looking for this:

<item android:state_pressed="false">
     <inset 
            android:insetBottom="-3dp"
            android:insetLeft="-3dp"
            android:insetTop="-3dp" >

                <shape >
                     <corners android:radius="0.0dip" />
                     <stroke android:width="2.0dip"  android:color="#EEE"/>

                </shape>
           </inset>
           </item>

it will show a border only at the right. And u have to set it as the background

Upvotes: 0

MattZ
MattZ

Reputation: 381

For border line on the top or bottom of you view, you can check my previous answer here. It is a very simple way.

Upvotes: 1

Related Questions