0

I have upgraded to Android Studio 2.2 from 2.1 now when I set the inputType to text on a EditText widget it will not restrict my entry to text only. This is happening on new projects. My older projects the concept works fine. Android Studio 2.2 is asking me to update to Gradle plugin V 2.2.1 and Version 2.14.1 on older projects. Is this a BUG or am I setting something wrong in my XML file? I thought this issue would be fixed with AS 2.2.1 build 145-3330264 but no change. I have searched SO and tried various fixes related to junit 4.12 jar that is not present in new projects with no results.

    <EditText
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:ems="10"
    android:text="Name"
    android:id="@+id/etPW"
    android:layout_marginTop="50dp"
    android:layout_marginLeft="140dp"
    android:textColor="@android:color/black"
    android:textStyle="bold"
    android:textSize="16sp"
    android:inputType="text"
    android:gravity="bottom"/>
James_Duh
  • 1,321
  • 11
  • 31
  • What exactly are you expecting to be blocked by `android:inputType="text"`? That input type allows any text, last I checked. – CommonsWare Oct 11 '16 at 18:16
  • @CommonsWare I would like the ET field to block the entry of numbers and in the past it worked as expected now not so much. Sorry I did not explain the error – James_Duh Oct 11 '16 at 18:24
  • 2
    `android:inputType="text"` doesn't block the entry of numbers. That's the default input type for `EditText`. You can type in anything that you want. I am not aware of an input type that prevents the entry of numbers. – CommonsWare Oct 11 '16 at 18:30
  • @CommonsWare NO the ET still lets me enter numbers. I am using my keyboard to enter values but did test the keyboard on the emulator. I am a 7th grader and do not have a real device to test on – James_Duh Oct 11 '16 at 18:34
  • "NO the ET still lets me enter numbers" -- correct. AFAIK, there is no `android:inputType` value that blocks the entry of numbers. – CommonsWare Oct 11 '16 at 18:37

2 Answers2

3

You can add this to your XML:

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

That should not allow numbers to be entered (or punctuation, so add whatever characters you want to allow). I tested it and it worked for me.

DigitalNinja
  • 1,078
  • 9
  • 17
  • When did this syntax or concept become incorporated into Android Studio ? It works just great So I will up vote. So if I understand only characters that are in the digits are permitted so for a Password Field I would use this and not Regex? – James_Duh Oct 11 '16 at 18:42
  • @James_Duh I'm not sure how long it has been available, but it's intended to be used for numeric input. However, if you are allowing text to be input then it will still work to limit the input to those specified "digits". I can't say if you should use this over regex, that would be a design choice depending on certain criteria you know. – DigitalNinja Oct 11 '16 at 18:48
  • Well I am real confused as my old projects did not require this line of code and as Fedor Tsyganov found the answer from a 6 year old post I have to wonder what Google is doing Your one line is simple but I spent a lot of time learning Regex so your comments are appreciated – James_Duh Oct 11 '16 at 18:54
1

Similar question been asked here.

I will duplicate the answer:

Either of these two.

XML:

<EditText
    android:id="@+id/editText"
    android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

Java:

EditText state = (EditText) findViewById(R.id.editText);

Pattern ps = Pattern.compile("^[a-zA-Z ]+$");
Matcher ms = ps.matcher(state.getText().toString());
boolean bs = ms.matches();
if (bs == false) {
    if (ErrorMessage.contains("invalid"))
        ErrorMessage = ErrorMessage + "state,";
    else
        ErrorMessage = ErrorMessage + "invalid state,";
Community
  • 1
  • 1
Fedor Tsyganov
  • 992
  • 13
  • 30