Nameet Nameet
Nameet Nameet

Reputation: 49

How to draw a line below textview

Im using single text-view and appending values to it in order to display on constraint layout. I need to separate each textview using a line below it. However, because I'm using append to text-view the line shows up at the end of the list. Is there a way to do this?

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LessonActivity">

<Button
    android:id="@+id/buttonStartId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="8dp"
    android:text="@string/start"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textViewLessonId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:text="@string/lessonresult"
    android:textSize="36sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.497"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/buttonStartId" />
    </android.support.constraint.ConstraintLayout> 

And I'm using for loop with this text-view something like this

for(int i = 1; i < 11; i++){
            textViewLessonId.append(user_input+" X "+i+" = "+i*user_input);
            textViewLessonId.append("\n");
        }

Upvotes: 1

Views: 1208

Answers (3)

SAURABH_12
SAURABH_12

Reputation: 2300

  1. Use View Tag in Layout xml file to create a line

     <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorAccent"/>
    
  2. In Java code

    View ruler = new View(myContext); ruler.setBackgroundColor(0xFF00FF00); theParent.addView(ruler,new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 2));

3.If you does not want to use an extra view just for underlines. Add this style on your textView.

style="?android:listSeparatorTextViewStyle"

Upvotes: 1

Naveen Kommuri
Naveen Kommuri

Reputation: 406

Create compound view with textview and view and use that view instead of plain textview.

Upvotes: 0

Gvtha
Gvtha

Reputation: 1503

Using View: Add the below <View.../> inbetween your <TextView.../>

<TextView 
 android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:text="Text 1"/>

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

<TextView 
 android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:text="Text 2"/>

Using LinearLayout:

android:divider="?android:dividerHorizontal"
android:showDividers="middle"

For Example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:divider="?android:dividerHorizontal"
 android:showDividers="middle"
 android:orientation="vertical" >            

    <TextView 
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="Text 1"/>

    <TextView 
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="Text 2"/>
</LinearLayout>

Upvotes: 2

Related Questions