0

I have an Android Application inside a fragment, I am trying to get volume keys pressed event, but the event doesn't fire off.

I tried setting a breakpoint at onKey() method, it never went there.

Code below:-

import android.view.View.OnKeyListener;
 public class ExampleFragment extends Fragment implements OnKeyListener {
     @Override
     public boolean onKey(View v, int keyCode, KeyEvent event) {
         if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
             //to do code goes in here
             return true;
         }
         return false;
     }
}
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
Saranya Vs
  • 3
  • 1
  • 6

2 Answers2

1

You haven't set the Fragment as the KeyListener to anything. There needs to be a View registered to submit KeyEvents to.

In your Fragment, try something like:

@Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    // Create an EditText we want to Listen to.
    EditText e = new EditText(getActivity());
    // Assert this Fragment was the KeyListener.
    e.setOnKeyListener(this);
    // Return the View.         
    return e;
}

In an ideal world, a Button should not be responsible for listening to KeyEvents. It should only deal with clicks and presses, since this is how a Button behaves in the real world. In general, you should avoid giving any kind of graphical objects application-specific knowledge, it should be up to the application to decide what to do, and update the UI accordingly.

This kind of architecture commonly comes in the form of the Model View Controller.

Instead, you might want to try overriding onKeyDown in the Fragment's owning Activity, and routing that to a corresponding method in the Fragment.

See this example.

Mapsy
  • 4,192
  • 1
  • 37
  • 43
  • An `OnKeyListener` can be applied to *any* `View`. – Mapsy Jul 12 '17 at 15:22
  • ` startButton.setOnkeyListener(this)` I added in the code but still, it didn't capture the event for me. – Saranya Vs Jul 12 '17 at 15:27
  • It would be something like `startButton.setOnKeyListener(this)`; be wary of the capitalisation. If this answer has helped you, please consider marking the question as answered. :-) – Mapsy Jul 12 '17 at 15:28
  • Could you also let me know if I bind it to a button, will it call the on click function of Button? – Saranya Vs Jul 12 '17 at 15:31
  • Yes, if you program it that way. ;) – Mapsy Jul 12 '17 at 15:32
  • Could you show me to an Example, how to make it work? – Saranya Vs Jul 12 '17 at 15:34
  • Not unless you mark this question as answered, and post a new question. – Mapsy Jul 12 '17 at 15:40
  • My question isn't solved yet pal. this is what I have at the moment. Exactly the way we bind any listener, the question still remains the same, how to am I suppose to get the event fired off `startButton.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) || (keyCode == KeyEvent.KEYCODE_VOLUME_UP)){ startButtonPressed(); return true; } return false; } });` – Saranya Vs Jul 12 '17 at 15:46
0

I had the same problem. In my case, onKeyListener works right in the one Fragment but it doesn't work in the another. I found out that it depends on the type of keyboard. I had set on the EditText inputType="text" and it began to work. It is a miracle. I really can't understand what is going on. In my case problem arise in Huawei, on the genymotion Error didn't arise.

   <EditText
    android:id="@+id/answer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:inputType="text"
    />
gloomyad
  • 21
  • 2
  • I used virtual keypad input type, though my edit text input property wasn't set. I had to hack it with interface overriding a method to work on two different fragments. – Saranya Vs Nov 14 '17 at 14:42