When my Android app was adapted to the TV remote control, I added two groups of buttons in the layout file. I tried to let them get the focus. The left and right keys of the remote control can switch the focus to different buttons. I hope to move the focus to the rootView with the up key. I used nextFocusDown=”@id/btn_left_previous”. When the down key is pressed, the focus is transferred from the root to “@id/btn_left_previous”. But when I want to transfer the focus from the button to the root through the up key nextFocusUp=@+id/selector_bg”, it fails. The following is my layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/selector_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:focusable="true"
android:focusableInTouchMode="true"
android:nextFocusDown="@id/btn_left_previous">
<FrameLayout
android:id="@+id/view_content_window"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:background="@color/colorBlack" />
<ImageButton
android:id="@+id/btn_hide"
android:layout_width="wrap_content"
android:layout_height="@dimen/player_title_height"
android:background="@null"
android:src="@drawable/icon_hide"
android:layout_centerHorizontal="true"
android:visibility="gone" />
<LinearLayout
android:id="@+id/view_connected_title"
android:layout_width="match_parent"
android:layout_height="@dimen/player_title_height"
android:background="@color/colorItemBg"
android:orientation="horizontal"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/px10"
android:gravity="center"
android:minWidth="@dimen/px50"
android:textColor="@color/colorPlayerTitle"
android:textSize="@dimen/player_title_textsize" />
<com.kgzn.castscreen.screenshare.widget.StrokeTextView
android:id="@+id/tv_dev_name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="@dimen/px30"
android:gravity="center"
android:textColor="@color/colorPlayerTitle"
android:textSize="@dimen/player_title_textsize" />
</LinearLayout>
<include
android:id="@+id/music_picture"
layout="@layout/music_picture"
android:visibility="gone" />
<include
android:id="@+id/view_ksharemediacontroller"
layout="@layout/view_ksharemediacontroller"
android:visibility="gone" />
<include
android:id="@+id/view_mymediacontroller"
layout="@layout/view_mymediacontroller"
android:visibility="gone" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="@dimen/px228"
android:layout_height="@dimen/px70"
android:layout_alignParentBottom="true"
android:layout_marginStart="@dimen/px19"
android:layout_marginBottom="@dimen/px19"
android:background="@android:color/transparent">
<androidx.constraintlayout.widget.Group
android:id="@+id/group_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:constraint_referenced_ids="btn_left_esc, btn_left_next, btn_left_previous" />
<ImageButton
android:id="@+id/btn_left_previous"
android:layout_width="@dimen/px70"
android:layout_height="@dimen/px70"
android:background="@null"
android:contentDescription="@null"
android:scaleType="centerInside"
android:src="@drawable/selector_previous"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/btn_left_next"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:focusable="true"
android:nextFocusLeft="@id/btn_right_next"
android:nextFocusUp="@id/selector_bg" />
<ImageButton
android:id="@+id/btn_left_next"
android:layout_width="@dimen/px70"
android:layout_height="@dimen/px70"
android:layout_marginStart="@dimen/px9"
android:layout_marginEnd="@dimen/px9"
android:background="@null"
android:contentDescription="@null"
android:scaleType="centerInside"
android:src="@drawable/selector_next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/btn_left_esc"
app:layout_constraintStart_toEndOf="@id/btn_left_previous"
app:layout_constraintTop_toTopOf="parent"
android:nextFocusUp="@id/selector_bg" />
<ImageButton
android:id="@+id/btn_left_esc"
android:layout_width="@dimen/px70"
android:layout_height="@dimen/px70"
android:background="@null"
android:contentDescription="@null"
android:scaleType="centerInside"
android:src="@drawable/selector_esc"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/btn_left_next"
app:layout_constraintTop_toTopOf="parent"
android:nextFocusRight="@id/btn_right_esc"
android:nextFocusUp="@id/selector_bg" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="@dimen/px228"
android:layout_height="@dimen/px70"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="@dimen/px19"
android:layout_marginBottom="@dimen/px19"
android:background="@android:color/transparent">
<androidx.constraintlayout.widget.Group
android:id="@+id/group_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:constraint_referenced_ids="btn_right_esc, btn_right_previous, btn_right_next" />
<ImageButton
android:id="@+id/btn_right_esc"
android:layout_width="@dimen/px70"
android:layout_height="@dimen/px70"
android:background="@null"
android:contentDescription="@null"
android:scaleType="centerInside"
android:src="@drawable/selector_esc"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/btn_right_previous"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:nextFocusLeft="@id/btn_left_esc"
android:nextFocusUp="@id/selector_bg" />
<ImageButton
android:id="@+id/btn_right_previous"
android:layout_width="@dimen/px70"
android:layout_height="@dimen/px70"
android:layout_marginStart="@dimen/px9"
android:layout_marginEnd="@dimen/px9"
android:background="@null"
android:contentDescription="@null"
android:scaleType="centerInside"
android:src="@drawable/selector_previous"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/btn_right_next"
app:layout_constraintStart_toEndOf="@id/btn_right_esc"
app:layout_constraintTop_toTopOf="parent"
android:nextFocusUp="@id/selector_bg" />
<ImageButton
android:id="@+id/btn_right_next"
android:layout_width="@dimen/px70"
android:layout_height="@dimen/px70"
android:background="@null"
android:contentDescription="@null"
android:scaleType="centerInside"
android:src="@drawable/selector_next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/btn_right_previous"
app:layout_constraintTop_toTopOf="parent"
android:nextFocusRight="@id/btn_left_previous"
android:nextFocusUp="@id/selector_bg" />
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
Finally, I achieved my expected effect by binding key listeners to each control in the Java code, as shown below. I just want to know why my changes to the layout file did not achieve the expected effect.
View.OnKeyListener viewKeyListener = new View.OnKeyListener() {
@Override
public boolean onKey(View view, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_UP && event.getAction() == KeyEvent.ACTION_DOWN && groupRightPpt.getVisibility() == View.VISIBLE) {
Log.d("lyk", "upupup");
rootView.requestFocus();
return true;
}
return false;
}
};
btnLeftPrevious.setOnKeyListener(viewKeyListener);
btnLeftNext.setOnKeyListener(viewKeyListener);
btnLeftEsc.setOnKeyListener(viewKeyListener);
btnRightEsc.setOnKeyListener(viewKeyListener);
btnRightPrevious.setOnKeyListener(viewKeyListener);
btnRightNext.setOnKeyListener(viewKeyListener);