Note that there are some explanatory texts on larger screens.

plurals
  1. POAligning ImageSpan to the top of the TextView
    primarykey
    data
    text
    <p>Currently, I wish to add an image in between texts and align it to the top of the TextView.</p> <p>Something like this:</p> <p><img src="https://i.stack.imgur.com/PMlXh.png" alt="enter image description here"></p> <p>The only vertical alignments I can find are baseline (which seems to put it right down the center of the text) and align bottom.</p> <p>What happens if I use ALIGN_BASELINE is:</p> <p><img src="https://i.stack.imgur.com/t4PBs.png" alt="enter image description here"></p> <p>Is there a way to align it to the top instead?</p> <p>My current code:</p> <pre><code> txtView.setText(this.addImageAsterisk( "The string to have asterisk at the end*"), BufferType.SPANNABLE); </code></pre> <p>then</p> <pre><code>private CharSequence addImageAsterisk(String string) { Drawable d = context.getResources().getDrawable(R.drawable.img_asterisk); ImageSpan imageSpan = new ImageSpan(d, ImageSpan.ALIGN_BASELINE); final SpannableString spannableString = new SpannableString(string); spannableString.setSpan(imageSpan, string.length()-1, string.length(), 0); return spannableString; } </code></pre> <p>removing ImageSpan.ALIGN_BASELINE sets it to align to the bottom which is also not my expected result.</p> <p>--- Thank you user Lalit Poptani, I tried applying your answer---- after applying this, what happens is that the whole textview seems to have extra margin top.</p> <p>before applying span:</p> <pre><code>This is the text* </code></pre> <p>after applying the SuperscriptSpanAdjuster</p> <pre><code>(some extra space) This is the text* </code></pre> <p>My code:</p> <pre><code>String string = "This is the text*"; Drawable d = this.context.getResources().getDrawable(R.drawable.img_asterisk); d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); ImageSpan imageSpan = new ImageSpan(d, ImageSpan.ALIGN_BASELINE); SuperscriptSpanAdjuster s = new SuperscriptSpanAdjuster(1.5); final SpannableString spannableString = new SpannableString(string); spannableString.setSpan(s, string.length() - 1, string.length(), 0); spannableString.setSpan(imageSpan, string.length(), string.length() + 1, 0); textView.setText(spannableString); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload