How to change the distance between letters in Textview?

How to change the spacing between letters in text form? Did it help if I have HTML text (I cannot use webview in my code).

PS I use my own font in text form with HTML text.

+63
android typeface android-textview
Feb 27 2018-11-28T00:
source share
7 answers

check android: textScaleX

Depending on how many intervals you need, this may help. This is the only thing remotely related to the spacing between letters in a TextView.

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

+12
Feb 27 2018-11-21T00:
source share

Since API 21 there is an option that sets the distance between letters. You can call the setLetterSpacing method or set it to XML with the letterSpacing attribute.

+130
Jan 26 '15 at 8:55
source share

This answer is based on Pedro's answer, but is configured so that it also works if the 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 programmatically:

 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); 
+20
Apr 22 '14 at 9:54 on
source share

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

check it out for more

+8
Dec 15 '15 at 7:39
source share

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

+7
May 8 '13 at 4:20
source share

Since android does not support such a thing, you can do it manually using FontCreator. It has good options for changing the font. I used this tool to create a custom font, even if it takes several times, but you can always use it in your projects.

0
Aug 28 '13 at 12:12
source share

You can use the Html.fromHTML() syntax to Html.fromHTML() HTML text in a text field. Additional information you will receive from http://developer.android.com/reference/android/text/Html.html#fromHtml%28java.lang.String%29

0
Apr 22 '14 at 10:01
source share



All Articles