0

I am developing a simple devotional app, which has a Kannada (a language in India) sentence to be displayed. I am successful in using typeface and displaying the content.

In few places I have word which has a line on top/bottom of the word as shown below. I tried with a spannable image but I am still not able to achieve it properly.

This is a sample of the code which I am referring to. Here I am using a small icon to display it in between the string.

Spannable span1 = new SpannableString("The  imageplace");
Drawable android = TestImageActivity.this.getResources().getDrawable(R.drawable.end);
android.setBounds(5, 0, 20, 5);
ImageSpan image = new ImageSpan(android, ImageSpan.ALIGN_BASELINE);
span1.setSpan(image, 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
tvTextImage3.setText(span1);

small line above and below text

TylerH
  • 20,799
  • 66
  • 75
  • 101
keerthi
  • 11
  • 2
  • I an not getting any solution on how this is achieved, if any one have any idea on this, let them help – keerthi Dec 30 '16 at 10:21
  • I need a reason why this question got closed. – keerthi Dec 30 '16 at 10:42
  • Adding the code is a big help. Can you also explain further what the problem is? It seems like you are trying to make some text appear like the image. The code that you included is not complete to produce that text, and you didn't show how the result of your code differs from what you wanted to achieve. – Peter Hall Dec 30 '16 at 16:28

1 Answers1

2

ImageSpan extends ReplacementSpan so any characters you are spanning won't get rendered, as the TextLayout is expecting that the span itself will be doing all the rendering.

What I would recommend is implementing your own ReplacementSpan subclass. Since it looks like your graphics are associated with one character, you would wrap the single character.

In the getSize override, you would use start and end to index into text and get the character(s) you are spanning, then use paint.getTextBounds() to measure the width of the text and return that value. You want the width calculation to work in a way that the width of the span doesn't affect the default spacing of the text.

Another thing this method might need to do is change the FontMetrics by increasing the ascent and descent in order to give you some space to draw the lines.

In the draw override, you use the paint to render the text that isn't being rendered within the span. The paint and font metrics should already have the proper values so that your text render looks like the surrounding text. Of course, you'll also render the line graphics you want.

For some sample code, take a look at my answer to a similar question. This has all the pieces I just discussed.

If you want me to write some code for this, you'll need to provide some code that gives me a starting point with some actual Kannada text along with what the lines are and where they go. I don't even know if Kannada text is LTR or RTL; that might affect how the span subclass is coded. Preferably the text would correspond to the image you posted so I can see how it should look when it's working.

Community
  • 1
  • 1
kris larson
  • 30,387
  • 5
  • 62
  • 74