Search code examples
androidandroid-layoutuser-interfaceuitextviewtextview

how to give a line below the Textview in android


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


Solution

  • 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>