OnEditorAction not being called inside a DialogFragment - android

I wanted to add a neat functionality to my Dialog. When the user presses DONE button on the keyboard after editing one of EditTexts inside the dialog, it would simulate onPositiveClick of the dialog so that the data inside the EditTexts gets handled properly and the dialog is dismissed. This is much better than first entering the data and then pressing the dialogs OK button. My dialog extends DialogFragment and implements OnEditorActionListener and this is my listener method:
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((actionId & EditorInfo.IME_MASK_ACTION) == EditorInfo.IME_ACTION_DONE)//check if done was pressed
dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
return false;
}
But the problem is that this method is never called when i press DONE on my keyboard. Do you know what causes that?

Related

setCancelable(false) and onBackPressed conflict

I have a problem
I have a non cancelable custom dialog
which mean this custom dialog can only close if presses buton inside the custom dialog, so it won't cancel on backpress or click outside
I tried setCancelable(false) and it works however in my activity I have a onBackPressed and whenever my non cancelable dialog show onBackPressed wont trigger when I click back button because I think they conflict
is their a solution to do this?
EDIT: The purpose is I want the user to click button ok, or skip inside the custom dialog which means this dialog is required before proceeding to next activity
also in onBackPressed since I am using fragment whenever user press back it changes to previous fragment
sorry for lacking of explanation
my code is this
Dialog
dialog_welcome_navigation = DialogUtils.showCustomDialog(context, R.layout.dialog_welcome_navigation);
dialog_welcome_navigation.setCancelable(false); // disable closing dialog with back pressed
dialog_welcome_navigation.setCanceledOnTouchOutside(true);
and the onBackPressed
#Override
public void onBackPressed(){
Log.d("TAG", "--back--");
}
After searching I have found a solution thanks to this SO answer
https://stackoverflow.com/a/25251122/3481654
I add a setOnKeyListener on my dialog
dialog_welcome_navigation.setOnKeyListener(dialogWelcomeNavigationOnKey);
private DialogInterface.OnKeyListener dialogWelcomeNavigationOnKey = new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == android.view.KeyEvent.KEYCODE_BACK) {
dialog_welcome_navigation.dismiss();
// move other fragment
return true;
}
return false;
}
};

input method next not focusing on an edit text

I have 5 edit texts where user enters his options, I keep first two visible and other 3 visibility gone. If user wants to enter option 3 I make it visible by pressing action next key on keyboard while user finishes typing on option 2. But the problem is it doesnt focus on edit text 3. Now to experiment visibility of edittext 3 from gone to invisible, action next method works well. Is there a way to make input next method work when view's visibility turns gone to visible?
I am using this code to make focus appear on edittext 3 which is not working in case of edittext visibility turning visible from gone. Same code works well in case of edittext visibility turning visible from invisible.
option2.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_NEXT){
option3.setVisibility(View.VISIBLE);
option3.requestFocus();
}
return false;
}
});
How to make focus on button3 :
button2.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
button3.setVisibility(View.VISIBLE);
button3.requestFocus();
}
return false;
}
});

Fragment executes a method from the previous activity when edittext is focused

I have an activity A (navigation drawer) that launches some fragments
The activity A contains this:
#Override
public void onBackPressed() {
//create dialog to close the app
}
The problem is that it doesn't matter the fragment where I am, when I touch the back button and I have an EditText focused in the current layout, it will launch the dialog.
This means that if I want to close the app, I have to be in the main window, and touch back button and it asks me if I want to leave.
But if I am in any fragment and touch the back button wile an EditText is focused, it also launches the dialog, overriding the behavior of the back button that I have set in that current fragment.
When I touch back button on a fragment this is what it excecutes if the EditText is not focused, which work perfectly:
view.setFocusableInTouchMode(true);
view.requestFocus();
view.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//stuff
}
}
});
If an EditText is focused, it will execute the code for the back button in the Activity.
Anyone?
I am sure that it is related to context, focus or something like that, but still can't figure out why

Simulating a click on a Next/Done button in Robolectric

In a given screen i have few controls where the first one is EditText and than RadioGroup and than a Button, at EditText i have used android:imeOptions="actionNext", now for this writing a straightforward Robolectric test case is not a big deal as
we can write
Button someButton = (Button) findViewById(R.id.some_button);
someButton.performClick();
but the question is how to automate and write a test case on clicking Next/Done button of the given softinput KeyBoard. How we can write and perform next/done Button of softinput keyboard click ?
Please guide!!
You've got probably something like this which you want to test:
getView(R.id.etPassword).setOnEditorActionListener(
new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
checkCredentialsAndStartLoginTask();
}
return false;
}
});
This listener is called when you click on "Next" on the soft keyboard. If you want to test this simply call the onEditorAction with the adequate actionId as parameter on the EditText to simulate this soft keyboard button click. See this sample.
#Test
public void emptyUsernameShouldTriggerToast() throws Exception {
activity = controller.create().start().resume().visible().get();
// enter username and password
ui.etAccount.setText("");
ui.etPassword.setText("Battery");
// test initial state
ui.etPassword.onEditorAction(EditorInfo.IME_ACTION_NEXT);
// test Toast
ShadowHandler.idleMainLooper();
assertThat( ShadowToast.getTextOfLatestToast() ).isEqualTo(("Please enter username and password.") );
}

OnKeyListener for dialog is not get called

My question is, OnKeyListener for dialog is not getting called when touch setting button of keyboard, but it's working well when touch on tab button, backward button.
So if you have any solution, it will be appriciable. thanks
dialog.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
// TODO Auto-generated method stub
Log.d("", "on key press");
return false;
}
});
When you press a button on the Keyboard that will affect your application, it will fire OnKeyListener event.
The setting button on the keyboards are only related to that keyboard and will not fire any event. This is impossible.

Categories

Resources