Based on the following code snippet, I was wondering how to hide the soft keys (status and navigation bars) and maintain immersive mode throughout the whole app session even when an AlertDialog is displayed:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
AlertDialog.Builder dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog = new AlertDialog.Builder(this);
findViewById(R.id.button).setOnClickListener(this);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:
dialog.setTitle("Title");
dialog.setMessage("Message");
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// Dismisses dialog.
}
}).create().show();
break;
}
}
}
... the soft keys remain hidden throughout the app until I press the button to display the dialog (takes the focus away) in which the soft keys show up and then later hides again after dismissing the dialog. Thanks a bunch.
Try this:
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:
dialog.setTitle("Title");
dialog.setMessage("Message");
AlertDialog alert = dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// Dismisses dialog.
}
}).create();
alert.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
alert.show();
break;
}
}
Or this:
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:
dialog.setTitle("Title");
dialog.setMessage("Message");
AlertDialog alert = dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// Dismisses dialog.
}
}).create();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alert.getWindow().getDecorView()
.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
alert.show();
break;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With