Handling user Inputs from mouse pointer in Android TV Box on My Android Application

1.9k Views Asked by At

EDIT : This Question has transformed into another Question

Here is the link to updated Question. Android App Mouse Pointer Input Not working well on Activity UI Elements

This Question :

I am building an Android app which needs to run on Android TV Boxes as well as Tabs and Mobile Phones. I have been able to make the app as per desired need like handling user inputs from USB Mouse and Touch on Mobile Phone but have a problem in detecting USB Mouse inputs on TV Box. The Remote Control inputs are working perfectly fine but again mouse pointer clicks are undetected on app UI. UI of all other apps works perfect using both mouse and remote inputs.

Following is the output on a successful click detect using the mouse. (As Working on Mobile Phone) enter image description here

I am using the following listener to detect the mouse click or any click in the current code.

  btn.setOnTouchListener(new Button.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent arg1) {
            if ((arg1.getAction() == MotionEvent.ACTION_UP)) {
                // Button Click Using Mouse Detected
                // doing Desired Action Here
   
            }
            return true;
        }
    });

I have also added onClickListener() on the same button but has no problem on phone with both options.

Activity XML File :

 <!-- Text view to show click event data -->
 <TextView
    android:id="@+id/xd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"

    android:layout_marginTop="20sp"
    android:fontFamily="@font/poppinsregular"
    android:textColor="#000000"
    android:textSize="20sp"
    android:text="Test"
    android:visibility="visible" />
   <!--  button 1 -->
  <Button
    android:id="@+id/one"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/sync_btn_design"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:fontFamily="@font/poppinsregular"
    android:text="Tester 1"
    android:textColor="#ffffff"
    android:textSize="20sp"
    android:textStyle="bold" />
  <!--  button 2 -->
  <Button
    android:id="@+id/two"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10sp"
    android:background="@drawable/buttondesign"
    android:focusable="true"
    android:focusableInTouchMode="true"

    android:fontFamily="@font/poppinsregular"
    android:text="Tester 2"
    android:textColor="#ffffff"
    android:textSize="20sp"
    android:textStyle="bold" />

Basically any type of left click is not detected on Android TV Box app UI but works on Mobile Phones or Tabs.

What I Tried?

I used the following code for detecting clicks on my view. So all the mouse or touch events get detected and the output printed in Textview for Debugging on TextView id 'xd' in the given XML Code.

https://developer.android.com/training/gestures/detector#detect-all-supported-gestures

What are the options I can try from here to solve it?

1

There are 1 best solutions below

1
On
 private int focusedItem=0;
btn.setOnKeyListener(new View.OnKeyListener(){
@Override
public boolean onKey(View v, int keyCode, KeyEvent event){
RecyclerView.LayoutManager lm=recyclerView.getLayoutManager();
if(event.getAction()==KeyEvent.ACTION_DOWN){
if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN){return tryMoveSelection(lm, 1);} else if (keyCode == KeyEvent.KEYCODE_DPAD_UP){return tryMoveSelection(lm, -1);}}
return false;
}
});