2

I am creating one application,in my login page i am able to enter uppercase and lowercase letters both,what i want is if user enters letters in uppercase it should convert to lowercase,in short i dont want to allow 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);
jimmy
  • 71
  • 1
  • 1
  • 8

5 Answers5

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
  • 4,501
  • 5
  • 35
  • 78
11
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
  • 6,683
  • 3
  • 22
  • 34
0

Try this :

String properCase (String inputVal) {
// Empty strings should be returned as-is.

if (inputVal.length() == 0) return "";

// Strings with only one character uppercased.

if (inputVal.length() == 1) return inputVal.toUpperCase();

// Otherwise uppercase first letter, lowercase the rest.

return inputVal.substring(0,1).toUpperCase()
    + inputVal.substring(1).toLowerCase();
}
Tufan
  • 3,384
  • 4
  • 32
  • 53
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,664
  • 2
  • 18
  • 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
  • 775
  • 6
  • 15