I have a PreferenceActivity with an EditTextPreference. I can easily detect positive button press using OnPreferenceChangedListener. However, the listener is never called when the user presses the negative button.
Is there anyway to detect it?
Thanks.
You cannot detect it unless you override the onDialogClosed(...) in EditTextPreference (assuming that you use the old and now deprecated framework version of the preferences):
public class EditTextPreferenceNegative extends EditTextPreference {
#Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult); // this will notify the change listener for positive results
if (!positiveResult) {
// do something with your negative result
}
}
}
Note, however, that this will be called even if the dialog is closed without pressing the negative button. If you want to detect only the negative button, you can override the onClick(...) method which is called when either of the buttons is clicked.
Related
I have an activity and a dialog fragment. What I do is when dialog fragment is being shown and user clicks outside dialog fragment, to show an Alert dialog.
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return object: Dialog(activity!!, theme) {
override fun onTouchEvent(event: MotionEvent): Boolean {
if (MotionEvent.ACTION_OUTSIDE == event.action) {
presentAlertDialog()
return true
}
return super.onTouchEvent(event)
}
}.apply {
setCanceledOnTouchOutside(false)
setCancelable(false)
window?.apply {
setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL)
setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH)
}
}
If at the point where user clicked there is no view at the activity, then there is no problem. However, when there is a view (e.g., button ) at the point user clicked, both alert dialog and that view's onClickListener is called. What I want to do is just to show alert dialog and make activity's view to dismiss that touch. Is there a way to do it? Thanks in advance!
Edit: To make clearer, I add a screenshot:
What I want is when user touches outside the dialog fragment (i.e., "Waiting" ) and inside Sign Up button (or any other), I want to show an alert dialog and want Sign Up's onClickListener to dismiss that touch.
Okay i guess i understood a little you have two dialogs and when someone clicks outside of first dialog you want to open another. Here is how you can achieve this.
First set dialog.setCanceledOnTouchOutside(true); then implement cancel() listener
dialog.setOnCancelListener(
new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
//Open another dialog here
}
});
It's better to use this rather than handling onTouchEvent by yourselves. Hope this helps let me know if still has any issues. I might write some code if given time. Cheers!
I want to take control of soft keyboard appering. So I have override EditText class and method onCheckIsTextEditor.
#Override
public boolean onCheckIsTextEditor() {
return MenuActivity.expanded;
}
And after it everything works fine except delete/backspace button. When I press it nothing change. Also when I set onClickListener to my editText i can see that every click except delete fire this listener.
public void setOnBackSpaceListener(){
this.setOnKeyListener((view, i, keyEvent) -> {
Log.d("AAA -> ", String.valueOf(keyEvent.getKeyCode()));
return false;
});
}
So it's look like android think there was no click and I don't know why?
I'm going to make the assumption that you are implementing View.OnKeyListener in your Activity or Fragment. Are you passing your listener to the view you would like to capture the event from?
In this case, it will be:
mEditText.setOnKeyListener(this);
I have a seekbar in my activity. In the button event when I go to another activity and then use back button, the values of the seekbar are set where the user left it. I would like to reset the seekbar when the user comes back to the seekbar activity. How can I achieve it? Thanks
public void onClick(View v) {
if (seek.getProgress > 0) {
// do something
}
}
simply reset the seek bar in onPause or onResume of your SeekBarActivity, i.e. :
#Override
protected void onPause() {
seek.setProgress(0);
super.onPause();
}
This will be called when you have started another activity and the current one is paused.
How can make an EditText have a onClick event so that on single click an action is done.
private void addListenerOnButton() {
dateChanger = (EditText) findViewById(R.id.date_iWant);
dateChanger.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
this is not working as excepted....single click gives just the onscreen keypad but not the datepicker dialog which appears only if i double click
if we just add android:focusableInTouchMode="false" in edittext on layout page it should work in a singleclick on its onclicklistener. no need to handle onFocusChangeListener.
Change your code from an onClickListener to an OnFocusChangeListener.
private void addListenerOnButton() {
dateChanger = (EditText) findViewById(R.id.date_iWant);
dateChanger.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
showDialog(DATE_DIALOG_ID);
}
}
});
}
EditText is not meant for singleClick.
I mean you should not use Click Listener with it.
rather you can do like,
Use onFocusChangeListener which is also not 100% correct approach.
Best would be instead the EditText use one TextView write onClick of that and if needed give a background image to that TextView.
Rewrite
I have an EditText that launches a dialog when the user either clicks it once or navigates to it with a trackball / directional pad. I use this approach:
Use an OnFocusChangeListener for gaining focus to open the dialog.
Override dismissDialog() to clear the focus from the EditText when the user closes the dialog, preventing the user from entering text without the dialog (as far as I can tell)
.
I have also tried this (however I now remember this method did respond to trackball movement):
Use an OnClickListener for touch events.
Set setFocusable(false) to prevent user input.
Hope that helps.
In one of my activities I have three EditTexts and an OK button. The OnFocusChangeListener is set to all three EditTexts. The listener should trigger every time the focus is lost.
Switching between EditTexts works perfectly. But if the user presses the OK button there's no focus change (losing the focus) triggered for the EditText the user focused before pressing the button.
What's wrong with my code?
private class MyOnFocusChangeListener implements OnFocusChangeListener {
private EditText editText;
public MyOnFocusChangeListener(final EditText editText) {
super();
this.editText = editText;
}
#Override
public void onFocusChange(final View view, final boolean isFocused) {
if (!isFocused) {
if (editText == editText1) {
// Do a calculation
} else if (editText == editText2) {
// Do another calculation
} else if (editText == editText3) {
// Do a different calculation
}
}
}
}
#Override
public void onCreate(final Bundle bundle) {
// ...
editText1.setOnFocusChangeListener(new MyOnFocusChangeListener(editText1));
editText2.setOnFocusChangeListener(new MyOnFocusChangeListener(editText2));
editText3.setOnFocusChangeListener(new MyOnFocusChangeListener(editText3));
// ...
}
You could try to clear the focus when user click on OK or other button....
e.g.
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
editText1.clearfocus();
editText2.clearfocus();
editText3.clearfocus();
....
}
}
You might want to try using: addTextChangedListener(..) in this case.
Sounds like you could be having issues with touch mode, from the android docs:
"The relationship between touch mode, selection, and focus means you must not rely on selection and/or focus to exist in your application."
To expand on #dong221, and incorporating the comment made by #Harald, one way to clear the focus without having to keep track of the last selected EditText is to get a reference to the currentFocus from the window object. Something like this:
myDoneButton.setOnClickListener { v ->
// Assuming we are in an Activity, otherwise get a reference to the Activity first
window.currentFocus?.clearFocus()
}
It works if you bind onFocusChangeListener to the view element you want to be observed
editText.onFocusChangeListener = this
editText.setOnClickListener(this)
by keyword this it means the ViewHolder class