how to make android TV app accesiible by DPAD?

1.7k views Asked by At

I have created an Android App for Android TV, I want to know how can I enable D-pad naigation to my menu. I am new to android TV applications,and could not find anyother way to do the same. For menu I have added 4 TextViews, but they are not accesible by D-Pad, is there anyother way to make them accessible to DPAD or I should use anyother View for this.

Here's the layout file:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusableInTouchMode="true"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/mainframe"
        android:layout_width="810dp"
        android:layout_height="538dp"
        android:layout_marginStart="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="1dp"
        android:layout_marginBottom="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/linearLayout"
        app:layout_constraintTop_toTopOf="parent">

    </FrameLayout>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="148dp"
        android:layout_height="0dp"
        android:layout_marginTop="1dp"
        android:layout_marginBottom="1dp"
        android:background="@color/colorPrimaryDark"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0">

        <TextView
            android:id="@+id/NIYA"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="20dp"
            android:fontFamily="cursive"
            android:nextFocusDown="@+id/livePreview"
            android:text="NIYA"
            android:textColor="@color/colorPrimary"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/livePreview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="100dp"
            android:fontFamily="@font/montserrat"
            android:gravity="center_horizontal"
            android:nextFocusDown="@+id/AddDevice"
            android:text="Live Preview"
            android:textColor="@color/colorPrimary"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/AddDevice"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:fontFamily="@font/montserrat"
            android:gravity="center_horizontal"
            android:nextFocusDown="@+id/deviceManager"
            android:text="Add A Device"
            android:textColor="@color/colorPrimary"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/deviceManager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:fontFamily="@font/montserrat"
            android:gravity="center_horizontal"
            android:nextFocusDown="@+id/chooseStorage"
            android:text="Device Manager"
            android:textColor="@color/colorPrimary"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/chooseStorage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:fontFamily="@font/montserrat"
            android:gravity="center_horizontal"
            android:text="Choose Storage"
            android:textColor="@color/colorPrimary"
            android:textSize="18sp" />
  </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
2

There are 2 answers

0
Ian G. Clifton On

In general, you don't want to use a TextView for something like this. It sounds to me like these views are meant to be Buttons because you want the user to interact with them. Using a Button is also better for accessibility and you can always style a Button to look how you want.

The reason your layout doesn't work is because a TextView is not focusable by default. Although I highly recommend using a Button in this case, you can make a TextView focusable in the XML by adding android:focusable="true" or by setting focusable to true at runtime with the setFocusable setter method. Just note that you need to make sure the View itself has a means of showing that it's focused (e.g., a StateListDrawable).

0
kiran On

Implement key listener to achieve this thing, just like click listener. For example:

   yourTextview.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            //TODO Auto-generated method stub
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                    return true;
                } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                    return true;
                }  if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
                    return true;
                }  if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
                    return true;
                }
                else if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    return true;
                } else if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
                    return true;
                }
            }
            return false;
        }
    }); 

Do whatever you want to perform inside the if statement on each action. You can add these if your control does not respond on the key event by adding this in your xml android:focusable="true" or android:clickable true, but this is not necessary it should work without these