Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 566
Nick Avatar asked Dec 07 '25 13:12

Nick


1 Answers

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!

like image 130
Nick Avatar answered Dec 09 '25 03:12

Nick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!