Keyboard hides after onActivityResult - android

I am developing chat application. WhenI click Take picture button my keyboard appears and the new intent starts. When I take the picture and return to chat application, the keyboard is hidden. I want the keyboard to remain appeared.
Anyone else had this problem?
Thanks!

To show soft keyboard do this:
//Import this
import android.view.inputmethod.InputMethodManager;
//Create object
private InputMethodManager imm;
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);

In my case, I need to open the keyboard within onActivityResult() after getting a picture from Gallery or taking a picture via camera.
To make it works, I have to put a delay like 500 ms. Otherwise, it will not open the keyboard.
Here is my code inside onActivityResult():
// Set original file name to description.
EditText etDescription = getView().findViewById(R.id.etAttachmentDescription);
etDescription.setText(fileName);
etDescription.selectAll();
etDescription.requestFocus();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// Open keyboard after 500ms
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}, 500);

please use this method to hide your soft keyboard .call this method from onactivity result.
public static void hideSoftKeyboard(Activity context) {
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputManager != null)
inputManager.hideSoftInputFromWindow(context.getWindow().getDecorView().getApplicationWindowToken(), 0);
context.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

#Badrul's solution worked for me but I had to requestFocus() on the edit text view first, but it worked great.
mEditText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);

Related

Keyboard auto on in Android

I want to implement a tablet in Android app that keyboard auto on the screen. Now my situation is I add a edit-text in a xml file, and when clicking the edit-text, keyboard appears, I want to show keyboard auto on.
When users click on the check mark, the keyboard disappears, but I want that keyboard will not be dismissed and clicking on check mark means checking the input.
Any samples or help is appreciated!
Use below method with activity
//To show the keyboard
public void showKeyboard(Activity activity) {
if(activity.getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0);
}
}
//To hide the keyboard
public void hideKeyboard(Activity activity) {
if(activity.getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
}
OR
Use below method with EditText
//To show the keyboard
public void showSoftKeyboard(EditText editText) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
//To hide the keyboard
`public void hideSoftKeyboard(EditText editText) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
Use above method as per your requirement
This will work and also always try to use etText.requestfocus(); it will automatically open a keyboard
and
public static void ShowKeyboard(Activity activity, View v)
{
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
view.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
}
Check with this:
Add this on your checkbox click
To Show Soft Keyboard:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(EDITABLE_VIEW,InputMethodManager.SHOW_IMPLICIT);
OR
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
To Hide Soft Keyboard:
InputMethodManager imm = InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(FOCUSABLE_VIEW.getWindowToken(), 0);
Here, “FOCUSABLE_VIEW” can be any view which is visible on screen like
Refer:http://chintanrathod.com/show-hide-soft-keyboard-programmatically-in-android/
Below are the methods to show and hide keyboard. Have a look.
public static void ShowKeyboard(Activity activity, View view) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
view.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
}
public static void HideKeyBoard(Activity activity) {
try {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (inputMethodManager.isAcceptingText())
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
e.printStackTrace();
}
}
If you want to open keyboard on activity start, you can add following code in <activity> tag in manifest:
android:windowSoftInputMode="stateVisible"
this will show the keyboard whenever you start activity.
You can use the following code,
editext.requestfocus();
this will automatically brings the keyboard.
Do not use,
android:windowSoftInputMode="stateHidden"
inside your android manifest file...
just remove "android:windowSoftInputMode" from your Activity.

On Button Click Hide Keyboard

I have a meme creator application, I have two text fields and a button, I want when the button is pressed to hide the keyboard, is this possible?
public void dismissKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (null != activity.getCurrentFocus())
imm.hideSoftInputFromWindow(activity.getCurrentFocus()
.getApplicationWindowToken(), 0);
}
Activity have to be passed to this method, Keyboard will get dismissed.
You can hide sof keyboard with this lines
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
Put this in the onClick(View view) event.
You need to import android.view.inputmethod.InputMethodManager;
The keyboard will hides when you click the button.
What you want should already be happening. When you click the button, the focus changes from the text field to the button, so the keyboard will automatically hide.
EditText editText = (EditText)findViewById(R.id.textBox);
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
this should be in button click event
Android Kotlin
On button click hide the keyboard in kotlin
fun dismissKeyboard(activity: Activity) {
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
if (null != activity.currentFocus) imm.hideSoftInputFromWindow(
activity.currentFocus!!.applicationWindowToken, 0
)
}
And use in your class like this
button.setOnClickListener {
dismissKeyboard(this)
}

How to open a keyboard in Android with EditText?

I have-
EditText &
Button
How can I open a soft keyboard when one selects the edittext view, the user can write anything and it should display in the edittext.
I used the below code but the keyboard doesn't come up-
EditText edit= (EditText) findViewById(R.id.list);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edit, InputMethodManager.SHOW_IMPLICIT);
Any idea how I can open the keyboard and close it again by using back button?
This is my function to open the keyboard :
protected void showKeyBoard(final View v){
v.post(new Runnable() {
public void run() {
v.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
}
});
}
Make sure
android:windowSoftInputMode="stateAlwaysVisible" in manifest File.
and
edittext.requestFocus();

android softkeyboard showSoftInput vs toggleSoftInput

showSoftInput() doesn't show the keyboard for me, but toggleSoftInput() does. I saw some other post that said to disable the hard keyboard when using the emulator, but I'm not using an emulator. I'm loading my APK on an actual device with no hard keyboard. Shouldn't both methods work? Why doesn't showSoftInput() work? I would like to explicitly associate the keyboard with a specific text field.
Doesn't work:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
editText.setText("textchange"); //i see the text field update
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
Works:
InputMethodManager imm = (InputMethodManager) getDelegate().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
It seems that the keyboard is initially displayed but hidden by something else, because the following works (but is actually a dirty work-around):
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
editText.postDelayed(new Runnable()
{
#Override
public void run()
{
editText.requestFocus();
imm.showSoftInput(editText, 0);
}
}, 100);
And when looking at logcat I suspect the cause behind this message hides the keyboard initially shown:
Hide Clipboard dialog at Starting input: finished by someone else... !
Show Keyboard + focus and also if you want to Hide the keyboard
#Override
public void onResume () {
super.onResume();
inputSearch.setFocusableInTouchMode(true);
inputSearch.requestFocus();
// Show Keyboard
InputMethodManager imm = (InputMethodManager) getSherlockActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(inputSearch, InputMethodManager.SHOW_IMPLICIT);
}
P.S inputSearch = (EditText) getSherlockActivity().findViewById(R.id.inputSearch);
// Hide Keyboard
InputMethodManager imm = (InputMethodManager) getSherlockActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(inputSearch.getWindowToken(), 0);
ShowSoftInput works if the imm's target view is same with the editText.
You can check this by imm.isActive(editText) or editText.isInputMethodTarget().
ToggleSoftInput is just toggling the keyboard of the current target of imm.
Target view of imm is set after the focus has been changed by editText.requestFocus(). I think some post processes exist between these two tasks since one post runnable was not enough. I tested double post and it worked for me.
editText.requestFocus();
editText.post(new Runnable() {
#Override
public void run() {
editText.post(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(editText, 0);
}
}
});
}
});
The precise answer to this question why does showSoftInput doesnt work and toggleSoftInput does?
Is that the View to which you are trying to display the Keyboard doesn't have the focus. So to solve this problem and to use the method showSoftInput you will have to call the following to methods on your view:
setFocusable(true);
setFocusableInTouchMode(true);
Calling the above methods will make sure that when you click on the View retains and captures the focus.
showSoftInput() does not work when device has a hard (slide-out) keyboard
Android show softkeyboard with showSoftInput is not working?
Try this:
public void showTheKeyboard(Context context, EditText editText){
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
If this doesn't work read the tutorial from here
public void hideKeyboard() {
myTextView.setFocusable(true);
myTextView.setFocusableInTouchMode(true);
imm.hideSoftInputFromWindow(myTextView.getWindowToken(), 0);
}
WORKS
public void hideKeyboard() {
imm.hideSoftInputFromWindow(myTextView.getWindowToken(), 0);
}
DOES NOT WORK
imm is dealt with earlier as I am using a Fragment so:
Declare imm in the Fragment
private InputMethodManager imm;
Then in the fragment add:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
imm = (InputMethodManager)
getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
}
He says after 3 to 4 hours of research and failures !!
Thanks user_CC ! :-)
Phil
I know this post is pretty old, but for those who came for answers from this date and none of the above worked. The code below worked for me on an Activity when an Alert Dialog popped up.
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
to show keyboard:
keyboard.toggleSoftInput(editText.getPaintFlags(), 0);
to hide keyboard:
keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0);

Controlling the Soft Keyboard during onClick - Android

I had an issue where if user was typing and hit submit, the keyboard would not go away, so I found this code to fix that issue (by placing this in the onClick method):
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
However, if the user manually closed the keyboard, then clicked submit, I found the code above would bring the keyboard back -- not good.
The question:
Is there better code to use? OR can I just say something like =
if (keyboard = displayed) {
// do code above
} else {
// do nothing
}
To hide the keyboard:
final InputMethodManager inputMethodManager =
(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
To show it:
final InputMethodManager inputMethodManager =
(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_FORCED);

Categories

Resources