1

I am trying to Display a text box with letters separated by spaces.

    EditText wordText = (EditText) findViewById(R.id.word_text);
    wordText.setPaintFlags(wordText.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
    InputFilter[] filterArray = new InputFilter[2];
    filterArray[0] = new InputFilter.AllCaps();
    filterArray[1] = new InputFilter.LengthFilter(10);
    wordText.setGravity(Gravity.CENTER);

Here's the layout file.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/match_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_marginTop="100dp">

    <EditText android:id="@+id/word_text"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:hint="Your Word"
        android:maxLength="15"
        android:inputType="textCapWords"/>

    <Button
        android:id="@+id/match_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="start"
        android:text="Match"
        android:layout_marginTop="50dp"/>
</LinearLayout>

How can I add space between letters in API 12. I dont want to use setLetterSpacing().

Also, when I start typing, setGravity(Gravity.CENTER) is causing my letters to appear from the center of the box. How do i make it appear from Left to right ? If I set Gravity.LEFT, the EditText itself moves to the left in the LinearLayout. Can anyone help ?

user691197
  • 927
  • 6
  • 20
  • 38
  • Why you don't want to use setLetterSpacing()? And for alignment of text use android:layout_gravity="left" in your XML layout or appropriate API function. It can also ask you for usage of android:layout_gravity="start". – Oleg Novosad Aug 27 '15 at 21:58
  • I suggest you add the layout file also, – diyoda_ Aug 27 '15 at 22:35
  • As i mentioned, i get compiler error since my minSdk is API 12 where setLetterSpacing isnt available. And when I use Gravity.Left, my EditText itself moves to the left of the screen. I want it to be positioned in the center, but when I type, letters should appear from left. Is it possible to do this ? – user691197 Aug 28 '15 at 05:34

2 Answers2

1

The subject of letter spacing seems to have caused quite some traffic over the years. Some preach for using textScaleX property, but that by itself is not an acceptable solution, because it skews the letters as well. Several other solutions that were mentioned are:

  1. Create a custom font and add you own spacing there. It is convoluted though, because it involves copyright and not-so-dynamic spacing between letters
  2. Adding spaces in between letters - also not so desirable, because there is not enough granularity when it comes to increases in spacing values
  3. A combination of textScaleX and adding spaces between letters - it involves a custom view where textScaleX is applied only to the spaces - so the letters remain unchanged, only the space increases-decreases. You can use the non breakable space unicode character as well. For this solution you can check out Change text kerning or spacing in TextView?
Community
  • 1
  • 1
N.T.
  • 2,601
  • 1
  • 14
  • 20
1
  1. Make TextWatcher (this example for enter only digits into EditText field, you can change it if necessary)

    public class CustomTextWatcher implements TextWatcher{
    
        private static final char space = ' ';
    
    public CodeTextWatcher (){}
    
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
    }
    
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }
    
    @Override
    public void afterTextChanged(Editable s) {
    
        // Remove all spacing char
        int pos = 0;
        while (true) {
            if (pos >= s.length()) break;
            if (space == s.charAt(pos) && (((pos + 1) % 2) != 0 || pos + 1 == s.length())) {
                s.delete(pos, pos + 1);
            } else {
                pos++;
            }
        }
    
        // Insert char where needed.
        pos = 1;
        while (true) {
            if (pos >= s.length()) break;
            final char c = s.charAt(pos);
            // Only if its a digit where there should be a space we insert a space
            if ("0123456789".indexOf(c) >= 0) {
                s.insert(pos, "" + space);
            }
            pos += 2;
        }
    }
    

    }

  2. Attach this textWatcher to your EditText:

     EditText.addTextChangedListener(new CustomTextWatcher());
    
koa73
  • 861
  • 2
  • 10
  • 27