12

I want to add uderline which cover the fill parent width below the textview to show the textview look like the heading.

        <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" />

Thanks in advance

8 Answers8

16

use this code below the textview

<View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray" />
Kastriot Dreshaj
  • 1,121
  • 6
  • 16
9

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 at 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 drawable name as 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 color.xml

  <color name="blue_grey">#607d8b</color>
  <color name="emphasis">#31b6e7</color>
Shubhank Gupta
  • 833
  • 2
  • 15
  • 35
7

If you want to add programmatically then do this

mTextView.setPaintFlags(mTextView.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);
Maverick
  • 961
  • 9
  • 13
5

If you are adding text to the Textview from Strings folder you can specify as follows.. From

strings.xml

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

If You are Adding text dynamically in that case 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);
Harish
  • 3,122
  • 2
  • 31
  • 46
3

Its works for me.

tv.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
2

Put view on below text view:

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="# your hex color code  />
Pang
  • 9,564
  • 146
  • 81
  • 122
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49
1

Example using ConstraintLayout. Constrain the view to the TextView. A line with the exact space of the TextView will be drawn.

<androidx.constraintlayout.widget.ConstraintLayout

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"               
            android:text="title"                             
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <view
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:background="@color/color_black"
            app:layout_constraintTop_toBottomOf="@id/tv_title"
            app:layout_constraintStart_toStartOf="@id/tv_title"
            app:layout_constraintEnd_toEndOf="@id/tv_title"/>

</androidx.constraintlayout.widget.ConstraintLayout>
Rajeev Shetty
  • 1,534
  • 1
  • 17
  • 27
0

Insert a divider, on width, start with about 200dp and go from there. Change Background color to what you want, change gravity to center horizontal. You can mess with the height as you please.