Keyboards and related settings in Android systems
6 minute(s) read
|
Published on: May 03, 2021
Updated on: Dec 14, 2021
|
In this part of the educational article, we want to talk about the keyboards of Android systems and apply some desired settings and changes to them. In this part of the tutorial, we want to explore how to hide and show it and also use some special libraries that can be notified of events or, in other words, to inform us of events.
In this part of the tutorial, what you will learn is as follows:
- How to manage Android keyboard - How to hide and show Android keyboard - Remove Android-keyboard using InputMethodManager class
- Manage and apply some custom keyboard changes using the KeyboardVisibilityEvent class
- Announce some keyboard-related events
- Eliminate the appearance of the Android-keyboard
Keyboard management
It may happen to app developers and programmers that they do not display when they need a keyboard, and when they do not need an Android keyboard at all, the keyboard appears, which may cause some problems. To do this, we need to make changes and settings to finally manage the Android-keyboard and appear when we need it. To do this, we use the InputMethodManager class to manage the keyboard. Then we use the KeyboardVisibilityEvent library to add delete and no display to the keyboard. We can even define a Listener that notifies us of keyboard deletion and non-display events.
To better understand, let's give an example. For example, consider the form that the user needs to keep the keyboard hidden and not displayed after entering his personal information using the keyboard. In this case, it will cause problems. This is why we have to create a project and apply its special settings to it so that the keyboard is hidden and not displayed when the user does not need it.
1- To do this, we create a project in Android Studio with the desired name KeyboardManagement.
2- It should be noted that the type of activity used in this project is Empty Activity.
3- The language used in this tutorial to create this project is Java.
4- In the activity layout, we define three widgets of EditText, TextView, and Button types.
5- The codes that should be in activity_main.xml are as follows:
6- In the next step, we need to add a listener to place the text that enters inside EditText on TextView and set.
7- The codes that should be in MainActivity.java are as follows:
8- We run the project and enter a text to test.
9- After running the project, we see that the keyboard is still displayed and appears.
10- We have to apply settings that disappear and hide after using the keyboard and pressing a keyboard button.
11- To do this, we add a method called closeKeyboard to the activity and then the onCreate method.
12- As follows:
In the code inserted, we put the top trace on the view on which the focus is done in a variable of type focus. We have arbitrarily selected the name of this variable focusView.
In this project, if the user clicks on EditText, the view will be saved.
I define the code listed above to test whether focused is null in the following lines. Inside this condition, we have created an instance of the InputMethodManager class with the desired name imm.
Finally, the SoftInput keyboard will be hidden using the hideSoftInputFromWindow method.
13- Then, we call the method in the Listener button. Like the following:
14- The code that should be included in MainActivity.java is as follows:
15- I will implement the project.
16- After running the project, we will see that the keyboard that appeared will be hidden after clicking the button.
KeyboardVisibilityEvent
In the above part, we hid the Android keyboard using the internal class of Android and did not use any library for this purpose. Still, we want to manage the Android keyboard using the library in this part of the tutorial. We want to use the KeyboardVisibilityEvent library. In using this library and being able to easily hide or show the Android keyboard, we can also be informed of events that occur in it. In other words, this library reports showing, hiding, and other keyboard operations, and we can use it to stay up to date with many events.
It is worth mentioning that you can buy, download and use the mentioned library through the following link:
https://github.com/yshrsmz/KeyboardVisibilityEvent
According to the description given on the GateHub site, to use this library, we must put the lines and text below in the build. Griddle. Therefore, to use this library easily and without any problems, you must put the following code in the build. Griddle:
Note:
Instead of the LATEST_VERSION in the code and text above, we must enter the latest version that I use.
After adding this library, we must sync the project to be successfully added and synced.
1- To be able to hide the Android keyboard using the library, we must enter the texts and codes listed below in the Listener button method instead of the close keyboard method, such as the following:
UIUtil is one of the classes in this library. The Android-keyboard is hidden using the hide keyboard method of this library. In contrast, the show keyboard method performs the opposite operation of the previous method if this method is also used to display the Android keyboard.
It should be noted that normally if the user focuses on EditText once, the Android keyboard will be displayed automatically, but there may be some exceptions and problems. To prevent this problem, we use a method that displays the keyboard.
Unlike the hide keyboard method, which has a parameter, this method must also enter the second parameter of this method.
Note that for the second parameter of this method, we must enter the name EditText.
2- Add this method to onCreate. As follows:
3- We can create a KeyboardVisibilityEvent method using the setEventListener method in the KeyboardVisibilityEvent class of this library, like the code listed below.
Keyboard status change is received via parameter b. To display the output of this method, I define a condition that the output of each mode appears as a Toast:
4- The complete code that should be in the MainActivity.java section is as follows:
5- We will implement the project.
6- After running this project, we will see that the appropriate messages "Keyboard appeared" and "Keyboard deleted" are displayed at the appropriate times. When the user presses a button to appear on the keyboard, the message "Keyboard appeared." And when the user presses a button to hide the keyboard, the message "Keyboard deleted" will appear.
In this part of the tutorial, you learned how to:
- Manage Android-keyboard
- How to make the Android-keyboard appear
- How to hide the keyboard when you do not need to
- How to be notified of keyboard events
About our Android and iOS app development services