I have an application which shows two languages[Urdu,English] in one edittext, and both of them have their own typeface also, now when both languages are written in the edittext, two cursors are shown for each languages, which seems like a strange quirk, although it does not disturb the text, but the user might get confused like i was about the position of the cursor and where to write. Hope you get the question.
I dont know why this is happening and if it is possible to remove or not...
Changed the language according to this
After doing some research here and there found the answer, first of all you have to create a custom keyboard View which extends keyboardView and in it create static key value variable like
static final int KEYCODE_LANGUAGE_SWITCH_ENG = -102;
static final int KEYCODE_LANGUAGE_SWITCH_URDU = -103;
after that in your IME class where you have implemented the inputMethodService, create the keyboards inside the onInitializeInterface override function. like
mSymbolsKeyboard = new Keyboard(this, R.xml.qwerty2);
mEngQwertyKeyboard = new Keyboard(this, R.xml.eng_qwerty);
after this add these final static keys in the onKey override function as switch cases, and in the cases set the keyboards accordingly:
setKeyboard(mEngQwertyKeyboard);
set two typefaces in one edittext like this function:
public SpannableStringBuilder setWord(String text)
{
tokenList = new ArrayList<String>();
startIndext = 0;
endIndex = 0;
isEnglish = false;
stringBuilder = new SpannableStringBuilder("");
Log.i("CustomUrduKeyBoard","inside setWord text is: "+text+" stringBuilder: "+stringBuilder);
if(text == null)
{
} else
{
for (int i = 0; i < text.length(); i++)
{
// System.out.println(text.charAt(i) + "-Code:" + (int) text.charAt(i)
// + "-" + Integer.toHexString(text.charAt(i)));
if (text.charAt(i) < 255)
{
if (!isEnglish)
{
endIndex = i;
String token = text.substring(startIndext, endIndex);
SpannableStringBuilder urduBLD;
urduBLD = new SpannableStringBuilder("");
urduBLD.append(token);
urduBLD.setSpan(new CustomTypefaceSpan("", urduFont),0,urduBLD.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
stringBuilder.append(urduBLD);
tokenList.add(urduBLD.toString());
startIndext = endIndex;
}
isEnglish = true;
// english
continue;
} else
{
if (isEnglish)
{
endIndex = i;
String token = text.substring(startIndext, endIndex);
SpannableStringBuilder engBLD;
engBLD = new SpannableStringBuilder("");
engBLD.append(token);
engBLD.setSpan(new CustomTypefaceSpan("", englishFont),0,engBLD.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
stringBuilder.append(engBLD);
tokenList.add(engBLD.toString());
startIndext = endIndex;
}
isEnglish = false;
// urdu
continue;
}
}
String token = text.substring(startIndext, text.length());
for (int i = 0; i < token.length(); i++)
{
if (token.charAt(i) < 255)
{
SpannableStringBuilder urduBLD;
urduBLD = new SpannableStringBuilder("");
urduBLD.append(token);
urduBLD.setSpan(new CustomTypefaceSpan("", englishFont),0,urduBLD.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
stringBuilder.append(urduBLD);
tokenList.add(urduBLD.toString());
break;
} else
{
SpannableStringBuilder engBLD;
engBLD = new SpannableStringBuilder("");
engBLD.append(token);
engBLD.setSpan(new CustomTypefaceSpan("", urduFont),0,engBLD.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
stringBuilder.append(engBLD);
tokenList.add(engBLD.toString());
break;
}
}
}
return stringBuilder;
}