Hiding keyboard after calling new Activity that shows a ProgressDialog
Asked Answered
T

5

6

I'm having trouble with the on screen keyboard. I have an activity with an EditText which shows the keyboard, and a button to go to a second activity. The second activity shows a ProgressDialog on its onCreate(), does stuff, and dismisses the ProgressDialog. The problem is that while the ProgressDialog is displayed, so is the keyboard.

I would like the keyboard to disappear before creating the ProgressDialog. I searched thorougly both StackOverflow and other sites, but nothing seems to work with this particular scenario.

I'm attaching two pics for your reference:

This is the code of the first activity:

public class FirstActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });
    }
}

and this is the code of the second activity:

public class SecondActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        // TODO: hide keyboard here

        final ProgressDialog dialog = ProgressDialog.show(this, "", "Please wait...", true, false, null);

        // in real code, here there is an AsyncTask doing stuff...
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dialog.dismiss();
            }
        }, 5000);
    }
}

Thanks

Telethermometer answered 3/5, 2012 at 10:1 Comment(2)
Take a look at this: https://mcmap.net/q/972888/-hide-soft-keyboardJawbreaker
@Rajesh, already tried, not applicable here. The second activity must hide the keyboard, even if the keyboard was shown by another activity.Telethermometer
T
20

Solved using a variation of the technique posted by phalt:

InputMethodManager im = (InputMethodManager) this.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

This code works correctly during onCreate/onStart/onResume, since doesn't rely on a focused view to get the window token from.

Telethermometer answered 3/5, 2012 at 12:5 Comment(0)
S
10

Write this code in manifest.xml file for 'SecondActivity' Activity.

<activity name="EditContactActivity"
    android:windowSoftInputMode="stateAlwaysHidden">
    ...
</activity>
Saltigrade answered 3/5, 2012 at 10:6 Comment(3)
This is not a viable option for me, as the layout of the second activity may include an EditText (determined by the result of the AsyncTask).Telethermometer
I found this to be useful, so the keyboard would pop up when needed, but not automatically on loading the screen: android:windowSoftInputMode="stateHidden"Standford
needs stateAlwaysHidden|adjustNothing not just stateAlwaysHiddenExultant
T
1

you can use like this also:

InputMethodManager imm;

Write below line in onCreate() Method:

imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

And this line is in onclick of button:

imm.hideSoftInputFromWindow(arg0.getWindowToken(), 0);

Example:

public class FirstActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {

            imm.hideSoftInputFromWindow(arg0.getWindowToken(), 0);
            Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
            startActivity(intent);
        }
    });
}
}
Truesdale answered 3/5, 2012 at 10:9 Comment(1)
This normally works, but the second activity is in a library project, while the first activity is in another project using this library project. Thus, as I have no control on the first activity, the second activity must directly handle the keyboard hiding.Telethermometer
T
0

If In Fragment Class

@Override
    public void onResume()
    {
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        super.onResume();
        }

If In Activity Class

 @Override
    public void onResume()
    {
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        super.onResume();
        FetchLocation fl = new FetchLocation(mContext, locationResult);
    }
Temporize answered 24/3, 2015 at 10:55 Comment(0)
K
-1

Have you tried:

InputMethodManager im = (InputMethodManager)
this.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);

im.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),
InputManagerMethod.HIDE_NOT_ALWAYS);

This is the code I throw in at points I want to hide the keyboard.

Kindig answered 3/5, 2012 at 10:34 Comment(3)
Crashes the application with a NullPointerException since in onCreate (obviously after setContentView), onStart, and onResume there is no focused view yet.Telethermometer
Okay, have you instead tried to set the keyboard as hidden in the activity manifest and then let is be shown afterwords through code? Edit: for example: take Krishna Suthar's reply below and then reverse my code so that the keyboard can be used.Kindig
This will throw null pointer exception because it'll try to get current focus and it won't get one. Try running monkey script on your code. At one point, it might give you null pointer.Merriemerrielle

© 2022 - 2024 — McMap. All rights reserved.