Search code examples
androidxmllayoutandroid-softkeyboardgesture

How to change keyboard layout in onSwipe method?


I would like to know how to change keyboard layout in onSwipe method.
I have qwerty.xml with letters, but I need numbers and symbols.
These two groups of characters are in numeric.xml.
The numeric.xml will be show and qwerty.xml will be hidden when I swipe left or right.

If you need any part of my code please ask.


Solution

  • You need to change the layout on your keyboard object in your InputManagerService. Then you need to invalidate all keys to get the keyboard to be redrawn. Like so:

     public class MyKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener
     {
    
        private KeyboardView myKeyboardView;
        private Keyboard myKeyboard;
    
        public View onCreateInputView()
        {
    
           viewOfKeyboard = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard1, null);
           theKeyboardLayout = new Keyboard(this, R.xml.keyboardlayout);
           viewOfKeyboard.setKeyboard(theKeyboardLayout);
           viewOfKeyboard.setOnKeyboardActionListener(this);
           return myKeyboardView;
        }
    
        public void swipeRight()
        {
           if(isQwerty)
           {
             myKeyboard = new Keyboard(this, R.xml.numeric);
             //Creates a new keyboard object.
             myKeyboardView.setKeyboard(myKeyboard);
             //Assigns the new keyboard object to the keyboard view
             myKeyboardView.invalidateAllKeys();
             //Makes android redraw the keyboard view, with the new layout.
             isqwerty = false;
           }
           else
           {
             myKeyboard = new Keyboard(this, R.xml.qwerty); 
             myKeyboardView.setKeyboard(myKeyboard);
             myKeyboardView.invalidateAllKeys();
             isqwerty = true;
           }
    
           //.........the rest of MyKeyboard's methods     
    
        }