Hiding the keyboard after calling a new operation that shows ProgressDialog

I have problems with the on-screen keyboard. I have an action with EditText that shows a keyboard and a button to go to the second lesson. The second action shows the ProgressDialog on its onCreate() , does things, and rejects the ProgressDialog . The problem is that when the ProgressDialog displayed, the same thing happens with the keyboard.

I would like the keyboard to disappear before creating the ProgressDialog . I have carefully searched for Kara and other sites, but nothing seems to work with this particular scenario.

I am attaching two photos for reference:

This is the code for the first action:

 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 second action code:

 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

+6
source share
5 answers

It was decided to use the technique variation sent 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 , as it does not rely on a focused view to get a window marker.

+20
source

Record this code in the manifest.xml file for the "SecondActivity" activity.

 <activity name="EditContactActivity" android:windowSoftInputMode="stateAlwaysHidden"> ... </activity> 
+10
source

You can also use this:

InputMethodManager imm;

Write the line below in the onCreate () method:

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

And this line is in the onclick of the 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); } }); } } 
+1
source

If in a fragment class

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

If the activity class

  @Override public void onResume() { this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); super.onResume(); FetchLocation fl = new FetchLocation(mContext, locationResult); } 
0
source

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 that I throw at the points that I want to hide.

-1
source

Source: https://habr.com/ru/post/914794/


All Articles