简体   繁体   中英

Android TV button does not show focused drawable

I plan to publish a game on Android TV. I have developed the main screen for the game which contains some buttons.

What I need is to have different drawables for each button state. I need only three states:

  1. Default
  2. Focused
  3. Pressed

In order to have different drawables to appear according to different button states I created the following .xml file:

buttonquickgame.xml

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@drawable/greenbutton_focused" /> <!-- pressed -->
    <item android:state_focused="true"
        android:drawable="@drawable/greenbutton_focused" /> <!-- focused -->
    <item android:drawable="@drawable/greenbutton" /> <!-- default -->
</selector>

I have placed this xml file in res/drawable folder. I followed exactly what Google suggests here: https://developer.android.com/training/tv/start/navigation.html

Then I set the background of the button to be the previous .xml file like this:

<Button
    android:id="@+id/start"
    android:layout_width="250.0dip"
    android:layout_height="60.0dip"
    android:layout_marginTop="20dip"
    android:background="@drawable/buttonquickgame"
    android:onClick="OnClickButton"
    />

The problem is the applications loads, the button has the default state icon, when I press it, it displays the pressed drawable correctly, but it never displays the focused drawable no matter what.

I have tried everything. I even requestFocus programmaticaly, the focused drawable won't appear. I have checked this behaviour in emulator and my Sony Android TV with no success.

Do you have any ideas of what am I missing here?

Thank you very much in advance!

After a lot of hours of trial and error I found a working solution.

The new selector xml file is as follows:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@drawable/orangebutton_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
        android:drawable="@drawable/orangebutton_focused" /> <!-- focused -->
    <item android:drawable="@drawable/orangebutton" /> <!-- default -->
    <item android:drawable="@drawable/orangebutton_focused" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>

</selector>

In the Activity I manually select the button and request focus like this:

   Button quick_start = (Button) findViewById(R.id.start);
   quick_start.setSelected(true);
   quick_start.requestFocus();

Now it is working fine on my AndroidTV and the emulator as well.

I hope that this answer will save you some time if you face a similar problem!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM