There is a page where the user can send e-mail, sms or call its guests when needed. The problem is that when the user sends e-mail to its guest, the keyboard doesn't hide. Even-though I have a small problem solving the issue, It still seems hard to find alike post to solve it. I'll be also making screenshots and placing them in here.
As you can see, the keyboard doesn't hide after sending mail.
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_EMAIL,
new String[] { **EmailAddress** });
startActivityForResult(sendIntent, 1);
#Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
super.onActivityResult(arg0, arg1, arg2);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager inputManager = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(**AnyViewOfScreen**.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}, 300);
}
It is easy just add the following code in your manifest for the desire activity:
android:windowSoftInputMode="stateAlwaysHidden"
android:configChanges="keyboardHidden"
Its not the to override when the keyboard shows and hides itself, but here are the two methods I use to hide and show the keyboard as needed.
public void hideKeyboard(final View aView){
aView.post(new Runnable() {
#Override
public void run(){
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
public void showKeyboard(final View aView) {
aView.post(new Runnable() {
#Override
public void run() {
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(ListingScreen.this.getCurrentFocus().getWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
});
}
When you call hide/show Keyboard, pass in your current view. The post runnable thread will wait to run until the view has finished loading, then dismiss the keyboard.
Hope this helps someone :
#Override
protected void onResume() {
super.onResume();
Log.d("OnResume", "Called");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager inputManager = (InputMethodManager) LocationDetailActivity.this
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}, 300);
}
if you do not have any focusable view in your layout, just add a dummy linear layout to your xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true">
<requestFocus />
</LinearLayout>
Call This method at the place where you want to hide your keyboard if it is opened(e.g call this when you click to sendingEmail button)
protected void showVirturalKeyboard()
{
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
#Override
public void run()
{
InputMethodManager m = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if(m != null)
{
m.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
}, 100);
}
After trying every solution I found on StackOverflow nothing seemed to worked. In the end I did find a way to force close the keyboard, but it is not ideal.
You can set android:windowSoftInputMode="adjustPan" in the Android Manifest for that activity.
The unfortunate side effect of this is explained here http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft :
"The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window."
I had similar problem. Gmail hides the keyboard after sending. When you return to your application it focuses on something else. If you take slower device you'll see that gmail hides keyboard after sending message.
Related
I'm trying to show a Keyboard for a view as the focus is set to it once it renders. I tried multiple ways of trying to get it to work but whenever the Dialog renders and the focus is in the View, it doesn't show the keyboard until I click it.
This was one way I tried and it didn't work.
yieldEdit.RequestFocus();
imm = (InputMethodManager)(this.Activity).GetSystemService(AMSessionActivity.InputMethodService);
I then also tried to add an event to the view like so.
yieldEdit.FocusChange += (sender, e) =>
{
if (e.HasFocus)
{
imm.ToggleSoftInput(ShowFlags.Forced, 0);
}
};
This also doesn't seem to work. I'm not sure why it isn't working as it worked when it was an Activity.
Thanks
Use below code in EditText xml;
android:focusable="true"
android:focusableInTouchMode="true"
and use the following to your activity
AMSessionActivity amsActivity = (AMSessionActivity) Activity;
yieldEdit.RequestFocus();
InputMethodManager imm = (InputMethodManager)amsActivity.GetSystemService(Context.InputMethodService);
imm.ShowSoftInput(yourTextBox, ShowFlags.Implicit);
Try to use :-
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
editText.post(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
}
});
editText.requestFocus();
I have a custom view that could show a view in same space that should be soft keyboard native for android.
I need to having the keyboard opened, click in a button, hide the keyboard and shows other view in same place that keyboard be/was.
I have that implemented right now just with a hide keyboard and show custom view but has a weird behavior and min lag and overlapping.
Has someone implemented a similar stuff?
I have checked the Github project and found the bug and I have fixed that bug with the following code:
if (isRedPanelVisible()) {
showRedPanel(false);
showKeyboard(true, new KeyboardCallback() {
#Override
public void onKeyboardDone(boolean isVisible) {
}
});
}
if (KeyboardVisibilityEvent.isKeyboardVisible(TestActivity.this)) {
hideKeyboard(TestActivity.this);
new android.os.Handler().postDelayed(new Runnable() {
#Override
public void run() {
showRedPanel(true);
}
}, 100);
Note: You just have to put this in TestActivity.java under button's click event and Remove the previous code.
What I did
if your readPanel is visible then I called the showRedPanel to false and try to open the keyboard.
After that I have added a check for Keyboard's visibility event and if keyboard is visible I called hideKeyboard to make keyboard go away and call showReadPanel with true after a delay of 100 ms
Code: hideKeyboard
public void hideKeyboard(Activity activity) {
// Check if no view has focus:
try {
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
} catch (Exception e) {
}
}
So what happens in your code is that:
Tell system to close the keyboard -> Show red panel with a small delay -> Red panel is shown before keyboard closing -> Since keyboard mode is in adjustResize the red panel shown above keyboard -> Keyboard get closed -> Everything in place
Try to change windowSoftInputMode in manifest from adjustResize to adjustNothing.
Sadly keyboard in android doesn't work smoothly like in IOS, keyboard is handled by OS means you no control over it size, opening/closing animation and no callback! So the best way is to always show red panel and when needed Open keyboard on top of it.
se the following functions to show/hide the keyboard:
/**
* Hides the soft keyboard
*/
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
/**
* Shows the soft keyboard
*/
public void showSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
view.requestFocus();
inputMethodManager.showSoftInput(view, 0);
}
Every time I have focus on some edit field and Dialog shows up, it will pull up soft keyboard as soon as I dismiss(); Dialog. I have tried every way to remove it after click event, but whatever I do it still shows up.
public static void hideSoftInput(FragmentActivity _activity){
if(_activity.getCurrentFocus() != null){
InputMethodManager inputManager = (InputMethodManager) _activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(_activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
public static void hideSoftInput(View _v, Context _c){
if(_v.getWindowToken() != null){
InputMethodManager inputManager = (InputMethodManager) _c.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(_v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
I can't find it now but someone else on here suggested to wrap the call to hide the keyboard in a postDelayed call.
It worked for me when many other options failed. The only thing is that it may give a funny jiggle of the screen because you will be suppressing the keyboard while it is trying to show. Without the postDelayed it seems it will try to hide the keyboard before Android tries to show it after the dialog has closed. So ultimately we have to fight a timing issue in Android.
Something like this:
view.postDelayed(new Runnable()
{
#Override
public void run()
{
hideKeyboard();
}
}, 50);
I have code like the following to immediately show the soft keyboard when entering my app:
#Override
protected void onResume() {
super.onResume();
...
myEditText.requestFocus();
myEditText.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(myEditText, InputMethodManager.SHOW_IMPLICIT);
}
}, 100);
...
}
However, on the Android 2.1 emulator, the keyboard appears and then immediately disappears. If I make the delay longer, like 1000, it reliably appears. On an Android 4.0 emulator, a delay of 100 reliably shows the keyboard, but shorter delays do not.
Does anyone know who might be hiding the keyboard? Is there a reliable way to prevent it? If not, is there a delay I can use to guarantee that the keyboard will show?
If I understand you correctly, I think you can remove the following code in your onResume():
myEditText.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(myEditText, InputMethodManager.SHOW_IMPLICIT);
}
}, 100);
And simply use android:windowSoftInputMode="stateAlwaysVisible" for your Activity in the manifest.
I think what you are seeing is Android identifying the view that should get focus by default and giving it focus (which hides your keyboard). Setting your delay longer or shorter just makes it so your code runs before or after that focus is set. You could figure out what view is getting focus by default, and if you don't want it to ever have focus, set it as focusable false and focusableInTouchMode false. If it does need to have focus at some point, you could set an onFocusChanged listener, and when it gets focus the first time, post your runnable(without delay) to give the focus to the EditText and open the keyboard.
Thanks to #Daniel Smith and #Cookster.
This was happening because I did not set a windowSoftInputMode in my manifest, so it was using the default value (stateUnspecified), which hid the keyboard on startup. Apparently, that setting is applied after some delay on resume, and so my call to show the keyboard only worked if my delay was longer than the built-in delay to hide it.
To fix, I set windowSoftInputMode="stateUnchanged" and then I always either hide or show the keyboard in onResume. I also removed the delay, which was no longer necessary once the built-in hiding was not happening.
Never mind, that mitigated the problem (it lets me reduce the delay), but it didn't fix it completely. There is something very nondeterministic about this, and the keyboard is no longer appearing if I don't use the delay. However, if I reintroduce a delay of about 100ms, the keyboard seems to show up about 90% of the time, which puts me back where I started: why is this happening and what's a safe delay?
put this code in onRun() in onResume() method:
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
[Edit]
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
text.requestFocus();
text.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(text, InputMethodManager.SHOW_FORCED);
}
}, 100);
text.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(text.getWindowToken(), 0);
}
}, 200);
}
So i've got an EditText which is disabled initially. When i press a button it enables it, and automatically opens the soft keyboard.
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(etToDelete, InputMethodManager.SHOW_FORCED);
After i enter some text in it, i press the EditText which should make it disable again and hide the opened keyboard.
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(((EditText)view).getWindowToken(), 0);
BUT, what it does is very quickly close and then reopens it. MY GUESS is when you normally press an EditText it opens the keboard, so in this case even though i close it, it reopens it because of that :(
What is the solution? I've tried a couple of methods by which i stop the keyboard from showing when an EditText is pressed, but i was unsuccessfull, if someone could offer me a concrete example of how this should be made, i'd be grateful.
I have same problem I solved so:
first create a class KeyBoardManager:
import android.content.Context;
import android.os.Handler;
import android.view.inputmethod.InputMethodManager;
public class KeyBoardManager {
public KeyBoardManager(Context context) {
final Handler handler = new Handler();
final InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
new Thread(new Runnable() {
#Override
public void run() {
while(true){
try{Thread.sleep(100);}catch (Exception e) {}
handler.post(new Runnable() {
#Override
public void run() {
if(!imm.isAcceptingText()){
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
}
}
});
}
}
}).start();
}
}
and in method onCreate of first activity you create a new instance of KeyBoardManager like:
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new KeyBoardManager(this);
}
}
and when your edittext is draw in screen for the firs time you call:]
(new Handler()).postDelayed(new Runnable() {
editText.requestFocus();
editText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
editText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
}, 200);
Every method for hiding keyboard when starting fragment didnt work for me, but this made it, so try it out maybe
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);