2

I have an EditText to get description and want to set max length 145 chars. I have set maxlength 145 in XML. This works correctly for English. But for languages other than English, being specific, Marathi, Hindi etc it doesn't work properly. I counts each symbol as separate character.

For example: "व्ही"

this is considered as one character in Hindi, and it's length should be 1, but it returns me length 2.

I have seen the solution in this link

But it doesn't work for Hindi/Marathi language because it cannot detect connected characters. How to detect connected characters?

Thanks in advance :)

Community
  • 1
  • 1
Vrushali Sabnis
  • 197
  • 2
  • 12

2 Answers2

0

Plz try this code

String str = "व्ही";
int count = 0;

for(int i=0; i<str.length(); i++)
{
    if(!isMark(str.charAt(i)))
        count++;
}

textview.setText(count);
Neel Patel
  • 2,028
  • 18
  • 17
0

Try this:

import java.text.BreakIterator;
import java.util.Locale;

public class MyClass {

    private int graphemeClusterCount(Locale locale) {

        String text = "व्ही";

        BreakIterator breakIterator = BreakIterator.getCharacterInstance(locale);

        breakIterator.setText(text);

        int count = 0;

        int start = breakIterator.first();
        for (int end = breakIterator.next();
             end != BreakIterator.DONE;
             start = end, end = breakIterator.next()) {

            count++;
        }

        return count;
    }
}

Some references:

Unicode FAQ on what it calls grapheme clusters: http://unicode.org/faq/char_combmark.html#7

Sample code from Android documentation: https://developer.android.com/reference/java/text/BreakIterator.html

ICU has a BreakIterator implementation, too: http://site.icu-project.org/ and you can import in Gradle:

compile 'com.ibm.icu:icu4j:58.2'
nandsito
  • 3,782
  • 2
  • 19
  • 26