1

I am developing app for the google tv and i have to use left navigation bar in my app. I downloaded the google tv examples and am playing with the leftnavbar to make it work as per my requirements. I want it to behave like the one tv & movies OR youtube app for Google TV. It should expand when focus is moved to its tabs by LEFT D-Pad key and collapse when I move focus from it using RIGHT D-Pad key.

I did't know if there are any properties which will help me achieve this functionality.

So I tried by registering the setOnFocusChangeListener listener and calling flipOption(LeftNavBar.DISPLAY_AUTO_EXPAND) function in it as follows to force it to behave the way i want:

  private LeftNavBar getLeftNavBar() {
    if (mLeftNavBar == null) {
       -------
       -------
        mLeftNavBar.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {

                flipOption(LeftNavBar.DISPLAY_AUTO_EXPAND);
            }
        });


    }
    return mLeftNavBar;
}

Now the above code works fine when there is nothing in leftNavBar. It shows/hides upon receiving/loosing focus. But it does not work when I add some tabs in it.

Any Idea how to achieve it?

waqas
  • 342
  • 1
  • 3
  • 17
  • 1
    There is a problem with leftnavbar on Android 4.x. It only expands and collapses on GTV Honeycomb OS, but not on JB OS. So, leftnavbar will stop working as expected after GTV will update to v.4. It needs a fix. – mcd2000 Sep 09 '13 at 09:58

2 Answers2

1

The googletv-android-samples has both the LeftNavBar code and the LeftNavBarDemo. If you use the LeftNavBarDemo, you can see which settings you need to accomplish the behavior your looking for.

It's just setting the correct options - You really don't need to flipOption onFocusChange - it will expand and contract automagicly if you set the right initial options. I'd give you specifics, but each application is different.

  • I also tried it.. but could not make it work... if by "options" you mean the options settings by the buttons on LeftNavBarDemo's detail fragment. Then I have tried it too. but didn't work... but if u are talking about setting options in XML file, then I dont know how to do it. .. so can u please give some specifics??? – Aamir Jul 20 '12 at 05:48
  • You download the LeftNavBarLibrary & LeftNavBarDemo and run the LeftNavBarDemo and can't make that work? I'll see if I can put together a more detailed set of instructions in the AM. But if you can't get that working, you'll have a hard time getting much else working. – Les Vogel - Google DevRel Jul 20 '12 at 07:17
  • I am so sorry for wasting ur time and embarrassing myself. This just does not work on Emulator .. i checked by modifying the properties on the emulator.. I think the new ADT update has caused the emulator to behave this way... As soon as I installed the application on Google TV.. it began to work. – Aamir Jul 20 '12 at 10:16
0

I'm guessing that the GTV team have added some special sauce to the View Hierarchy in the GoogleTV OS because this magical method below somehow gets called when any of the children of the LeftNavBar gain focus.

protected void onDescendantFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (has(mDisplayOptions, LeftNavBar.DISPLAY_AUTO_EXPAND)) {
            setExpanded(hasFocus);
        }
    }

I can't see how to imitate this using other method so I implemented the hack below.

1) At the end of onFinishInflate() in LeftNavView.java add the following code to detect when the underlying listview gains focus so that you can expand the menu. Unfortunately it's not as simple as using hasFocus == false to reliably close the menu, so see step 2.

@Override
protected void onFinishInflate() {
   //...
   //existing code
   //...
   TabListView tabListView = (TabListView)mTabs.getView();
   tabListView.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus)
                setExpanded(true, true);
        }
    });
}

2) Add the following at the top of focusSearch(boolean hasFocus) in LeftNavView.java to collapse the menu (if the LeftNavBar already has focus and the DPAD direction is right.

@Override
public View focusSearch(View focused, int direction) {
    //if leftnav hasFocus but direction is right then collapse the nav menu
    if (hasFocus() && direction == View.FOCUS_RIGHT) {
        setExpanded(false, true);
    }

    //...
    //existing code
    //...
}

CAVEAT: With this hack you'll need to be careful that you don't have views that request focus when loaded (either calling .requesFocus() or implementing requestFocus/> tag in your layout). In this situation your view will gain focus but the menu will still be expanded and in some cases you'll not be able to get focus back on the menu using the dpad (depending on the layout of your view)

If you want to focus something in your newly loaded fragment then you could implement and call the method below (at the end of onActivityCreated()) as a workaround.

//e.g. passing in KeyEvent.KEYCODE_DPAD_RIGHT
public static void simulateKey(final int KeyCode) {
        new Thread() {
            @Override
            public void run() {
                try {
                    Instrumentation inst = new Instrumentation();
                    inst.sendKeyDownUpSync(KeyCode);
                } catch (Exception e) {
                    Log.e(TAG, "Exception when sendKeyDownUpSync " + e.toString());
                }
            }
        }.start();
    }
Damian
  • 8,062
  • 4
  • 42
  • 43