10

Some 3rd party keyboards have more than one character on each key, for example Better Keyboard 8 has numbers and punctuation above the letters on each key:

Better Keyboard screen shot

Can this be done with the <Key> tag? If so I cannot figure out how. I would appreciate if anybody knows how.

Thanks in advance, Barry

Barry Fruitman
  • 12,316
  • 13
  • 72
  • 135

1 Answers1

28

I figured it out so I am answering my own question.

It cannot be done in XML, but it can be done in Java by overriding the onDraw() method of KeyboardView. This pointless example draws a small letter at the top of each key after the keys are drawn by the parent class:

public class MyKeyboardView extends KeyboardView {
    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Paint paint = new Paint();
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(25);
        paint.setColor(Color.RED);

        List<Key> keys = getKeyboard().getKeys();
        for(Key key: keys) {
            if(key.label != null)
                canvas.drawText(key.label.toString(), key.x + (key.width/2), key.y + 25, paint);
        }
    }
}
Barry Fruitman
  • 12,316
  • 13
  • 72
  • 135
  • This is perfect, thanks! How do you make the secondary characters appear below the key-text instead of above it? – IgorGanapolsky May 18 '12 at 19:32
  • Just tweak the 3rd value in drawText(). It controls the y-position. key.y is the bottom of the key relative to the bottom of the keyboard. – Barry Fruitman May 20 '12 at 16:45
  • Hi @BarryFruitman my requirement same as shown in above keyboard, so i used the code given you in your answer post. But i am getting NullPointerException when i am calling drawText() method in onDraw() overriden method of View class. – Aniket Oct 19 '13 at 12:49
  • Hi @BarryFruitman i have successfully displayed characters using onDraw() as you shown above but problem arises when i am creating keys using paint...i used drawRect()....but nothing helped me...so can you please tell me how can i create keys using paint as you shown above... – Aniket Oct 28 '13 at 13:12
  • Sorry Aniket I cannot answer your question here. I suggest you post another question with a sample of your code. Good luck. – Barry Fruitman Oct 29 '13 at 17:37
  • @BarryFruitman: My requirement was different, but this code helped me a lot. – V_J Jul 29 '15 at 10:28
  • but how to remove the text which i add from the qwerty.xml file in folder xml. – sneha desai Nov 24 '16 at 11:06