4

I want to have a line between each row in TextView. Can original TextView do this? If not, how can I do it?

separating line


ANSWER:

Thanks to @Slartibartfast reference and advice. I made a customized TextView. And I get something like this.

textview with line separator

This is what I want!

The code:

public class LinedTextView extends TextView {

private Rect mRect;
private Paint mPaint;

public LinedTextView(Context context) {
    super(context);
    initialize();
}

public LinedTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialize();

}

public LinedTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initialize();
}

private void initialize() {

    mRect = new Rect();
    mPaint = new Paint();
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setColor(0x800000ff);
}

@Override
protected void onDraw(Canvas canvas) {

    int cnt = getLineCount();
    Rect r = mRect;
    Paint paint = mPaint;
    for (int i = 0; i < cnt; i++) {
        int baseLine = getLineBounds(i, r);
        canvas.drawLine(r.left, baseLine + 1, r.right, baseLine + 1, paint);
    }

    super.onDraw(canvas);
}

}
Barlas Apaydin
  • 7,233
  • 11
  • 55
  • 86
kvh
  • 2,118
  • 19
  • 29
  • 1
    use a listview with dividers. – Raghunandan Jul 18 '13 at 10:13
  • use a background image. – Tugrul Jul 18 '13 at 10:24
  • 1
    You can either follow http://stackoverflow.com/questions/10748467/android-ruled-horizonal-lines-in-textview and create a complex textview or do the simple thing like @Raghunandan says and use a list view – Vrashabh Irde Jul 18 '13 at 10:25
  • i think having a customized TextView will be my choice, rather than using a ListView with dividers or View below TextView. Because it will take me pains to separate the words in to each item of ListView. Thanks @Slartibartfast for your reference, that is really what i want! – kvh Jul 18 '13 at 11:15

1 Answers1

3

Use the following line of code below your TextView

<View android:layout_width="fill_parent"
    android:layout_height="1px"
    android:background="@android:color/background_dark" />

You can configure it according to your need.

You can also use ListView with divider.

Ameer Moaaviah
  • 1,530
  • 12
  • 27