Linga
Linga

Reputation: 10545

Android Capitalize characters not working in phone

in my app, I want to allow the user to enter only upper case letters. So I used capitalize property.

This is one part of code

<EditText
    android:id="@+id/et5"
    android:layout_width="140dp"
    android:layout_height="wrap_content"
    android:layout_x="127dp"
    android:layout_y="219dp"
    android:maxLength="25"
    android:ems="10"
    android:singleLine="true"
    android:capitalize="characters"
    android:text="" />

In some cases I also tried this

android:inputType="textCapCharacters"

It works fine in emulator, But it is not working in phone. In phone, it shows lower case letters. What is the mistake here and how to overcome?

Upvotes: 3

Views: 4467

Answers (4)

ErickBergmann
ErickBergmann

Reputation: 699

Try this:

editText.setFilters(new InputFilter[] {new InputFilter.AllCaps()});

and set EditText xml property to:

android:inputType="text|textCapCharacters"

Hope it helps :)

Upvotes: 8

Sanket Pandya
Sanket Pandya

Reputation: 1095

Try working with this in java class like this :

textView.setText(text.toUpperCase());

you can also use Textwatcher method to apply capitalize on text changed

To make it work from xml you should try this:

android:inputType="textCapCharacters" 

Upvotes: 1

Sumit Sharma
Sumit Sharma

Reputation: 1867

Try this

<EditText
android:inputType="textCapCharacters" />


<EditText
android:inputType="textCapWords" />

or you can do programmatically in java file

 String str = et.getText.toString().toUpperCase();

Upvotes: 0

Furqi
Furqi

Reputation: 2403

edittext.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {            

}
    @Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {             
}
@Override
public void afterTextChanged(Editable arg0) {
      String s=arg0.toString();
  if(!s.equals(s.toUpperCase()))
  {
     s=s.toUpperCase();
     edittext.setText(s);
  }
}
});     

Upvotes: 0

Related Questions