Allan Macmillan
Allan Macmillan

Reputation: 1491

TextView onDraw - Drawing Lines

I would like to draw vertical lines between Numbers/Letters in my TextView. So it should look similar to

A|B|C|D

without the use of the | character and use drawLine() instead.

I am trying to do it using the width of the TextView and assuming the centre of each character will find itself at , 1/8, 3/8, 5/8, 7/8 of the TextView width for this example. However the lines dont line up as they should.

Not sure whats not working, help appreciated.

Upvotes: 1

Views: 277

Answers (1)

kabuko
kabuko

Reputation: 36302

I am trying to do it using the width of the TextView and assuming the centre of each character will find itself at , 1/8, 3/8, 5/8, 7/8 of the TextView width for this example.

That's your problem. For starters, you haven't specified that you're using a mono-spaced font. If you're not, then the letters won't be evenly distributed. Even if you are using a mono-spaced font, likely the padding at the beginning (and possibly end) of the TextView are going to offset things. I can't remember exactly how TextView measures things, but I suspect looking at actual left padding value would be a good start to find the left padding. If you want to use this with a variable width font, you'll want to use something like Paint.measureText to measure the width of the characters.

Once you have all that, you can add the width of the character(s) to the left padding to find the position to place each line.

Upvotes: 1

Related Questions