10

I am restricting on Android Edittext as below

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" but can't enter spaces. How can I make it allow spaces? Is this possible to get only via .xml?.

Jess
  • 2,991
  • 3
  • 27
  • 40
james
  • 1,967
  • 3
  • 21
  • 27
  • "but can't able to enter space." - How do you mean, exactly? Are you getting an error when you add a space there, or is it just not working? – Mike M. Aug 06 '16 at 11:40
  • Have you tried adding a space into that string? Look at the accepted answer here. http://stackoverflow.com/questions/23212439/how-to-restrict-the-edittext-to-accept-only-alphanumeric-characters – OneCricketeer Aug 06 '16 at 12:24
  • just add the space between any character of your digit like this android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY Z" – A.R. Aug 06 '16 at 12:25

4 Answers4

17

Try this, hope it helps,

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ " 
Harshal Benake
  • 2,391
  • 1
  • 23
  • 40
  • 1
    Sorry, but didn't worked on my case.EditText still restricts the `space`.I was trying on a PIXEL emulator in studio itself. – iCantC Mar 16 '19 at 07:18
  • 1
    @iCantC It works on real devices. Its a very old attribute. Try on real device. Also check if u r trimming the string in code as it wont work. – Harshal Benake Mar 16 '19 at 07:46
  • Yes it worked perfectly , as you predicted I was actually trimming the text, so tried it by removing the trim function and it was as smooth as butter. – iCantC Mar 16 '19 at 08:19
8

Edittext digit accept white space only if give space end of characters, have look:

android:digits="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ" 

Happy coding!!

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
1
<string name="digits_text">abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@$%^*()_+-{},./\|&amp; </string>

<android.support.design.widget.TextInputLayout
    android:id="@+id/textinputlayout_support_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/textview_or">
    <android.support.design.widget.TextInputEditText
        android:id="@+id/textinputedittext_help_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:digits="@string/digits_text"
        android:hint="@string/description"
        android:inputType="textMultiLine"
        android:lines="6"
        android:maxLines="10" />
</android.support.design.widget.TextInputLayout>

Digits allowed space in EditText Android.

Hitesh sapra
  • 248
  • 2
  • 12
0
  1. Include space(" ") in end of Digits String

    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "

<android.support.design.widget.TextInputEditText
                        style="@style/Text_input_EditText"
                        android:hint="Enter S/o , D/o , W/o *"
                        android:inputType="textCapCharacters"
                        android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
                        android:maxLength="100"
                        android:id="@+id/do_ad_so_do_wo"
                        />
Mallikarjuna
  • 874
  • 6
  • 17