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.

Jonik
  • 80,077
  • 70
  • 264
  • 372
OkyDokyman
  • 3,786
  • 7
  • 38
  • 50
  • 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 '15 at 15:15

8 Answers8

266

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

JerabekJakub
  • 5,268
  • 4
  • 26
  • 33
  • 1
    This doesnt actually work when set in the XML. I get an error like this: `"1.2dp" in attribute "letterSpacing" cannot be converted to float."` – dopatraman Jan 16 '16 at 20:19
  • 22
    @dopatraman You have to ommit units, type just value: android:letterSpacing="1.2" instead of android:letterSpacing="1.2dp" – JerabekJakub Jan 18 '16 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 02 '16 at 07:33
  • To be fair, according to Google there are more than 45% devices with API 21 and higher (June 2016). For lower API, you can check another answers here, for example Bart's below. – JerabekJakub Jul 07 '16 at 07:41
  • 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 '16 at 14:52
  • @JerabekJakub is there any alternative for using `letterSpacing` below API 21? – Jimit Patel Mar 07 '17 at 05:37
  • @JimitPatel Hi, as I said before, you can check other answers, for example Bart's answer below. – JerabekJakub Mar 08 '17 at 19:50
  • 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. – Drew Szurko Oct 19 '17 at 00: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. ;-) – Someone Somewhere Apr 09 '18 at 13:32
  • if I toggle between numberPassword as the initial setting in xml and then swap to `inputField.setInputType( InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);`, the masking bullets initially take up way less space until changed via Java. I was wondering how letterspacing can be used to keep spacing consistent when changing between these two types – reactor Nov 07 '19 at 18:18
  • @DrewSzurko the preview is almost never accurate in my experience! – hmac Jan 12 '23 at 09:38
110

More space:

  android:letterSpacing="0.1"

Less space:

 android:letterSpacing="-0.07"
Pablo Cegarra
  • 20,955
  • 12
  • 92
  • 110
29

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)

Joaquin Iurchuk
  • 5,499
  • 2
  • 48
  • 64
zrgiu
  • 6,200
  • 1
  • 33
  • 35
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);
Bart Burg
  • 4,786
  • 7
  • 52
  • 87
  • 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 '14 at 21:29
  • Needs a `null` check in `applyLetterSpacing`, but other than that, life saver! – Bart van Nierop Feb 03 '15 at 19:03
  • doesnt work when u set the text afterwords and then apply spacing programatically – Jono Feb 04 '15 at 14:35
  • @jonney it should, I have it working like that. Could you send an example? – Bart Burg Feb 05 '15 at 07:45
  • Ok when I get to the office I will. Basically I added this view in xml and then did a find view by id to get the object and changed the spacing and text programmatically and it did not change it. If i dont set text the spacing works. If i do settext the spacing only applies a small amount? It's as if it can only apply the big size amount but nothing greater than that at all – Jono Feb 05 '15 at 08:36
  • Hey, this doesn't work with ellipsis at the end of the text. Any ideas on how to fix that ? – Shivam Bhalla Aug 10 '15 at 09:21
  • What is the purpose of `c.toLowerCase()`? – lhunath Mar 24 '16 at 19:48
  • @Ihunath to be completely honest, I don't remember. Try to remove it and see what happens – Bart Burg Mar 25 '16 at 12:53
  • @BartBurg your code works great. but If TextView has spannable string already then this code removes it. – DearDhruv Jun 23 '16 at 10:53
  • 1
    The no-break space - "\u00A0" - is really useful. Thanks – Shayan_Aryan Sep 22 '16 at 08:11
12

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

check this for more

Ravi
  • 34,851
  • 21
  • 122
  • 183
  • 4
    How can to same in <21 ? – Rahul Devanavar Mar 01 '17 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)); } – Maulik Patel Jun 04 '18 at 09:14
10

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

Community
  • 1
  • 1
Pedro Barros
  • 1,326
  • 1
  • 12
  • 19
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.

Ali
  • 21,572
  • 15
  • 83
  • 95
-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

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
snehal_penurkar
  • 273
  • 1
  • 3
  • 15
  • 1
    how does this help? I'm not aware of an HTML tag for letter spacing that works in a TextView. – k2col May 10 '16 at 17:27