Sunil Kumar Sunil Kumar - 4 months ago 24
Android Question

Switch between alphabetic keyboard and numeric keyboard when user presses a button on keyboard

I am building a custom

Hindi
android soft keyboard
from this tutorial.I have a
key
named
123
on this
keyboard
. I want to change the keyboard layout to another layout which consists of numbers(I have the xml file ready for this numeric layout) but I don't have any idea .How should I do it?
Here is my xml file for hindi alphabets:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="10%p"
android:horizontalGap="0px"
android:verticalGap="0px"
android:keyHeight="60dp">
<Row>
<Key android:codes="0x0905" android:keyLabel="अ" android:keyEdgeFlags="left"/>
<Key android:codes="0x0906" android:keyLabel="आ"/>
<Key android:codes="0x0907" android:keyLabel="इ"/>
<Key android:codes="0x0908" android:keyLabel="ई"/>
<Key android:codes="0x0909" android:keyLabel="उ"/>
<Key android:codes="0x090A" android:keyLabel="ऊ"/>
<Key android:codes="0x090F" android:keyLabel="ए"/>
<Key android:codes="0x0910" android:keyLabel="ऐ"/>
<Key android:codes="0x0913" android:keyLabel="ओ"/>
<Key android:codes="0x0914" android:keyLabel="औ" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="0x0915" android:keyLabel="क" android:keyEdgeFlags="left"/>
<Key android:codes="0x0916" android:keyLabel="ख"/>
<Key android:codes="0x0917" android:keyLabel="ग"/>
<Key android:codes="0x0918" android:keyLabel="घ"/>
<Key android:codes="0x091A" android:keyLabel="च"/>
<Key android:codes="0x091B" android:keyLabel="छ"/>
<Key android:codes="0x091C" android:keyLabel="ज"/>
<Key android:codes="0x091D" android:keyLabel="झ"/>
<Key android:codes="0x0902" android:keyLabel="ं"/>
<Key android:codes="0x0903" android:keyLabel="ः" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="0x091F" android:keyLabel="ट" android:keyEdgeFlags="left"/>
<Key android:codes="0x0920" android:keyLabel="ठ"/>
<Key android:codes="0x0921" android:keyLabel="ड"/>
<Key android:codes="0x0922" android:keyLabel="ढ"/>
<Key android:codes="0x0924" android:keyLabel="त"/>
<Key android:codes="0x0925" android:keyLabel="थ"/>
<Key android:codes="0x0926" android:keyLabel="द"/>
<Key android:codes="0x0927" android:keyLabel="ध"/>
<Key android:codes="0x0928" android:keyLabel="न"/>
<Key android:codes="-100" android:keyLabel="क्षत्रज्ञ" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="0x092A" android:keyLabel="प" android:keyEdgeFlags="left"/>
<Key android:codes="0x092B" android:keyLabel="फ"/>
<Key android:codes="0x092C" android:keyLabel="ब"/>
<Key android:codes="0x092D" android:keyLabel="भ"/>
<Key android:codes="0x092E" android:keyLabel="म"/>
<Key android:codes="0x092F" android:keyLabel="य"/>
<Key android:codes="0x0930" android:keyLabel="र"/>
<Key android:codes="0x0932" android:keyLabel="ल"/>
<Key android:codes="0x0935" android:keyLabel="व"/>
<Key android:codes="-5" android:keyLabel="✖"
android:isRepeatable="true"
android:keyEdgeFlags="right" />
</Row>
<Row android:rowEdgeFlags="bottom">
<Key android:codes="0" android:keyLabel="123"/>
<Key android:codes="0x0936" android:keyLabel="श"/>
<Key android:codes="0x0938" android:keyLabel="स"/>
<Key android:codes="32" android:keyLabel="SPACE" android:keyWidth="40%p" android:isRepeatable="true"/>
<Key android:codes="0x0939" android:keyLabel="ह"/>
<Key android:codes="124"
android:keyLabel="|"
android:popupCharacters=",!.,?"
android:popupKeyboard="@xml/commapopup"
android:keyWidth="10%p"/>
<Key android:codes="-4" android:keyLabel="DONE" android:keyEdgeFlags="right"/>
</Row>
</Keyboard>
</PreferenceScreen>


here is the main java file:

public class SimpleIME extends InputMethodService implements KeyboardView.OnKeyboardActionListener {


private KeyboardView kv;
private Keyboard keyboard;

private boolean caps = false;

@Override
public View onCreateInputView()
{
kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
keyboard = new Keyboard(this, R.xml.qwerty);
kv.setKeyboard(keyboard);
kv.setOnKeyboardActionListener(this);
return kv;
}

private void playClick(int keyCode)
{
AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
switch(keyCode)
{
case 32:
am.playSoundEffect(AudioManager.FX_KEYPRESS_SPACEBAR);
break;
case Keyboard.KEYCODE_DONE:
case 10:
am.playSoundEffect(AudioManager.FX_KEYPRESS_RETURN);
break;
case Keyboard.KEYCODE_DELETE:
am.playSoundEffect(AudioManager.FX_KEYPRESS_DELETE);
break;
default: am.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD);
}
}


@Override
public void onKey(int primaryCode, int[] keyCodes)
{
InputConnection ic = getCurrentInputConnection();
playClick(primaryCode);
switch(primaryCode)
{
case Keyboard.KEYCODE_DELETE :
ic.deleteSurroundingText(1, 0);
break;
case Keyboard.KEYCODE_SHIFT:
caps = !caps;
keyboard.setShifted(caps);
kv.invalidateAllKeys();
break;
case Keyboard.KEYCODE_DONE:
ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
break;
default:
char code = (char)primaryCode;
// Toast.makeText(getApplicationContext(),String.valueOf(code),Toast.LENGTH_SHORT).show();
ic.commitText(String.valueOf(code),1);
}
}

@Override
public void onPress(int primaryCode) {
}

@Override
public void onRelease(int primaryCode) {
}

@Override
public void onText(CharSequence text) {
}

@Override
public void swipeDown() {
}

@Override
public void swipeLeft() {
}

@Override
public void swipeRight() {
}

@Override
public void swipeUp() {
}
}

Answer

In the switch statement Of Onkey method make a case as case 0 (code of 123 key) and Use following:

keyboard = new Keyboard(this, R.xml.number);
kv.setKeyboard(keyboard);
kv.setOnKeyboardActionListener(this);

here number.xml is the layout file for numeric keyboard.Hope this helps.