2

I need to show a preference dialog that wait for Joypad keypress.

I know that DialogFragment has its own Window then has its own onKeyListener.

I can easily catch Joypad press by setting a listener like that.

public class MyDialogFragment extends PreferenceDialogFragmentCompat  {

    @Override
    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
        super.onPrepareDialogBuilder(builder);

        builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {

                // do stuff with intercepted key press

                return true;
            }
        });

    }
}

But, I have trouble catching GenericMotionEvents. In order to intercept them, I've overridden onGenericMotionEvents in the Activity class and eventually forward the Event by calling a method on MyDialogFragment class.

It works 100% correctly when MyDialogFragment is not shown as when an analog trigger/direction stick is moved, I can get an event.

The weird part is that IF MyDialogFragment is shown, then I can get only analog direction stick events BUT NOT left/right analog triggers events.

Does someone know why and how to fix this behaviuor?

Mahdi Zarei
  • 5,644
  • 7
  • 24
  • 54
Davide Berra
  • 6,387
  • 2
  • 29
  • 50

1 Answers1

0

I've had a similar issue some time ago. You can use onGenericMotionEvent of Dialog or even some of the Views. It has some limitations though and works not as expected sometimes. It does work as intended though - it is just that sometimes all the generic motion events are being intercepted by something else and they aren't propagated any further anywhere - in this situation, you won't receive the callback trigger.

That is what was happening in my case and even overriding the callback method in Dialog(I haven't tried View though) failed to give me the needed result. What I did is a bit of a kludgy trick, but it did the job. I created my activity's UI as a child of one parent FrameLayout and my dialog UI was the topmost element in this FrameLayout. This trick allowed me to use the activity's native onGenericMotionEvent callback. It added some navigation handling overhead and was generally possible because of allowing UI design(without dialog shadow etc) but yeah...

Maybe some of these approaches will help you.

Pavlo Ostasha
  • 14,527
  • 11
  • 35