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.
Add only top and bottom border on LinearLayout
Make this two file and put this code. you can set border top and bottom border,
main.xml
<TextView
android:text="This is textline"
android:background="@drawable/border_set"
/>
border_set.xml
This file located into full path project_root/res/drawable/border_set.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="#000" />
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
Way to add a border of different color to top and bottom edge of an android view
You're quite close to what you want, what you need to do is add another item below your default item. Those two items are your top/bottom borders. By add bottom/top 1dp to both, you reveal the two colours.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="#000000"/>
</shape>
</item>
<item
android:top="1dp">
<shape android:shape="rectangle">
<solid android:color="#000000"/>
</shape>
</item>
<item
android:top="1dp" android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="#ffffff"/>
</shape>
</item>
</layer-list>
How to create border at bottom only?
Your problem is different or you are unable to explain it, however I got it
- Crete two files in drawable, bottom_selected and bottom_unselected
Bottom_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#001EFF" />
</shape>
</item>
<item android:bottom="3dp">
<shape android:shape="rectangle" >
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
Bottom_unselected.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#001EFF" />
</shape>
</item>
<item android:bottom="1dp">
<shape android:shape="rectangle" >
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
Now your Buttons
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/fragment_container"
android:layout_marginLeft="54dp"
android:layout_marginTop="110dp"
android:background="@drawable/bottom_selected"
android:text="Button" />
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:background="@drawable/bottom_unselected"
android:text="Button" />
<Button
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button01"
android:layout_alignBottom="@+id/Button01"
android:layout_toRightOf="@+id/Button01"
android:background="@drawable/bottom_unselected"
android:text="Button" />
Final View
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 set top border for bottom navigation bar in android as shown in image
You can try this: add a View
element above BottomNavigationView
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_above="@+id/bottom_navigation"
android:background="#000000"></View>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
app:itemBackground="@color/BottomNavigationBgColor"
app:itemIconTint="@color/CelestialBlue"
app:itemTextColor="@color/CelestialBlue"
app:menu="@menu/bottom_navigation_main" />
how to set border linear layout on top and bottom
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:bottom="1dp"
android:left="-2dp"
android:right="-2dp"
android:top="1dp">
<shape android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="@android:color/darker_gray" />
<solid android:color="@android:color/transparent" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
</item>
Use the above layout as backgroud border. It will give you a very good effect. change the color and padding if you wish. Add this as xml in your drawable folder and make this drawable as background for the layout.
Hope this helps.
how to set border bottom of a view in android
this will work for you just change 3dp to 1dp for android:bottom
<?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:color="@color/colorPrimary" android:width="2dp"/>
</shape>
</item>
<item android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="@android:color/white"/>
</shape>
</item>
</layer-list>
Applying a top and bottom border to TextView using background doesn't work, but foreground does
Exploring as in the discussion in the question comments with Zain, the theme (under styles
) I was using had set the backgroundTint
to the background color of the activity (and other components in which the TextView was lying).
So, even though the background was being set as the border drawable, it was being tinted to the color (by default) of the background of its parent, so the background was indistinguishable.
Note however that setting an explicit backgroundTint
on the TextView (while the one in styles existed), did not make the background visible, so the backgroundTint
from default styling had precedence over the one set individually.
This behavior seems to contradict Style hierarchy
If I misinterpreted, kindly correct me
TL;DR: If you're having a similar issue, check the backgroundTint property in styles.xml
among the other fixes (the drawable itself).
Related Topics
Context.Startforegroundservice() Did Not Then Call Service.Startforeground()
What Is Activity.Finish() Method Doing Exactly
How to Install an Apk File in the Android Emulator
How to Delete an Sms in Android Before It Reaches the Inbox
Android - How to Disable the Click of Home Button
How to Make My App a Device Owner
Benefit of Using Parcelable Instead of Serializing Object
Android Room - Simple Select Query - Cannot Access Database on the Main Thread
Setbackground VS Setbackgrounddrawable (Android)
Receive Result from Dialogfragment
Android Pick Images from Gallery
Error:(1, 0) Plugin With Id 'Com.Android.Application' Not Found
Separate Back Stack For Each Tab in Android Using Fragments
Show Compose Sms View in Android
Finished with Non Zero Exit Value
Difference Between App:Srccompat and Android:Src in Android's Layout Xml