Reputation: 91
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);
Upvotes: 4
Views: 38697
Reputation: 7077
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
Upvotes: 14
Reputation: 1225
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());
}
}
};
Upvotes: 0
Reputation: 5629
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());
}
});
Upvotes: 0
Reputation: 4001
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.
Upvotes: 16