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.
Related
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
}
});
I was having some problem when trying to check and uncheck radio button upon click.
What I am trying to do is, when the activity on load, the radio button is set to checked by default. When user pressed on the checked radio button, I set the radio button to uncheck and empty the textview. Then, when the user pressed on the radio button again, I set it to checked and set some text in textview. Here is my code:
#Click(R.id.radioButtonEmail)
void radioButtonEmailClicked(View v) {
if(radioButtonEmail.isChecked() == true){
radioButtonEmail.setChecked(false);
editTextEmail.setText("");
}else {
radioButtonEmail.setChecked(true);
editTextEmail.setText("TEST");
}
}
However, this only worked for the first time when I tried to uncheck the radio button. After I set it to false, when I try to check it again, it does not work. Any ideas? Thanks!
I don't have an exact explanation for the behavior you are seeing, but your logic looks off to me. I think you intend to do this:
#Click(R.id.radioButtonEmail)
void radioButtonEmailClicked(View v) {
if (radioButtonEmail.isChecked()) {
editTextEmail.setText("");
}
else {
editTextEmail.setText("TEST");
}
}
In other words, you don't manage whether the radio button is clicked from your event handler. Rather, you check for that state inside the handler, and then respond appropriately.
I have a AppCompatActivity activity named MainActivity with the following code placed on onCreate method to show/hide back and menu button
getSupportFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
toggle.setDrawerIndicatorEnabled(
getSupportFragmentManager().getBackStackEntryCount() == 0);
getSupportActionBar().setDisplayHomeAsUpEnabled(
getSupportFragmentManager().getBackStackEntryCount() > 0);
}
});
This is my only activity, I use fragments for the different views. The back button shows perfectly when appropiated but does nothing when I click on it.
Do I have to put some code on fragments? I have checked many other similar questions but I'm not able to detect what's missed
EDIT
Many solutions ask to override onOptionsItemSelected on Fragment or Activity but this method is not called when I click on the back button on toolbar.
EDIT 2
If I comment line
toggle.setDrawerIndicatorEnabled(getSupportFragmentManager().getBackStackEntryCount() == 0);
then back button click opens navigation menu.
You'll have to manually handle the home button as shown here :
catch toolbar home button click event
then load the previous fragment from backstack:
Get Fragment from backstack
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.
I tried to make a dialog on startup of my application like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Hi Stackoverflow!").create().show();
}
But I want to make it so that if I click a checkbox in the dialog "Don't show again" then the second time I start the application the dialog doesn't appear. How can I do it?
If you just want a Dialog with a Button then you don't really need an AlertDialog. You can simply create a Dialog, create a layout for the Dialog in xml with the CheckBox and Button, then use setContentView() on the Dialog.
To not show the Dialog again simply create a boolean variable and use onCheckedChanged() to set that variable to true if the checkbox is checked. Save that in SharedPreferences and check that value on start up.
Good example of getting started with SharedPreferences
SharedPreferences complete docs
You can use SharedPreferences for this. Add a flag that tells you if the dialog has been shown before (or if the app has been launched before) and determine from there whether to show the dialog or not.