Anky An
Anky An

Reputation: 640

How to set same EditText length (width) for all languages on all devices?

In my app, I set the maximum length for an EditText as 30. This is fine when I input English. However, when I type Chinese, I can type 30 characters as well. The Chinese character doubles the width of an English character. The width of the EditText also doubles in comparison to when I type English. This is not what I want.

How can I set 30 English characters as the maximum? When user types in a different language, the width of the EditText can't go beyond when it has 30 English characters. Thanks

Upvotes: 0

Views: 226

Answers (2)

ali alkiswany
ali alkiswany

Reputation: 1

try this code:

String language = Locale.getDefault().getDisplayLanguage().toString();

 if(language.equals(en) {

 // use set filter to limit input length in the edit text for english.
 int maxLength = 30;    
 editText.setFilters(new InputFilter[] {new 
 InputFilter.LengthFilter(maxLength)});

 } else {

// put the limit for other languages

}

hope it helps you .

Upvotes: 0

Ramana V V K
Ramana V V K

Reputation: 1251

You can get keyboard language by first detecting the locale of device keyboard and then getting the Locale object from it then set the maximum length. For example

 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();
    String localeString = ims.getLocale();
    Locale locale = new Locale(localeString);
    if( (locale.getDisplayLanguage()).equalIgnoreCase("english"))
      {
        editText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(30)});
       }

Upvotes: 1

Related Questions