Posts

Popup Keyboard for Special Characters in Android

Popup Keyboard for Special Characters in Android It turns out that when you hold down a key, Android gives you the option of popping up another keyboard. I figured out many of the details from Stack Overflow . Step 1 Create a new file called xml/popup.xml . <?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android"     android:keyWidth="10%p"     android:horizontalGap="0px"     android:verticalGap="0px"     android:keyHeight="60dp"     > </Keyboard> Step 2 Replace the correct line in xml/keys_layout.xml . <Key android:codes="101" android:keyLabel="e" android:popupKeyboard="@xml/popup" android:popupCharacters="éèêë"/> Step 3 Add the following code to your Java. <Key android:codes="101" android:keyLabel="e" android:popupKeyboard="@xml/popup" android:popupChar...

Suggestions in Android

One of the first things you may choose to add to your project is suggestions. There are probably many ways to do this. Here's how I did it. State I chose to store my state in some variables, alongside 'caps'. private boolean caps = false; private String composingText = ""; private String[] suggestions = {" ", " ", " "}; I'm not sure if this is the best way, but it's how I chose to proceed. Note that I chose the blank suggestions to consist of a single space, rather than an empty string, because if you set a Key's label to be an empty string, turning on caps/shift will cause the Android system to throw an error. setComposingText When we set up our project, we made use of Android Authority's tutorial. They use inputConnection's commitText, but inputConnection also has setComposingText. We're going to use both of these frequently, so let's create a useful method. private void setComposingText(St...

Shift Keys on Android

Shift Keys on Android When you press the CAPS button, a little green dot appears. At this point, you might be wondering: why? Source code Diving into the Keyboard.java source code , we find the following: @Override public void onCreateInputView() { public static final int KEYCODE_SHIFT = -1; ... public boolean setShifted(boolean shiftState) {     for (Key shiftKey : mShiftKeys) {         if (shiftKey != null) {             shiftKey.on = shiftState;         }       }       if (mShifted != shiftState) {         mShifted = shiftState;         return true;     }       return false; } ... if (key.codes[0] == KEYCODE_SHIFT) {     // Find available shift key slot and put this ...

Available Resources for Android

Available Resources for Android I wasn't able to find many good tutorials. As I mentioned on another post, you can start with Android Authority's blog or a copy of the source code . I learned about composing text from this article . Below are some other references you might find useful. Android Docs An obvious place to start, but a lot of details are missing. Keyboard KeyboardView InputMethodService Input Connection Keyboard.Key (xml) Keyboard.Row (xml) Other documentation from Android A working code example direct from Android - very useful !  Android Developer tutorial - pretty much the same as all the others Android Developer blog - again, not much new here Stack Overflow Popup Keyboard - useful for special characters Custom Theme Showing and hiding rows (also see my post) Links to code repos AnySoftKeyboard GaroKeypad SquareKeyboard Blogs Chris Black - lots of good links Let me know if you find anything else that adds to som...

Android Toasts work!

If you've been using Custom Keyboards in Android, you've probably realised that it's quite restrictive. However, good news: you can use Toasts! I found this on Stack Overflow . Here's how: private Handler mHandler;   @Override public void onCreateInputView() {     // all your other code first...     mHandler = new Handler(); } @Override public void onKey(int primaryCode, int[] keyCodes) {     // And then...     mHandler.post(new Runnable() {                   @Override         public void run() {             Toast.makeText(MyIntentService.this, "Hello Toast!", Toast.LENGTH_LONG).show();                       }     }); } That's...

Getting Started - Android

Getting Started Choosing a tutorial It seemed like the tutorials out there are pretty limited. I started with the one from Android Authority , but it's missing quite a lot. Luckily someone has linked a complete video in the comments , but you don't want to watch a half hour video, do you? Best to just jump to their source code . Below I try to detail all the missing steps. Let me know if I've forgotten anything. Creating Project When you create the project, select "Add no Activity". Missing imports If you're new to Android, it often helps to have the imports. import android.inputmethodservice.InputMethodService; import android.inputmethodservice.Keyboard; import android.inputmethodservice.KeyboardView; import android.text.TextUtils; import android.view.KeyEvent; import android.view.View; import android.view.inputmethod.InputConnection; Add the missing Android.Manifest <manifest xmlns:android="http://schemas.android.com/apk/res/android...