AlertDialog shows SystemUiVisibility - java

I am making an android game and i set my SystemUiVisibility with the setSystemUiVisibility to not show the top bar and other system ui's, but now when u die in game i want to show an AlertDialog with the score you have. When i show the AlertDialog it brings the SystemUiVisibility back and i dont want to have that happen. Does someone know how to fix that. Here is the code i am using right now for the AlertDialog:
dlgAlert.setMessage("You lost, you hit the right color " + Points + " times");
dlgAlert.setTitle("You died");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.create().show();

Make the Dialog non-focusable when we create it (so we don't trigger a user-interaction) and then make it focusable after it's displayed.
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
//Show the dialog!
dialog.show();
//Set the dialog to immersive
dialog.getWindow().getDecorView().setSystemUiVisibility(
context.getWindow().getDecorView().getSystemUiVisibility());
//Clear the not focusable flag from the window
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

Related

Dismiss Popup Window on Touch Outside

I'm trying to open up a pop-up window with 4 buttons on it that will dismiss when a button is pressed or when the user clicks outside of the pop-up window. I would just make an alert dialogue, but that will only support 3 buttons.
There have been a lot of questions about this same thing, and I can't find any consistent answer or any answer that works for me (including the deprecated Bitmap Drawable). I've put all of the suggestions I've seen into my code, but to no avail.
Here's everything I've used so far:
//to create new popup window
LayoutInflater chooseMealInflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View chooseMealLayout = chooseMealInflater.inflate(R.layout.choose_meal_dialog, null);
PopupWindow chooseMealPopup = new PopupWindow(chooseMealLayout, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
//to make popup dismiss on touch outside
chooseMealPopup.setOutsideTouchable(true);
chooseMealPopup.setFocusable(true);
chooseMealPopup.setContentView(chooseMealLayout);
chooseMealPopup.showAtLocation (chooseMealLayout, Gravity.CENTER,0,0);
chooseMealPopup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
I've tried to find everything I can, like keeping setFocusable before showAtLocation, but when I run the app, nothing happens when I click. Figured it might be something individual to my code, since I'm new and don't really know what I'm doing.
don't know what you really want to do...
if you want show dialog when click button
YourBtn.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
//what you want to do like show dialog
}
});

Check the dialog is visible - Espresso

I found on Stackoverflow something like that about checking when dialog is visible:
onView(withText("Yes"))
.inRoot(isDialog())
.check(matches(isDisplayed()))
.perform(click());
Of course this works if Dialog with button 'yes' is visible, but in different scenario, if dialog will be invisible I got crash:
android.support.test.espresso.NoMatchingViewException: No views in
hierarchy found matching: with text: is "Yes"
So how to write that if the dialog exists, click yes, and if it does not exist, then nothing will be clicked?
You could try this:
onView(withText("Yes")).inRoot(isDialog()).withFailureHandler(new FailureHandler() {
#Override
public void handle(Throwable error, Matcher<View> viewMatcher){
}
}).check(matches(isDisplayed())).perform(customClick());
//if dialog is visible, perform click, otherwise do nothing.

How to hide keypad in android after pressing home button

I am showing a list with messages and each message's row has a comment button.When i click on comment button opens a comment box with edit text and button for submitting the comment.When comment box appears on screen keypad also appears for entering text.If i pressed home button before entering text then application goes background but keypad remains on screen.This is the thing irritating me.For custom list i am using a custom adapter and code for comment box is written in that adapter.I tried using
inputmgr.hideSoftInputFromWindow(txtComments.getWindowToken(), 0);
but it is not working. So how i can hide this keypad programmatically.
Try using the code in https://stackoverflow.com/a/1109108/1904479. Hope you are not testing it in Android version 4.1.
please use this method to hide soft keyboard.
public static void hideSoftKeyboard(Activity context) {
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputManager != null)
inputManager.hideSoftInputFromWindow(context.getWindow().getDecorView().getApplicationWindowToken(), 0);
context.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
KeyEvent.KEYCODE_HOME can NOT be intercepted. You can hide the keypad inputmgr.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0); in onStop() method of your activity.
It does not require token from focused view editText.

Soft KeyBoard does not show up in Landscape orientation

In my app, I have a dialog with EditText. I have coded it in such a way that as soon as the dialog shows up,the soft keyboard shows up, the edit text will get focus , all the existing text in the editText is preselected. And user can start typing. This all works fine when I am holding my phone in portrait orientation. But the same thing does not work when I have phone in landscape orientation. Part of my code in my fragment is pasted below.
final EditText inputDeviceId = new EditText(getActivity());
inputDeviceId.setInputType(InputType.TYPE_CLASS_TEXT);
String savedDevId = getDeviceID();
inputDeviceId.setText(savedDevId);
inputDeviceId.setSelectAllOnFocus(true);
// some more code ... to create dialog , ok cancel buttons then,
final AlertDialog alertDialog = alertDialogBuilder.create();
inputDeviceId.setOnFocusChangeListener(new View.OnFocusChangeListener() {
if(hasFocus){
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
});
alertDialog.show();
Now this all works in portrait orientation and does not work in landscape.
In landscape mode the dialog with edittext shows up , it has focus and existing text is preselected but it does not show the keyboard. User has to click in the editText for keyboard to come up. Why? I am using nexus-5 to test.

Make the background behind a AlertDialog black or opaque without replacing with Dialog

Is it possible to make the background behind an AlertDialog opaque or black without using a Dialog / CustomDialog, I currently have this,
<item name="android:backgroundDimEnabled">true</item>
in my style which I assign to the constructor of the AlertDialog. but it is not dark enough and users can still see information which I do not want them to yet. To be honest my reason for not wanting to use a Dialog is that I have already got everything working with the AlertDialog and thought it would be a good idea to check if anyone knew a quick way to do this.
private void dimActivity(Dialog dialog, float dimAmount) {
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.dimAmount = dimAmount;
dialog.getWindow().setAttributes(lp);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
dimAmount: 0 - no dimming, 1.0f full dimming. You can check this: How to dim a screen on click of a button in android?
use this code this might help you
final Dialog dialog = new Dialog(context);
Window window = dialog.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
WindowManager.LayoutParams.FLAG_DIM_BEHIND);

Categories

Resources