-1

I want to add a keyboard to an activity in my app. I don't want the default Android keyboard, that pops up once the user clicks a text field and hides when the user clicks somewhere else. I want to make the keyboard static (it is accessible at any time and doesn't disappear) and identical on any device.

Essentially I would like the keyboard to function similarly as in this crossword puzzle app (Image) that I found.

The first solution that came to my head was to make the keyboard entirely by myself. Basically to add and hardcode every single button manually.

Is there a way to do this easier? Is there an available template?

(I am a total beginner in Android, so I might have missed even the most obvious solution)

Thanks.

EDIT: What I would like to achieve is not only that the keyboard stays open all the time, but also that I can easily control the size and position of the keyboard. I want it to cover a specific area of the screen.

user8886048
  • 109
  • 6
Spuu
  • 11
  • 4

2 Answers2

1

You're going to need to make your own keyboard layout, but unless it needs to be a fully-featured keyboard, it won't be too difficult. Put it in its own layout XML, with corresponding IDs for each key.

You can then use <include> in the layouts where it needs to be included. In Java (or Kotlin), you can then make a "helper" class, which takes the root View of the current Activity/Fragment, finds the keys and sets up a listener framework for when a key is pressed.

As for keeping the device's keyboard hidden, check out the answers here.

TheWanderer
  • 16,775
  • 6
  • 49
  • 63
  • Thank you for your quick answer! I'll give it a try. How should I deal with that many similar buttons? Do I have to format them one by one or is it possible to assign them to some sort of a "class"? Is it possible to change the properties of one key and it applies to every other key? – Spuu Oct 23 '18 at 13:25
  • [This is a helpful place to start](https://stackoverflow.com/questions/9577304/how-to-make-an-android-custom-keyboard/45005691#45005691). The whole thread is worth reading. – Outman Oct 23 '18 at 13:26
0

Put these methods inside your activity . For opening the keyboard

  public void openKeyboard() {
    InputMethodManager imm =
            (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm != null) {
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    }
}

For closing the keyboard :

public void closeKeyboard(Activity activity) {
    if (activity != null) {
        InputMethodManager inputMethodManager =
                (InputMethodManager) activity.getSystemService(
                        Activity.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null && activity.getCurrentFocus() != null) {
            inputMethodManager.hideSoftInputFromWindow(
                    activity.getCurrentFocus().getWindowToken(), 0);
        }
    }
}
Ajay Chauhan
  • 1,471
  • 4
  • 17
  • 37
  • This doesn't answer the question. OP is not looking for how to hide or show the device keyboard. – TheWanderer Oct 23 '18 at 13:06
  • the could be the solution as he is facing the issue of keyboard getting disappear if user click somewhere else except text field – Ajay Chauhan Oct 23 '18 at 13:11
  • "and identical on any device." — most people use different keyboards, or different styles on different keyboards. – TheWanderer Oct 23 '18 at 13:12