kvh
kvh

Reputation: 2158

How can I have a row separating line in TextView

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);
}

}

Upvotes: 4

Views: 1318

Answers (1)

Ameer Moaaviah
Ameer Moaaviah

Reputation: 1526

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.

Upvotes: 3

Related Questions