How to give a line under Textview in android

I want to add a uderline that covers the width of the parent field below the text field to show that the text image looks like a caption.

<TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginLeft="10dp" android:layout_marginStart="10dp" android:lines="1" android:maxLines="2" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/black" /> 

Thank you in advance

+5
source share
6 answers

you can try this

  <TextView android:id="@+id/title" style="@style/sectionHeader" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginLeft="10dp" android:layout_marginStart="10dp" android:lines="1" android:maxLines="2" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/black" /> 

add style to your styles.xml

  <style name="sectionHeader" parent="android:Widget.Holo.Light.TextView"> <item name="android:drawableBottom">@drawable/section_header</item> <item name="android:drawablePadding">4dp</item> <item name="android:layout_marginTop">8dp</item> <item name="android:paddingLeft">4dp</item> <item name="android:textAllCaps">true</item> <item name="android:textColor">@color/emphasis</item> <item name="android:textSize">14sp</item> </style> 

make one name with a name like section_header.xml

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <size android:width="1000dp" android:height="0.5dp" /> <solid android:color="@color/blue_grey"/> </shape> 

add color to your .xml color

  <color name="blue_grey">#607d8b</color> <color name="emphasis">#31b6e7</color> 
+6
source

use this code below the text

 <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/darker_gray" /> 
+7
source

If you want to add programmatically do it

 mTextView.setPaintFlags(mTextView.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG); 
+4
source

Put the view in the text view below:

 <View android:layout_width="match_parent" android:layout_height="1dp" android:background="# your hex color code /> 
+1
source

If you add text to the Textview from Strings folder, you can specify the following. From

strings.xml

 <string name="your_string_here"><u>This is an underline</u>.</string> 

If you add text dynamically, then you use the following.

In your activity: -

 TextView textView = (TextView) view.findViewById(R.id.textview); SpannableString spannableStringObject= new SpannableString("Your text here"); spannableStringObject.setSpan(new UnderlineSpan(), 0, spannableStringObject.length(), 0); textView.setText(spannableStringObject); 
+1
source

His work is for me.

 tv.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG); 
0
source

Source: https://habr.com/ru/post/1236127/


All Articles