159

How can i change letter spacing in a textview? Will it help if I have HTML text in it (I cannot use webview in my code).

P.S. I'm using my own typeface in the textview with HTML text.

1
  • 7
    In the layout editor you can do android:letterSpacing=".05" Where .05 would be roughly "50" in a program like photoshop
    – Jacksonkr
    Dec 18, 2015 at 15:15

8 Answers 8

266

Since API 21 there is an option set letter spacing. You can call method setLetterSpacing or set it in XML with attribute letterSpacing.

11
  • 22
    @dopatraman You have to ommit units, type just value: android:letterSpacing="1.2" instead of android:letterSpacing="1.2dp" Jan 18, 2016 at 15:38
  • 12
    Knowing that 31% of the devices don't support API 21, how would you do this in previous versions?
    – ninjaneer
    Jul 2, 2016 at 7:33
  • 30
    Note that the value of letterSpacing isn't in dp/sp, it is in 'EM' units. Typical values for slight expansion will be around 0.05. Negative values tighten text.
    – Cristan
    Sep 23, 2016 at 14:52
  • 29
    For some reason letterSpacing wasn't changing in the AS preview. I had to actually run the app on a physical device to see the change. Oct 19, 2017 at 0:44
  • 2
    It's true - Android Studio design mode does NOT properly respect this parameter. Probably worthy of a bug write-up against the tool. OR don't report it and tell your manager you need a device just to test this. ;-) Apr 9, 2018 at 13:32
111

More space:

  android:letterSpacing="0.1"

Less space:

 android:letterSpacing="-0.07"
4
30

check out android:textScaleX

Depending on how much spacing you need, this might help. That's the only thing remotely related to letter-spacing in the TextView.

Edit: please see @JerabekJakub's response below for an updated, better method to do this starting with api 21 (Lollipop)

10
  • 10
    How would you translate that over to letter spacing? Mar 10, 2011 at 3:58
  • 94
    Is there a way to increase/decrease the space between letters/characters? textScaleX - Literary scales the letters/characters.
    – Kaymatrix
    Aug 22, 2012 at 5:18
  • 2
    @Barts answer does what the question is asking
    – bkurzius
    Nov 5, 2014 at 16:00
  • 3
    Scaling the text in the X axis is not the proper way to handle kerning or tracking in Android. There is an api for lollipop that accomplishes this however previous to lollipop you will need a custom text view to accomplish this.
    – Elliott
    Aug 20, 2015 at 3:35
  • 4
    I don't know how this solution got so many upvotes (+33, -24) are people just blindly putting up their votes? letterSpacing vs textScaleX what a huge difference Mar 7, 2017 at 5:36
27

This answer is based on Pedro's answer but adjusted so it also works if text attribute is already set:

package nl.raakict.android.spc.widget;
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ScaleXSpan;
import android.util.AttributeSet;
import android.widget.TextView;


public class LetterSpacingTextView extends TextView {
    private float letterSpacing = LetterSpacing.BIGGEST;
    private CharSequence originalText = "";


    public LetterSpacingTextView(Context context) {
        super(context);
    }

    public LetterSpacingTextView(Context context, AttributeSet attrs){
        super(context, attrs);
        originalText = super.getText();
        applyLetterSpacing();
        this.invalidate();
    }

    public LetterSpacingTextView(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
    }

    public float getLetterSpacing() {
        return letterSpacing;
    }

    public void setLetterSpacing(float letterSpacing) {
        this.letterSpacing = letterSpacing;
        applyLetterSpacing();
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        originalText = text;
        applyLetterSpacing();
    }

    @Override
    public CharSequence getText() {
        return originalText;
    }

    private void applyLetterSpacing() {
        if (this == null || this.originalText == null) return;
        StringBuilder builder = new StringBuilder();
        for(int i = 0; i < originalText.length(); i++) {
            String c = ""+ originalText.charAt(i);
            builder.append(c.toLowerCase());
            if(i+1 < originalText.length()) {
                builder.append("\u00A0");
            }
        }
        SpannableString finalText = new SpannableString(builder.toString());
        if(builder.toString().length() > 1) {
            for(int i = 1; i < builder.toString().length(); i+=2) {
                finalText.setSpan(new ScaleXSpan((letterSpacing+1)/10), i, i+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        super.setText(finalText, BufferType.SPANNABLE);
    }

    public class LetterSpacing {
        public final static float NORMAL = 0;
        public final static float NORMALBIG = (float)0.025;
        public final static float BIG = (float)0.05;
        public final static float BIGGEST = (float)0.2;
    }
}

If you want to use it programatically:

LetterSpacingTextView textView = new LetterSpacingTextView(context);
textView.setSpacing(10); //Or any float. To reset to normal, use 0 or LetterSpacingTextView.Spacing.NORMAL
textView.setText("My text");
//Add the textView in a layout, for instance:
((LinearLayout) findViewById(R.id.myLinearLayout)).addView(textView);
10
  • is this added actually spaces between the characters? Boo... if someone copies this text it will include the extra spaces. No-Joy
    – johnw182
    Nov 15, 2014 at 21:29
  • Needs a null check in applyLetterSpacing, but other than that, life saver! Feb 3, 2015 at 19:03
  • doesnt work when u set the text afterwords and then apply spacing programatically
    – Jono
    Feb 4, 2015 at 14:35
  • @jonney it should, I have it working like that. Could you send an example?
    – Bart Burg
    Feb 5, 2015 at 7:45
  • 1
    The no-break space - "\u00A0" - is really useful. Thanks Sep 22, 2016 at 8:11
12

after API >=21 there is inbuild method provided by TextView called setLetterSpacing

check this for more

2
  • 4
    How can to same in <21 ? Mar 1, 2017 at 11:31
  • 2
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { this.setLetterSpacing(getResources().getDimension(R.dimen.letter_spacing)); }else{ this.setTextScaleX(getResources().getDimension(R.dimen.letter_spacing)); } Jun 4, 2018 at 9:14
10

I built a custom class that extends TextView and solves this problem... Check out my answer here =)

0
0

As android doesn't support such a thing, you can do it manually with FontCreator. It has good options for font modifying. I used this tool to build a custom font, even if it takes some times but you can always use it in your projects.

-1

For embedding HTML text in your textview you can use Html.fromHTML() syntax. More information you will get from http://developer.android.com/reference/android/text/Html.html#fromHtml%28java.lang.String%29

1
  • 1
    how does this help? I'm not aware of an HTML tag for letter spacing that works in a TextView.
    – k2col
    May 10, 2016 at 17:27

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.