Question

How to capitalize all letters when user start typing something in AutoCompleteTextView?

I've tried on this way:

txtProductInput.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                String upper = txtProductInput.getText().toString().toUpperCase();
                txtProductInput.setText(upper);

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });

But when user start typing application breaks on line

txtProductInput.setText(upper);

Stack trace log:

02-07 13:17:31.708: E/AndroidRuntime(12611): FATAL EXCEPTION: main
02-07 13:17:31.708: E/AndroidRuntime(12611): java.lang.StackOverflowError
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.text.method.ReplacementTransformationMethod$ReplacementCharSequence.getChars(ReplacementTransformationMethod.java:151)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.text.TextUtils.getChars(TextUtils.java:70)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.text.TextUtils.indexOf(TextUtils.java:103)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.text.StaticLayout.generate(StaticLayout.java:182)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.text.DynamicLayout.reflow(DynamicLayout.java:284)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.text.DynamicLayout.<init>(DynamicLayout.java:170)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.widget.TextView.makeSingleLayout(TextView.java:5843)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.widget.TextView.makeNewLayout(TextView.java:5741)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.widget.TextView.checkForRelayout(TextView.java:6280)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.widget.TextView.setText(TextView.java:3547)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.widget.TextView.setText(TextView.java:3405)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.widget.EditText.setText(EditText.java:80)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.widget.TextView.setText(TextView.java:3380)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at com.DEM.productionmonitor.LineProduct$1.onTextChanged(LineProduct.java:99)
Was it helpful?

Solution

'ZanoOnStack' gave a good idea so if anyone else need that this is a proper solution:

InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.AllCaps();
txtProductInput.setFilters(FilterArray);

On this way it is possible to add more other types of filters.

OTHER TIPS

<AutoCompleteTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:inputType="textCapCharacters"
/>

Have you tried in xml layout?

android:textAllCaps="true"

Try to insert it

String upper = s.getText().toString().toUpperCase();
s.setText(upper);

in afterTextChanged

it works for me

@Override
public void afterTextChanged(Editable arg) {
    String s=arg.toString();
    if(!s.equals(s.toUpperCase())){
        s=s.toUpperCase(); 
        tv.setText(s);
        tv.setSelection(tv.getText().length());

    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top