I come from a javascript background and this is my first brush with java.
I've been following this guide. I've got a fully working keyboard and now I'd like to start customizing it. First thing I want to do is to display symbols when a key is pressed.
After digging around it looks like setInputView might be what I need, but I'm not sure how to call it? There's a switch statement that controls the casing of symbols that I thought I'd be able to hook into but when I call the method I get the error Attempt to invoke interface method 'void android.inputmethodservice.KeyboardView$OnKeyboardActionListener.onPress(int)' on a null object reference
which I assume is because the function i THINK I'm calling, I'm not actually calling.
The following is the relevant java code I have so far:
package com.example.crhistian.spanishsymbolskeyboard;
import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.Keyboard;
import android.view.View;
import android.view.inputmethod.InputConnection;
import android.view.KeyEvent;
import android.media.AudioManager;
public class SpanishSymbols extends InputMethodService
implements KeyboardView.OnKeyboardActionListener{
private KeyboardView kv;
private Keyboard keyboard;
private KeyboardView spanishkv;
private boolean caps = false;
@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_MODE_CHANGE :
setInputView(spanishkv);
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;
if(Character.isLetter(code) && caps){
code = Character.toUpperCase(code);
}
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() {
}
@Override
public View onCreateInputView() {
kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
keyboard = new Keyboard(this, R.xml.keyboard);
spanishkv = (KeyboardView)getLayoutInflater().inflate(R.layout.spanishsymbols, null);
spanishkv.setKeyboard(keyboard);
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);
}
}
}
You need to set the listener on the spanish keyboard, something like