Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two different styles in a single textview with different gravity and height

I am looking for a textview which looks like enter image description here. I have used below code for the implementation of this..

SpannableString text;
    text = new SpannableString(bal_dollars.getText());
    int index = bal_dollars.getText().toString().indexOf(".");
    text.setSpan(new TextAppearanceSpan(getApplicationContext(),
            R.style.HeroGraphicBalanceSmallFont), index, bal_dollars
            .getText().toString().length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    bal_dollars.setText(text, TextView.BufferType.SPANNABLE);

here is the style used in the span..

<style name="HeroGraphicBalanceSmallFont" parent="@android:style/TextAppearance.Medium">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:textSize">50sp</item>
    <item name="android:singleLine">true</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:layout_gravity">top|right</item>
    <item name="android:paddingBottom">30dp</item>
    <item name="android:gravity">top|right</item>
    <item name="android:textColor">@color/status_text</item>
    <item name="customFont">fonts/HeroicCondensedMedium.ttf</item>
</style>

everything looks fine except the spacing of targeted span of text i.e decimal part gravity.. enter image description here. I tried all the xml attributes. Please help

like image 986
CodeFury Avatar asked Mar 12 '13 08:03

CodeFury


People also ask

Can you have multiple styles inside TextView?

There are many ways to make multiple styles inside android TextView and in this example I have made multiple style with three different ways using HTML tags and Html. fromHtml().

What is Spannable Text?

android.text.SpannableString. This is the class for text whose content is immutable but to which markup objects can be attached and detached.


1 Answers

I cracked it. I am posting this as it might be helpful for others and thanks to @kcoppock for the hint. I have extended MetricAffectingSpan class which affects character-level text formatting in a way that changes the width or height of characters extend this class.

public class CustomCharacterSpan extends MetricAffectingSpan {
double ratio = 0.5;

public CustomCharacterSpan() {
}

public CustomCharacterSpan(double ratio) {
    this.ratio = ratio;
}

@Override
public void updateDrawState(TextPaint paint) {
    paint.baselineShift += (int) (paint.ascent() * ratio);
}

@Override
public void updateMeasureState(TextPaint paint) {
    paint.baselineShift += (int) (paint.ascent() * ratio);
}}

use this to update the text in your views..

String string = tv.getText().toString();
    SpannableString text = new SpannableString(tv.getText());
    int index = tv.getText().toString().indexOf(".");
    text.setSpan(new CustomCharacterSpan(), index, string.length(),
            SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);//u can use ur own ratio using another constructor
    tv.setText(text, TextView.BufferType.SPANNABLE);
like image 137
CodeFury Avatar answered Jan 03 '23 06:01

CodeFury