Hide Android soft keyboard if it is open - android

I have three edit-text fields. Of these fields I want to display the soft input keyboard only for the first field and disabled for the later two fields sice these are date and time fields.
Edit-Text 1 //Show the keyboard
Edit-Text 2 and 3 //Hide the keyboard
By using below code I'm able to disable the keyboard for field 2 and 3 but when the user has focus on field 1 the keyboard appears but didn't hide when user taps on field 2 or 3. Although when field 2 or 3 is tapped first no keyboard appears.
//Code to disable soft input keyboard
public static void disableSoftInputFromAppearing(EditText editText) {
if (Build.VERSION.SDK_INT >= 11) {
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
editText.setTextIsSelectable(true);
} else {
editText.setRawInputType(InputType.TYPE_NULL);
editText.setFocusable(true);
}
How can I hide the soft input keyboard if its already open?

//activity
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager)activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
// fragment
public void hideSoftKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}
// If edit-text loses the focus, hiding keyboard
edTxtMessage.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (isEditable){
v.setFocusable(true);
v.setFocusableInTouchMode(true);
} else {
edTxtMessage.setFocusable(false);
}
return false;
}
});
edTxtMessage.setOnFocusChangeListener(new View.OnFocusChangeListener({
#Override
public void onFocusChange(View view, boolean b) {
if (!b){
hideKeyboard(getContext(), view);
}
}
});
private void hideKeyboard(Context context, View view) {
if (view != null) {
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}

Easy way is using the xml
android:inputType="none"
android:textIsSelectable="true

You can set the two attributes to your edittext
android:focusable="false"
android:focusableInTouchMode="false"
Note: By this Your edittext is clickable for the date and time not focus to softkeyboard

Check if the current focus is not null, otherwise it might lead to a null pointer exception
if (getCurrentFocus() != null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}

Related

Date picker dialog hiding behind soft keyboard

As shown in the picture, I have a date picker dialog which is launched on click of an Edittext control. But it is partially hidden behind the soft keyboard which was already open from previous Edittext control. How do I hide the soft keyboard when I launch the date picker dialog?
I have already tried the code from 1 and 2, but doesn't help.
You can check on onClick event, if the keyboard is open. If its open you can forcibly close it.
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
if this does not help you can also try the below code:
You should check for onFocus change event on the the view
View.OnFocusChangeListener listener;
listener = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (v.getId() == R.id.address && !hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
};

Show keyboard when an EditText gains focus and hide keyboard when it loses focus

The question is self explanatory.
Show soft keyboard when your edit text gains focus and hide keyboard when it loses focus. Here is the code that I have used.
this.newTaskTitle = (EditText) taskCreationView.findViewById(R.id.newTaskTitle);
this.newTaskTitle.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
//Set up input manager
InputMethodManager keyboardManager = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE
);
if(hasFocus) {
Log.i(TAG,"hasFocus");
//Display keyboard
keyboardManager.showSoftInput(
v,
InputMethodManager.SHOW_IMPLICIT
);
} else {
Log.i(TAG,"lostFocus");
//Hide keyboard
keyboardManager.hideSoftInputFromInputMethod(
v.getWindowToken(),
0
);
}
}
});
Even though the else executes when the EditText loses focus, the keyboard is never hidden. Why would that be ?
Isn't this the right way to hide the keyboard ?
I think, there is no need to set OnFocusChangeListener. Call below method from onClick of your button and after calling this method set visibility GONE of your EditText.On gaining focus soft keyboard get open automatically.
private void hideKeyBoard(Context context, EditText editText) {
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}

Can not open soft keyboard in EditText control

I want to create 1 edit text with the below condition:
- User can not focus on this control in normal.
- When user click on this control, soft-keyborad is displayed and user can input into this control
- When user press enter on this soft-keyborad or back on device, it is closed and back to normal view with control is not focus.
I tried the below code but not work :(
When starting, control is not focus: ok
When click on control, at the first click, control is focus but not display soft-keyborad
In the second click, display soft-keyborad
When press enter button in soft-keyborad: back to screen with control is not focus: OK
When press back button device, back to screen with control is still focus : not ok
public void onCreate(Bundle savedInstanceState)
{
final EditText txtSearch = (EditText)this.findViewById(R.id.p60004_txt_search_str);
txtSearch.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
txtSearch.setFocusable(true);//(false);
txtSearch.setFocusableInTouchMode(true);
txtSearch.requestFocus();
}
});
txtSearch.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
String strSearch = txtSearch.getText().toString();
if (strSearch != null && strSearch != ""){
searchFriend(UserAPIConstants.FRIEND_SEARCH_TYPE_SC, strSearch);
}
hideSoftKeyboard(v);
txtSearch.setFocusable(false);
txtSearch.setFocusableInTouchMode(false);
}
return false;
}
});
public void hideSoftKeyboard (View view) {
InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
In your xml which has edittext put these values for the layout node
android:focusable="true"
android:focusableInTouchMode="true"
and dont put any focusable or focusable in touch mode attributes for your edittext..
Then in your code in onKey method remove thse lines..
txtSearch.setFocusable(false);
txtSearch.setFocusableInTouchMode(false);
and put
txtSearch.clearFocus();
And You should override this method
onBackPressed()
like this..
#Override
public void onBackPressed() {
txtSearch.clearFocus();
//hide the soft keyboard..
}
try commenting
hidekeyboard(v);
and the changes that Alex Lockwood suggested.
use this in onClick()
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
to close the keypad use
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);

How To Display the Soft Keyboard in my Application with the Galaxy Tab [duplicate]

I am trying to force the Soft Keyboard open in an Activity and grab everything that is entered as I want to handle the input myself, I don't have an EditText. Currently I have tried this but it does not work. I would like the Soft Keyboardto open below mAnswerTextView (Note: it is a TextView not EditText).
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// only will trigger it if no physical keyboard is open
mgr.showSoftInput(mAnswerTextView, InputMethodManager.SHOW_IMPLICIT);
how do I force the Soft Keyboard open
How do I gab everything that is entered so that I can handle each character. I would like to flush each character from the Soft Keyboard after I have handled it. ie, the user should not be able to enter whole words in the Soft Keyboard.
try this to force open soft keyboard:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
then you can to use this code to close the keyboard:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(_pay_box_helper.getWindowToken(), 0);
You'll probably need to have an editable text area of some kind to take focus. You can probably have one invisible or on a transparent background with no cursor, though. You may need to toy around with the focusability settings for the view.
Use a TextWatcher to check for edits to that EditText with addTextChangedListener, or if you need an even-lower-level hook, set the textview's key listener with its setOnKeyListener() method. See the KeyListener documentation.
Use this call to force the soft keyboard open:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
.showSoftInput(myEditText, InputMethodManager.SHOW_FORCED);
and this one to close it:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
note that this is really not recommended - forcing the keyboard open is kind of messy. What's your use case that really necessitates your taking user input without a normal edit box and requires eating user input on a key-by-key basis without echoing it back?
To force the keyboard to open I used
this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
it worked for me.
Sometimes the other answers won't work.
Here is another way..
It will force the keyboard to show when the activity starts by listening to the window focus. onWindowFocusChanged() it will clear and request focus of the EditText, then set the soft input mode to visible and set the selection to the text in the box. This should always work if you are calling it from the activity.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
mEditText.clearFocus();
mEditText.requestFocus();
getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
mEditText.setSelection(mEditText.getText().toString().length());
}
}
You may also need
mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT);
}
}
});
Edit: I have also seen the keyboard not open inside nested fragments, beware of those kinds of situations.
if you want to control soft keyboard inside activity then use this code:
//create soft keyboard object
InputMethodManager imm = (InputMethodManager)this.getSystemService(INPUT_METHOD_SERVICE);
//1.USE
your_view.setFocusableInTouchMode(true); //Enable touch soft keyboard to this view
//or
your_view.setFocusable(true); //Enable keyboard to this view
imm.showInputMethod(your_view, InputMethodManager.SHOW_IMPLICIT);
//2.USE show keyboard if is hidden or hide if it is shown
imm.toggleSoftInputFromWindow(your_view.getWindowToken(),InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
//or
imm.toggleSoftInputFromWindow(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
//3.USE (you cannot control imm)
this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
//4.USE (with Dialog)
Dialog d = new Dialog(this, android.R.style.Theme_Panel);
d.getWindow().setTitle(null);
d.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
d.setOnKeyListener(keyListener);
d.setCanceledOnTouchOutside(true);
d.setCancelable(true);
d.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
d.show();
//to hide keyboard call:
d.dismiss();
//if you want get soft keyboard visibility call:
d.isShowing();
Sadly, as much as I'd have liked to up-vote one of the replies, none worked for me. It seems the solution is to wait for the layout phase to complete. In the code below, notice how I check if the showKeyboard method returns TRUE, and that's when I remove the global layout listener. Without doing this, it was hit and miss. Now it seems to work perfectly.
You need to do the below ideally in onResume()
#Override
public void onResume()
{
super.onResume();
ViewTreeObserver vto = txtTaskTitle.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
if (txtTaskTitle.requestFocus())
{
if (showKeyboard(getContext(), txtTaskTitle))
{
txtTaskTitle.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
}
});
}
public static boolean showKeyboard(Context context, EditText target)
{
if (context == null || target == null)
{
return false;
}
InputMethodManager imm = getInputMethodManager(context);
((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(target, InputMethodManager.SHOW_FORCED);
boolean didShowKeyboard = imm.showSoftInput(target, InputMethodManager.SHOW_FORCED);
if (!didShowKeyboard)
{
didShowKeyboard = ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(target, InputMethodManager.SHOW_FORCED);
}
return didShowKeyboard;
}
Working Great.........
edt_searchfilter_searchtext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
edt_searchfilter_searchtext.post(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getFragmentActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edt_searchfilter_searchtext, InputMethodManager.SHOW_IMPLICIT);
}
});
}
}
});
Below line call when you want to open keyboard
edt_searchfilter_searchtext.requestFocus();
if(search.getText().toString().trim().isEmpty()) {
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(search.getWindowToken(), 0);
}
I have tested and this is working:
...
//to show soft keyboard
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
//to hide it, call the method again
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
Simply, using adding 2 lines will work like a charm:
If using XML
android:focusable="true"
android:focusableInTouchMode="true"
Else in Java:
view.setFocusableInTouchMode(true);
view.requestFocus();
You can use this KeyboardHelper.java class
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
/**
* Created by khanhamza on 06-Mar-17.
*/
public class KeyboardHelper {
public static void hideSoftKeyboard(Context context, View view) {
if (context == null || view == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
public static void hideSoftKeyboardForced(Context context, View view) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromInputMethod(view.getWindowToken(), 0);
}
public static void hideSoftKeyboard(Context context, EditText editText) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
}
public static void showSoftKeyboard(Context context, EditText editText) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
editText.requestFocus();
}
public static void showSoftKeyboardForcefully(Context context, EditText editText) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
editText.requestFocus();
}
}

Forcing the Soft Keyboard open

I am trying to force the Soft Keyboard open in an Activity and grab everything that is entered as I want to handle the input myself, I don't have an EditText. Currently I have tried this but it does not work. I would like the Soft Keyboardto open below mAnswerTextView (Note: it is a TextView not EditText).
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// only will trigger it if no physical keyboard is open
mgr.showSoftInput(mAnswerTextView, InputMethodManager.SHOW_IMPLICIT);
how do I force the Soft Keyboard open
How do I gab everything that is entered so that I can handle each character. I would like to flush each character from the Soft Keyboard after I have handled it. ie, the user should not be able to enter whole words in the Soft Keyboard.
try this to force open soft keyboard:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
then you can to use this code to close the keyboard:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(_pay_box_helper.getWindowToken(), 0);
You'll probably need to have an editable text area of some kind to take focus. You can probably have one invisible or on a transparent background with no cursor, though. You may need to toy around with the focusability settings for the view.
Use a TextWatcher to check for edits to that EditText with addTextChangedListener, or if you need an even-lower-level hook, set the textview's key listener with its setOnKeyListener() method. See the KeyListener documentation.
Use this call to force the soft keyboard open:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
.showSoftInput(myEditText, InputMethodManager.SHOW_FORCED);
and this one to close it:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
note that this is really not recommended - forcing the keyboard open is kind of messy. What's your use case that really necessitates your taking user input without a normal edit box and requires eating user input on a key-by-key basis without echoing it back?
To force the keyboard to open I used
this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
it worked for me.
Sometimes the other answers won't work.
Here is another way..
It will force the keyboard to show when the activity starts by listening to the window focus. onWindowFocusChanged() it will clear and request focus of the EditText, then set the soft input mode to visible and set the selection to the text in the box. This should always work if you are calling it from the activity.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
mEditText.clearFocus();
mEditText.requestFocus();
getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
mEditText.setSelection(mEditText.getText().toString().length());
}
}
You may also need
mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT);
}
}
});
Edit: I have also seen the keyboard not open inside nested fragments, beware of those kinds of situations.
if you want to control soft keyboard inside activity then use this code:
//create soft keyboard object
InputMethodManager imm = (InputMethodManager)this.getSystemService(INPUT_METHOD_SERVICE);
//1.USE
your_view.setFocusableInTouchMode(true); //Enable touch soft keyboard to this view
//or
your_view.setFocusable(true); //Enable keyboard to this view
imm.showInputMethod(your_view, InputMethodManager.SHOW_IMPLICIT);
//2.USE show keyboard if is hidden or hide if it is shown
imm.toggleSoftInputFromWindow(your_view.getWindowToken(),InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
//or
imm.toggleSoftInputFromWindow(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
//3.USE (you cannot control imm)
this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
//4.USE (with Dialog)
Dialog d = new Dialog(this, android.R.style.Theme_Panel);
d.getWindow().setTitle(null);
d.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
d.setOnKeyListener(keyListener);
d.setCanceledOnTouchOutside(true);
d.setCancelable(true);
d.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
d.show();
//to hide keyboard call:
d.dismiss();
//if you want get soft keyboard visibility call:
d.isShowing();
Sadly, as much as I'd have liked to up-vote one of the replies, none worked for me. It seems the solution is to wait for the layout phase to complete. In the code below, notice how I check if the showKeyboard method returns TRUE, and that's when I remove the global layout listener. Without doing this, it was hit and miss. Now it seems to work perfectly.
You need to do the below ideally in onResume()
#Override
public void onResume()
{
super.onResume();
ViewTreeObserver vto = txtTaskTitle.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
if (txtTaskTitle.requestFocus())
{
if (showKeyboard(getContext(), txtTaskTitle))
{
txtTaskTitle.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
}
});
}
public static boolean showKeyboard(Context context, EditText target)
{
if (context == null || target == null)
{
return false;
}
InputMethodManager imm = getInputMethodManager(context);
((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(target, InputMethodManager.SHOW_FORCED);
boolean didShowKeyboard = imm.showSoftInput(target, InputMethodManager.SHOW_FORCED);
if (!didShowKeyboard)
{
didShowKeyboard = ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(target, InputMethodManager.SHOW_FORCED);
}
return didShowKeyboard;
}
Working Great.........
edt_searchfilter_searchtext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
edt_searchfilter_searchtext.post(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getFragmentActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edt_searchfilter_searchtext, InputMethodManager.SHOW_IMPLICIT);
}
});
}
}
});
Below line call when you want to open keyboard
edt_searchfilter_searchtext.requestFocus();
if(search.getText().toString().trim().isEmpty()) {
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(search.getWindowToken(), 0);
}
I have tested and this is working:
...
//to show soft keyboard
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
//to hide it, call the method again
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
Simply, using adding 2 lines will work like a charm:
If using XML
android:focusable="true"
android:focusableInTouchMode="true"
Else in Java:
view.setFocusableInTouchMode(true);
view.requestFocus();
You can use this KeyboardHelper.java class
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
/**
* Created by khanhamza on 06-Mar-17.
*/
public class KeyboardHelper {
public static void hideSoftKeyboard(Context context, View view) {
if (context == null || view == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
public static void hideSoftKeyboardForced(Context context, View view) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromInputMethod(view.getWindowToken(), 0);
}
public static void hideSoftKeyboard(Context context, EditText editText) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
}
public static void showSoftKeyboard(Context context, EditText editText) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
editText.requestFocus();
}
public static void showSoftKeyboardForcefully(Context context, EditText editText) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
editText.requestFocus();
}
}

Categories

Resources