how to bring back soft keyboard after hiding using InputMethodManager - android

I'm new to Android Programming so was just making a simple app.
In the app I have an EditText component which slides up as the keyboard pops up and i don't want it to slide up so i searched for it and got an work around of using,
In OnCreate method
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
But the issue was after using this line the EditText was in place after clicking on enter the keyboard didn't went away.So, I search for it and got this method for hiding the keyboard
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
activity.getCurrentFocus().getWindowToken(), 0);
But now the issue is the keyboard is hiding successfully but is not visible after i re click on EditText.So, I was searching on the net but no luck and started started searching methods in Android Studio for making the keyboard visible and figured out some what this
public static void showSoftKeyboard(Activity activity){
InputMethodManager inputMethodManager1 =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager1.showSoftInputFromInputMethod(
activity.getCurrentFocus().getWindowToken(), 0);
But it's not working is throwing NullPointerException.
So please can anyone help me on this.
And also is it there any alternative for keeping the EditText on its position without sliding up. So that there is no need of applying this hide and show method and if not can you please tell me how to bring back the keyboard.

Comment your logic, and please try with below approach,
Write below line in your activity tag in Android Manifest like below
<activity android:name=".yourActivityName"
android:windowSoftInputMode="adjustPan">
</activity>
Then add this line to your edittext xml in your layout
android:imeOptions="actionDone"
like below
<EditText
android:id="#+id/edt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edittext"
android:singleLine="true"
android:maxLines="1"
android:imeOptions="actionDone"/>
Or set it from your code
yourEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
On click on done button in soft keyboard, it will be automatically close.
And below is the code of click event of Soft Keyboard done button.
yourEditText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
// write your code here
}
return false;
}
});

Try this
public void showSoftKeyboard(Context context, View view){
if(view.requestFocus()){
InputMethodManager imm =(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
or
public void showSoftKeyboard(Context context){
if(context == null) return;
InputMethodManager imm =(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}

Related

Suppress Soft Input (hide keyboard) for Edittext view

I'm trying to make a simple calculator and to do so, I want to, and exit view that the user can move the courser within but can only input based off of the buttons I've included.
When I press on the Edittext view, however, the keyboard pops up and I can't figure out how to suppress it - I've tried both android:windowSoftInputMode="stateAlwaysHidden" and android:configChanges="keyboardHidden" in the manifest and also
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
//Hide keyboard
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
In Java but none of them work
thanks for the help but I've just found a solution
XML:
<EditText
android:id="#+id/InputLine"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_above="#id/Sixth_Up"
android:onClick="hideKeyboard">
</EditText>
Java:
public void hideKeyboard(View v) {
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editInput.getWindowToken(),0);
}
You can check that view is in focus and then hide the keyboard.
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager manager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

Hiding soft keyboard without having focus

I am using a FrameLayout to show an EditText and a ListView (with checkboxes) alternately. When showing an EditText, I would like the soft keyboard to be shown. And when showing the ListView, I would like the soft keyboard to be hidden. Now usually a focus is needed in order to hide the soft keyboard. When my ListView gets shown, then getCurrentFocus() returns null. Is there a way to hide the soft keyboard, without having a focus?
I am showing the soft keyboard like that:
public static void requestFocusAndMoveCursorToTheEndAndShowKeyboard(final EditText editTextParam, final Activity activityParam) {
if (editTextParam == null) {
return;
}
if (editTextParam.requestFocus()) {
editTextParam.setSelection(editTextParam.getText().length()); // move Cursor to the end of the EditText
InputMethodManager imm = (InputMethodManager) activityParam.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
And I am trying to hide the soft keyboard like that:
public static void hideSoftInputKeyboardFromWindow(Activity activityParam) {
if (activityParam == null) {
return;
}
View view = activityParam.getCurrentFocus();
if (view != null) {
InputMethodManager inputMethodManager = (InputMethodManager) activityParam.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
Make use of .clearFocus(); on edittext when you don't want a focus on it.
In your AndroidMenifest.xml add this:
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
Try this:
Write following method to youractivity or to your utility class
/**
* Hide soft keypad
*
*/
public static void hideKeyboard(Activity activity, View v) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
In your hideSoftInputKeyboardFromWindow method, try:
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
instead of
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
Edit: ok same answer as Dorami
How do you show them alternately? Using different fragments? Or do you simply inflate different layouts? Provide more details with your complete code
Thank you for your answers. Finally I got it solved using a View.OnFocusChangeListener for the EditText, like described here:
Hide soft keyboard on losing focus

Having Trouble with Soft Keyboard

Hi Everyone iam new to Android and stuck in really silly problem in my project i have one EditText which is defined in Header View of a List View whenever users touches the EditText soft keyboard will be displayed. i have something called clear button which clears the EditText After clearing soft keyboard is not displaying in 2.3 devices.Whenever i press lock/power button lock the device and unlock then soft keyboard is displaying
<activity
android:name=".pl.Mobile.AddJobNew"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape"
android:windowSoftInputMode="stateVisible|adjustResize|adjustPan"
>
Here is My Activity Declaration in Android for android:windowSoftInputMode : 'adjustResize' option working perfectly for Android 2.3 devices but failed in 4.0 devices where soft keyboard overlapping over edit text . option 'adjustPan' is workin good in 4.0 devices but failed in 2.3 devices
Please help me get out of this problem.
Thanks in Advance
Try this in the activity with the edit text:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){
// Do something for 4.0 and above versions
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}
else{
// do something for phones running an SDK before 4.0
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
Also remove,
android:windowSoftInputMode="stateVisible|adjustResize|adjustPan"
From your activity in the manifest file.
Build.Version
I think that the problem is that when you click the clear button, the editText looses its focus so the keyboard hides itself.
I would try 2 things to solve it;
after you clean the text, do:
yourEditTextField.requestFocus()
manually control the keyboard visibility:
public void showKeyboard(){
View view = getWindow().getCurrentFocus();
if (view==null)
return;
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
}
public void hideKeyboard(){
View view = getWindow().getCurrentFocus();
if (view==null)
return;
IBinder binder = view.getWindowToken();
if (binder == null)
return;
// prevent a bug in some keyboards that makes the text hang on the screen
if (view instanceof EditText)
((EditText)view).setText(((EditText)view).getText().toString());
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(binder, InputMethodManager.HIDE_NOT_ALWAYS);
}
I have below line in My AndroidManifest.xml file and it works great. Please try this code. Dont put any extra code. And still if you not get any success then let me know. I will like to help you out.
<activity android:name=".pl.Mobile.AddJobNew" android:screenOrientation="portrait" android:configChanges="keyboard|orientation" android:windowSoftInputMode="adjustPan"/>
Try with above code for your activity.
Try something like that
editext1.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if (v instanceof EditText)
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
v.requestFocusFromTouch();
return true;
}
return false;
}
});
If it not work for you try to add
editext1.setOnFocusChangeListener(new OnFocusChangeListener()
{
public void onFocusChange(View v, boolean hasFocus)
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
});
Also is good to remove request focus from layout. and maybe set this android:windowSoftInputMode="stateHidden|stateAlwaysHidden" in manifest on activity

KeyBoard Issue when using TabHost in android

Hi I am using four tabs in my tabhost, named TabOne, TabTwo, TabThree, TabFour.
In TabOne I have editext to make search option, when I pressed in edit text it show the keyboard.
But without closing the keyboard When I move to TabTwo the keyboard still showing. I dont want the keyboard should enable in TabTwo,TabThree,TabFour.
Because all other three tabs are not having edittext option.
Now My question is how to hide keyboard when clicked on the other tabs.
I have tried the below methods,
Adding the below code in oncreate method of TabB
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Adding the below xml tag in Android manifest file
android:windowSoftInputMode="stateAlwaysHidden"
Please guide me to solve this issue.
Hide edit text when it lose focus.
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard();
}
}
});
void hideKeyboard() {
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
Please look at this answer
enable the soft keyboard
inputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
and disable the soft keyboard
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
This worked for me:
In the Activity which is triggered when you click on the specific Tab, I used this in onCreate:
inputSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(inputSearch.getWindowToken(), 0);
}
}});
In the AndroidManifest.xml, I added the following to my MainActivity (Where all Tabs are defined):
android:windowSoftInputMode="stateAlwaysHidden"
this.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
This single line will work for you. check this out.

Best way to hide keyboard in Android

I would like to know the best way to hide keyboard after entering the text to EditText.
1) setonfocuschangelistener : Does this listener is fired only, when the done button is pressed or when the focus changes from one EditText to other? When I used this method, I couldn't hide the keyboard.
2) setOnTouchListener : When I used this, I could hide the keyboard, but i doubt there might be an issue with this. In this case, I add the touch listener to the root LinearLayout. Following code I had used:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
txtUserName = (EditText)findViewById(R.id.txtUserName);
btnLogin = (Button)findViewById(R.id.btnLogin);
layoutView = (LinearLayout)findViewById(R.id.li);
layoutView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(txtUserName
.getWindowToken(), 0);
return true;
}
});
}
Inside the main LinearLayout, I am using other two LinearLayouts. The issue that i faced with the above code is that at some points when I pressed, the keyboard doesn't hides.
My doubt is that I am adding touch listener only with root layout, not giving touch listener with other inner layouts or other controls(TextView). When I touch over other controls or some points around the TextView(ie, inner layouts), keyboard doesn't hides.
That means do i need to add touchListener to all layouts or controls inside the root layout?
How this situation can be handled in a better way?
You can use this code
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditView.getWindowToken(), 0);
My answer on this question:
Add this method:
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
If you want to hide keyboard when you touch screen, you can do by this way:
#Override
public boolean onTouchEvent(MotionEvent event) {
hideSoftKeyboard(LoginActivity.this);
return false;
}
Hope this will help you.
Try this :
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
it can be used to suppress the keyboard until the user actually touched the edittext view.
OR
inputManager.hideSoftInputFromWindow(editView.getWindowToken(), 0);
A simple check for null or a try-catch avoids the NullPointerException if no view is focused.
public void hideKeyboard(Activity activity) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (activity.getCurrentFocus() != null) {
inputManager.hideSoftInputFromWindow(activity.getCurrentFocus()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
Best way to hide keyboard, just dispatchDoneKey in your activity. Keyboard will be hidden if it is in visible state.
public void dispatchDoneKey() {
dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
}

Categories

Resources