How to hide keyboard in android - android

I wants to hide keyboard of device. I have try this code but its not working for me, Please Suggest me some other codes.
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
I have also try for manifest file but its also not working

InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

When this code in root view's onTouch, it should work. If you want to hide keyboard when activity opened, you can add android:windowSoftInputMode="stateHidden" to AndroidManifest.xml

EditText myEditText = (EditText) findViewById(R.id.myEditText);
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
use this code using inputmanager and hideSoftInputFromWindow

You can simply add LinearLayout(with visibility gone/invisible) or any layout,that has no child.
And redirect the focus to it when activity starts.
;)

Related

How to disable the soft keyboard

I am working on a project with an AutoCompleteTextView. I've put some buttons to work as a keyboard the problem is when i click(focus) on the autocompletetextview
the soft keyboard appears i dont want it to appear at all i have tried use this .
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
but this seems only to work if i put it in an onclick (button) and it is used to hide the keyboard while i need to disable it completely...any ideas?
public static void hideKeyboard(Activity activity) {
View view = activity.findViewById(android.R.id.content);
if (view != null) {
InputMethodManager imm =
(InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
You can call this method on button click Maybe this helps you.

Software Keyboard doesn't appear the first time the fragment is loaded

I've been trying to automatically show the keyboard with focus on a edit text when a fragment(that includes the edittext) is loaded.
I've tried using :
editTextPrice.requestFocus();
InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imgr.showSoftInput(getView(), InputMethodManager.SHOW_IMPLICIT);
But this doesn't work the first time I load the fragment. The focus is set and I can see the cursor but no keyboard appears. If I close the fragment and add it again it works.
If tried everything from post() to a postpone it with a handler, to onResume,ect.
Does anybody have an idea what might cause this to happen?
Thanks in advance.
Best regards
let me explain why keyboard is not opening when we load a fragment the view cannot get focused, so we need set focus to the view in a separate thread, Then after we need to open keyBoard
void showKeyboard(Context mContext, View view)
{
if (mContext != null && view != null && view.requestFocus())
{
InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
This above function is to open keyboard
editTextPrice.postDelayed(() -> {
editTextPrice.requestFocus();
showKeyboard(getContext(), editTextPrice);
}, 300);
android:focusable="true"
android:focusableInTouchMode="true"
And
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
you try using 2 function below:
fun hideKeyBoard(context: Context, view: View) {
val inputMethodManager = context.getSystemService(
Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}
fun showKeyBoard(context: Context, view: View) {
val inputMethodManager = context.getSystemService(
Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
Do not request focus from java code. Do it from the xml itself like
android:focusable="true"
also do the following in the manifest file
<activity android:name=".YourActivity"
android:windowSoftInputMode="stateAlwaysVisible" />

Soft keyboard keeps popping up

I've implemented an app with a NavigationDrawer and some Fragments.
But everytime I change Fragment with the NavigationDrawer, the soft keyboard keeps popping up, even if there's no EditText on the screen.
How can I solve this?
Have you tried adding this to your manifest for the activity:
android:windowSoftInputMode="stateHidden"
Add this line of code in your activity
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
And try with adding to fragment also.
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Try This in your BaseActivity or Main Activity
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
This helped me out. I hope it works.

how to disable softkeyboard in an activity?

I want to disable keyboard pop up in my activity. There will not be any keboard in the actvity. but hardware keyboard must work.how to do this? hardware buttons must work
Just try below code.
InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(editView.getWindowToken(), 0);
Use this in your manifest:
<activity android:name=".SampleActivity" android:windowSoftInputMode="stateAlwaysHidden"/>
Managing soft keyboard in android is not that easy, although you can hide keyboard programatically.
private void hideKeyboard() {
//This will get any view in focus.
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
note that Hardware Keyboard will work.

SoftInputMode cannot be set to hidden

I created an Activity with a Dialog theme and it has an EditText.
I don't want to show soft input on every activity so I used the following code on that activity:
EditText editTextData=GetView(R.id.txtData);
editTextPin.requestFocus();
InputMethodManager mgr = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
mgr.showSoftInput(editTextData, InputMethodManager.SHOW_IMPLICIT);
This makes the input keyboard visible.
After the input, I tried to make it hidden with this code:
InputMethodManager mgr = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromInputMethod(editTextData.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
It remains visible. What else should I call?
try ..
mgr.hideSoftInputFromInputMethod(editTextData.getWindowToken(),
1);
or
try
InputMethodManager mgr = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE); mgr.hideSoftInputFromWindow(editTextData.getWindowToken(), 0);
You can use below function to hide the keyboard..
private void hideSoftKeyboard(Activity act) {
InputMethodManager inputMethodManager = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(act.getCurrentFocus().getWindowToken(), 0);
}
Hope it will help you..

Categories

Resources