• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Implement onKeyListener

snowman24

Newbie
Having trouble getting an onKeyListener to work. I have created a dynamic gridlayout populated with textviews. The gridlayout supports horizontal and vertically scrolling, no problems. Once gridlayout is created and populated I create a view as shown, set the first cell background to Green, for no better reason than to test. I then have the onKeyListener. I have the Log just to see if anything happens, which it doesn't. As I scroll the grid I get nothing in logcat. Am I totally missing the way to use onKeyListener?

Java:
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

View fc = new View(this);
        fc = (View) gridLayoutE.getChildAt(0);
        fc.setBackgroundColor(Color.GREEN);
        gridLayoutE.requestFocus();

fc.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                Log.d("TEST ", String.valueOf(keyEvent));
                if(keyEvent.getAction() == KeyEvent.ACTION_DOWN){
                    if(i== KeyEvent.KEYCODE_DPAD_RIGHT){
                      Log.d("TEST ", String.valueOf(keyEvent));

                        return true;
                    }

                }

                return false;
            }
        });
}
 
Where did you get this code from?

Code:
View fc = new View(this);

You don't create a View yourself. It's defined in the layout XML. Your code then uses findViewById() to get a reference to it, and add Listeners etc.
 
The View fc was created to draw focus to the gridlayout and set the first cell background color. I see now I really don't need that. So I have deleted out the View fc. To create the onKeyListener I have following, but still not getting any type of response when pressing any part of Dpad.

Java:
 gridLayoutE.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                Log.d("TEST ", String.valueOf(keyEvent));
                if(keyEvent.getAction() == KeyEvent.ACTION_DOWN){
                    if(i== KeyEvent.KEYCODE_DPAD_RIGHT){
                        Log.d("TEST ", String.valueOf(keyEvent));

                        return true;
                    }

                }

                return false;
            }
        });
 
I think what's happening is that your GridLayout View is intercepting the click event, and it does not propagate to the child TextView.

See here for a possible workaround

https://stackoverflow.com/questions/38347937/android-propagating-click-from-parent-to-child
I do believe you are correct. The gridlayout maybe intercepting the dpad press. When I click the dpad right, the gridlayout does scroll right as it does if I press down or up. I will try to set the listener on the child of the gridlayout. Thanks for taking a look.
 
Back
Top Bottom