-
Notifications
You must be signed in to change notification settings - Fork 4
/
KeyboardUtils.java
executable file
·74 lines (64 loc) · 2.22 KB
/
KeyboardUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.bytesizebit.androidutils;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
/***********
* Android Utils
* Created by Shahar Barsheshet on 17/10/2015.
* bytesizebit@gmail.com
* www.bytesizebit.com
***********/
public class KeyboardUtils {
private KeyboardUtils() {
}
/**
* Hide soft keyboard
*/
public static void hideSoftKeyboard(Activity activity) {
View view = activity.getWindow().peekDecorView();
if (view != null) {
InputMethodManager inputmanger = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
/**
* Hide soft keyboard
*/
public static void hideSoftKeyboard(Context context, View view) {
view.clearFocus();
InputMethodManager inputmanger = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
/**
* Show soft keyboard
*/
public static void showSoftKeyboard(Activity activity) {
showSoftKeyboard(activity, null);
}
/**
* Show soft keyboard
*/
public static void showSoftKeyboard(Context context, View view) {
view.setFocusable(true);
view.setFocusableInTouchMode(true);
view.requestFocus();
InputMethodManager inputManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(view, 0);
}
/**
* Toggle soft keyboard state
*/
public static void toggleKeyboradState(Context context, EditText edit) {
edit.setFocusable(true);
edit.setFocusableInTouchMode(true);
edit.requestFocus();
InputMethodManager inputManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}