Skip to content

Commit ecb81b0

Browse files
committed
Done with Android Code!
1 parent aac02f6 commit ecb81b0

File tree

4 files changed

+65
-25
lines changed

4 files changed

+65
-25
lines changed

README.md

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# react-native-hide-keyboard
22

3-
A React Native hide keyboard module is for hiding and showing the keyboard without blurring the textInput component
3+
A React Native hide keyboard module is for hiding and showing the keyboard without blurring TextInput component.
44

55
## Installation
66

@@ -11,11 +11,19 @@ npm install react-native-hide-keyboard
1111
## Usage
1212

1313
```js
14-
import { multiply } from "react-native-hide-keyboard";
15-
16-
// ...
17-
18-
const result = await multiply(3, 7);
14+
import { hideKeyboard, showKeyboard } from "react-native-hide-keyboard";
15+
16+
// For hiding Keyboard
17+
const isItHide = await hideKeyboard();
18+
if (isItHide) {
19+
// Here Keyboard is hide without blurring textInput
20+
}
21+
22+
// For showing keyboard
23+
const isItShow = await showKeyboard();
24+
if (isItShow) {
25+
// Here keyboard is active/displayed
26+
}
1927
```
2028

2129
## Contributing
@@ -24,4 +32,4 @@ See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the
2432

2533
## License
2634

27-
MIT
35+
MIT
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.reactnativehidekeyboard;
22

3+
import android.app.Activity;
4+
import android.view.inputmethod.InputMethodManager;
5+
36
import androidx.annotation.NonNull;
47

58
import com.facebook.react.bridge.Promise;
@@ -11,9 +14,11 @@
1114
@ReactModule(name = HideKeyboardModule.NAME)
1215
public class HideKeyboardModule extends ReactContextBaseJavaModule {
1316
public static final String NAME = "HideKeyboard";
17+
ReactApplicationContext contexts;
1418

1519
public HideKeyboardModule(ReactApplicationContext reactContext) {
16-
super(reactContext);
20+
super(reactContext);
21+
contexts = reactContext;
1722
}
1823

1924
@Override
@@ -22,13 +27,30 @@ public String getName() {
2227
return NAME;
2328
}
2429

25-
26-
// Example method
27-
// See https://reactnative.dev/docs/native-modules-android
2830
@ReactMethod
29-
public void multiply(int a, int b, Promise promise) {
30-
promise.resolve(a * b);
31+
public void hideKeyboard(Promise promise){
32+
try {
33+
InputMethodManager inputMethodManager = (InputMethodManager) contexts.getSystemService(Activity.INPUT_METHOD_SERVICE);
34+
if(inputMethodManager.isAcceptingText()){
35+
inputMethodManager.hideSoftInputFromWindow(
36+
getCurrentActivity().getCurrentFocus().getWindowToken(),
37+
0
38+
);
39+
}
40+
promise.resolve(true);
41+
} catch (Exception e){
42+
promise.resolve(false);
43+
}
3144
}
3245

33-
public static native int nativeMultiply(int a, int b);
46+
@ReactMethod
47+
public void showKeyboard(Promise promise){
48+
try {
49+
InputMethodManager show = (InputMethodManager) contexts.getSystemService(Activity.INPUT_METHOD_SERVICE);
50+
show.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
51+
promise.resolve(true);
52+
} catch(Exception e){
53+
promise.resolve(false);
54+
}
55+
}
3456
}

package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-hide-keyboard",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"description": "A React Native hide keyboard module is for hiding and showing the keyboard without blurring the textInput component",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",
@@ -34,7 +34,13 @@
3434
"keywords": [
3535
"react-native",
3636
"ios",
37-
"android"
37+
"android",
38+
"android",
39+
"react native hide keyboard",
40+
"react-native-hide-keyboard",
41+
"hide keyboard",
42+
"show keyboard",
43+
"react native keyboard"
3844
],
3945
"repository": "https://github.com/MuhammadRafeh/react-native-hide-keyboard",
4046
"author": "Muhammad Rafeh <[email protected]> (https://github.com/MuhammadRafeh)",

src/index.tsx

+13-9
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ const LINKING_ERROR =
99
const HideKeyboard = NativeModules.HideKeyboard
1010
? NativeModules.HideKeyboard
1111
: new Proxy(
12-
{},
13-
{
14-
get() {
15-
throw new Error(LINKING_ERROR);
16-
},
17-
}
18-
);
12+
{},
13+
{
14+
get() {
15+
throw new Error(LINKING_ERROR);
16+
},
17+
}
18+
);
1919

20-
export function multiply(a: number, b: number): Promise<number> {
21-
return HideKeyboard.multiply(a, b);
20+
export function hideKeyboard(): Promise<boolean> {
21+
return HideKeyboard.hideKeyboard();
2222
}
23+
24+
export function showKeyboard(): Promise<boolean> {
25+
return HideKeyboard.showKeyboard();
26+
}

0 commit comments

Comments
 (0)