Aerial
Aerial

Reputation: 1185

How to hide keyboard automatically after sending e-mail using emailIntent

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.

enter image description here enter image description here enter image description here enter image description here

As you can see, the keyboard doesn't hide after sending mail.

Upvotes: 4

Views: 3886

Answers (7)

Ganesh
Ganesh

Reputation: 179

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>

Upvotes: 3

Vishal Dasadiya
Vishal Dasadiya

Reputation: 2585

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);
    }

Upvotes: 7

user_person
user_person

Reputation: 125

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."

Upvotes: 0

Atul Bhardwaj
Atul Bhardwaj

Reputation: 6717

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);
}

Upvotes: 0

Danylo Volokh
Danylo Volokh

Reputation: 4285

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.

Upvotes: -1

Buzz
Buzz

Reputation: 51

It is easy just add the following code in your manifest for the desire activity:

android:windowSoftInputMode="stateAlwaysHidden"
android:configChanges="keyboardHidden"

Upvotes: 5

coder
coder

Reputation: 10530

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.

Upvotes: 3

Related Questions