Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I built a custom class that extends TextView and adds a method "setSpacing". The workaround is similar to what @Noah said. The method adds a space between each letter of the String and with <a href="http://developer.android.com/reference/android/text/SpannedString.html" rel="noreferrer">SpannedString</a> changes the TextScaleX of the spaces, allowing positive and negative spacing.</p> <p>Hope that helps someone ^^</p> <pre><code>/** * Text view that allows changing the letter spacing of the text. * * @author Pedro Barros (pedrobarros.dev at gmail.com) * @since May 7, 2013 */ 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 spacing = Spacing.NORMAL; private CharSequence originalText = ""; public LetterSpacingTextView(Context context) { super(context); } public LetterSpacingTextView(Context context, AttributeSet attrs){ super(context, attrs); } public LetterSpacingTextView(Context context, AttributeSet attrs, int defStyle){ super(context, attrs, defStyle); } public float getSpacing() { return this.spacing; } public void setSpacing(float spacing) { this.spacing = spacing; applySpacing(); } @Override public void setText(CharSequence text, BufferType type) { originalText = text; applySpacing(); } @Override public CharSequence getText() { return originalText; } private void applySpacing() { if (this == null || this.originalText == null) return; StringBuilder builder = new StringBuilder(); for(int i = 0; i &lt; originalText.length(); i++) { builder.append(originalText.charAt(i)); if(i+1 &lt; originalText.length()) { builder.append("\u00A0"); } } SpannableString finalText = new SpannableString(builder.toString()); if(builder.toString().length() &gt; 1) { for(int i = 1; i &lt; builder.toString().length(); i+=2) { finalText.setSpan(new ScaleXSpan((spacing+1)/10), i, i+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } super.setText(finalText, BufferType.SPANNABLE); } public class Spacing { public final static float NORMAL = 0; } } </code></pre> <p>Using it:</p> <pre><code>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); </code></pre>
 

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