aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/activity/Keyboard.java
blob: 9bae7be5aa96119837b8e3a6ebfbbcbaa11b7c32 (plain)
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
package cgeo.geocaching.activity;

import org.eclipse.jdt.annotation.NonNull;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

/**
 * Class for hiding/showing the soft keyboard on Android.
 * 
 */
public class Keyboard {
    private final Activity activity;

    public Keyboard(final @NonNull Activity activity) {
        this.activity = activity;
    }

    public void hide() {
        ((InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
    }

    public void show(final View view) {
        view.requestFocus();
        ((InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(view, 0);
    }

    public void showDelayed(final View view) {
        view.postDelayed(new Runnable() {

            @Override
            public void run() {
                final InputMethodManager keyboard = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                keyboard.showSoftInput(view, 0);
            }
        }, 50);
    }
}