4

I am creating one application, in my login page I am able to enter uppercase and lowercase letters both, what I want is if the user enters letters in uppercase it should convert to lowercase, in short, I don't want to allow the user to enter uppercase letters.

<EditText
    android:id="@+id/email"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:background="@drawable/f_name"
    android:gravity="center"
    android:hint="EMAIL"
    android:inputType="textEmailAddress"
    android:textColor="#676767"
    android:textSize="20sp"/>

<EditText
    android:id="@+id/password"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_below="@id/email"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="5dp"
    android:background="@drawable/f_name"
    android:gravity="center"
    android:hint="PASSWORD"
    android:inputType="textPassword"
    android:textColor="#676767"
    android:textSize="20sp"/>

Code

etemail = (EditText) findViewById(R.id.email);
etemail.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_FLAG_MULTI_LINE);
Ramesh R
  • 7,009
  • 4
  • 25
  • 38
jimmy
  • 91
  • 1
  • 1
  • 9

4 Answers4

16

In the XML, add this for your edittext,if you want to restrict the user to input lowercase only.

android:digits="abcdefghijklmnopqrstuvwxyz "

OR

android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "

if you also want to allow to input numbers.

Narendra Singh
  • 3,990
  • 5
  • 37
  • 78
13
EditText edit = (EditText)findViewById(R.id.email);
String input;
input = edit.getText().toString();
input = input.toLowerCase(); //converts the string to lowercase
   or 
input = input.toUpperCase(); //converts the string to uppercase
Ramesh R
  • 7,009
  • 4
  • 25
  • 38
0

I could suggest you a small trick, You could add a textchangelistener to the edit text and de-capitalize every letter as soon as he changes the text.

   Field1 = (EditText) findViewById(R.id.email);

   Field1.addTextChangedListener(new TextWatcher() {

   @Override
   public void afterTextChanged(Editable s) 
   {
      Field1.setText(s.toString().toLowerCase());
    }

   @Override    
   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   @Override    
   public void onTextChanged(CharSequence s, int start,
     int before, int count) 
   {
      Field1.setText(s.toString().toLowerCase());
   }
  });
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
  • The method toLowerCase() is undefined for the type CharSequence – jimmy May 26 '15 at 05:43
  • Thank you for notifying it. – Uma Kanth May 26 '15 at 05:45
  • 1
    A bit late, but I think the given example will get you into an infinite loop and the app will be killed by the OS. Correct me if I am wrong – Todor Kostov Jun 15 '16 at 12:05
  • @TodorKostov That is true. In fact here's a thread with the same problem. http://stackoverflow.com/questions/35719262/capitalize-every-word-in-edit-text-while-typing/35720381#35720381 There is a work-around for this. However I prefer using `android:digits` for this. – Uma Kanth Jun 15 '16 at 14:04
0

You need to add this code to convert an EditText to lowercase, while a user is typing.

//Add this line to the onCreate method
editText.addTextChangedListener(editTextWatcher);

//Create the TextWatcher as a class variable (outside methods).
private final TextWatcher editTextWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        String s = editable.toString();
        if (!s.equals(s.toLowerCase())) {
            s = s.toLowerCase();
            R.id.editText.setText(s);
            R.id.editText.setSelection(R.id.editText.getText().toString().length());
        }
    }
};
Mujeeb
  • 995
  • 1
  • 8
  • 18