How to set Android ActionBar's next focusable control programmatically
Asked Answered
D

1

6

I am developing an application for Google TV where all the navigation will be done using D-Pad/Keyboard.

My Activity has an ActionBar and a few Lists horizontally beneath it. say list A, B & C. Lists B & C load data based on the selected item of list A.. so they may/may not load data.

Issues is that when ActionBar has focus and i press DOWN D-Pad key, the focus is lost and is not transfered to List C (or B if C has no data ) .. y?

To achieve this, i tried to set ActionBar's next focusable view programatically like following

getActionBar().getCustomView().setNextFocusDownId(R.Id.list_C_id);

but not worked.

I also tried checking the layout hierarchy using Hierarchy Viewer ... but, frankly, i did not understand what to do with its findings.

How can i set Android ActionBar's next focusable control programmatically??

Any help is greatly appreciated.

Activity's XML is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mainContainer" style="@style/main" >

<!-- Splash Screen -->
<LinearLayout
    android:id="@+id/splash_screen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/welcome"
    android:orientation="vertical" >
</LinearLayout> 

<!-- main content holder.. it is shown afetr some time of showing above splash screen -->
<LinearLayout
    android:id="@+id/main_content_screen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:focusable="true"
    android:visibility="invisible">

    <!-- All Packages List -->

    <LinearLayout
        android:layout_width="205dp"
        android:layout_height="match_parent"
        android:background="@drawable/content_round_bg"
        android:focusable="true"
        android:orientation="vertical" >

        <!-- Header of Packages-->
        <RelativeLayout
            style="@style/marginLeft5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"                
            android:background="@drawable/content_round_bg" >

            <ImageView
                android:id="@+id/packagesIcon"
                style="@style/titleIcon"
                android:src="@drawable/packages" >
            </ImageView>

            <TextView
                style="@style/title"
                android:layout_toRightOf="@+id/packagesIcon"
                android:text="Packages" >
            </TextView>

            <ProgressBar
                android:id="@+id/progressBarPackages"
                style="@style/progressBar" />
        </RelativeLayout>

        <!-- <View style="@style/horizontalSeperator" /> -->

        <ListView
            android:id="@+id/allPackagesList"
            style="@style/list"
            android:nextFocusRight="@+id/gridChannels">
        </ListView>

        <!-- Message which is hidden by default and is made visible if list has no data -->
        <LinearLayout
            android:id="@+id/packageNotFound"
            style="@style/emptyListIdentifier" >

            <ImageView
                style="@style/titleIcon"
                android:src="@drawable/info" >
            </ImageView>

            <TextView
                style="@style/emptyListIdentifierText"
                android:layout_marginLeft="5dp"
                android:text="Packages not available" >
            </TextView>
        </LinearLayout>
    </LinearLayout>

    <View style="@style/verticalSeperator" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:focusable="true"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/relativeLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:focusable="true"
            android:layout_below="@+id/titleContainer" >

            <LinearLayout
                android:id="@+id/channelsContainer"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.35"
                android:background="@drawable/content_round_bg"
                android:focusable="true"
                android:orientation="vertical" >

                <!-- Header of Channels-->
                <RelativeLayout
                    style="@style/marginLeft5"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/content_round_bg" >

                    <ImageView
                        android:id="@+id/channelsIcon"
                        style="@style/titleIcon"
                        android:src="@drawable/channels" >
                    </ImageView>

                    <TextView
                        style="@style/title"
                        android:layout_toRightOf="@+id/channelsIcon"
                        android:text="Channels" >
                    </TextView>

                    <ProgressBar
                        android:id="@+id/progressBarChannels"
                        style="@style/progressBar" />
                </RelativeLayout>

                <RelativeLayout style="@style/listsContiner" android:focusable="true" >

                    <GridView
                        android:id="@+id/gridChannels"
                        style="@style/grid"
                        android:divider="#10F1F1F1"
                        android:nextFocusLeft="@+id/allPackagesList"
                        android:nextFocusRight="@+id/gridAllVODs"
                        android:numColumns="2" />
                </RelativeLayout>

                <!-- Message which is hidden by default and is made visible if list has no data -->
                <RelativeLayout
                    android:id="@+id/channelsNotFound"
                    style="@style/emptyListIdentifier" >

                    <ImageView
                        android:id="@+id/NoChannelsIcon"
                        style="@style/titleIcon"
                        android:src="@drawable/info" >
                    </ImageView>

                    <TextView
                        style="@style/emptyListIdentifierText"
                        android:layout_toRightOf="@+id/NoChannelsIcon"
                        android:text="Channels are currently not available" >
                    </TextView>
                </RelativeLayout>
            </LinearLayout>

            <View style="@style/verticalSeperator" />

            <LinearLayout
                android:id="@+id/vodsContainer"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.65"                    
                android:background="@drawable/content_round_bg"
                android:orientation="vertical" >

                 <!-- Header of VODs-->
                <RelativeLayout
                    style="@style/marginLeft5"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/content_round_bg" >

                    <ImageView
                        android:id="@+id/VODsIcon"
                        style="@style/titleIcon"
                        android:src="@drawable/vod" >
                    </ImageView>

                    <TextView
                        style="@style/title"
                        android:layout_toRightOf="@+id/VODsIcon"
                        android:text="VOD" >
                    </TextView>

                    <ProgressBar
                        android:id="@+id/progressBarVODs"
                        style="@style/progressBar" />
                </RelativeLayout>                    

                <RelativeLayout style="@style/listsContiner" >
                    <GridView
                        android:id="@+id/gridAllVODs"
                        style="@style/grid"
                        android:divider="#10F1F1F1"
                        android:nextFocusLeft="@+id/gridChannels"
                        android:nextFocusRight="@+id/allPackagesList"
                        android:numColumns="1" />
                </RelativeLayout>

                <!-- Message which is hidden by default and is made visible if list has no data -->

                <RelativeLayout
                    android:id="@+id/videosNotFound"
                    style="@style/emptyListIdentifier" >

                    <ImageView
                        android:id="@+id/NoVODsIcon"
                        style="@style/titleIcon"
                        android:src="@drawable/info" >
                    </ImageView>

                    <TextView
                        style="@style/emptyListIdentifierText"
                        android:layout_centerInParent="true"
                        android:layout_toRightOf="@+id/NoVODsIcon"
                        android:text="Videos are currently not available" >
                    </TextView>
                </RelativeLayout>
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

All the containers of each List/Grid upto Top most LinearLayout have set android:focusable="true" directly or in its style.

EDIT: Sample app describing exactly my spp's screen can be downloaded from here

Please include in the first list. You can search it by its id allPackagesList in the main.xml file. I forgot to insert it. sorry

Issues in this app are:

  1. Focus does not come to List/Grids from ActionBar Item.
  2. Navigation between ActionBar Items takes more than one D-Pad "LEFT" OR "RIGHT" presses to move
Distributor answered 11/2, 2012 at 10:16 Comment(0)
D
0

Is the List wrapped inside another container? Try adding android:focusable="true" for your Lists and their containers..

Degree answered 13/2, 2012 at 1:36 Comment(7)
it did not work ... i have updated my question and placed the activity's XML.. i can use some custom header with buttons (to act as action buttons) and image for a logo.. but i dont want to... as i want to use the inbuilt ActionBar. Please help me sort it out as this is very urgentDistributor
I dont see ListView or ActionBar in the xml.Degree
ListView & two GridView are there in xml ... and i did not add ActionBar in XML ... i just set theme to holo & inflated a menu in onCreateOptionsMenu .. and the ActionBar appeared ... i followed the android ActionBar tutorial from developer.android.com/guide/topics/ui/actionbar.html. i am sory if i have missed something as i am new to androidDistributor
sorry I can't help here. I tried to create a project to reproduce the problem but its a very big xml file and I am not sure of the way you are using it. Its difficult for me to debug without some simple source code that reproduces the issue easily.Degree
I have updated my question and added a sample application's link. You can download it... its XML is exactly the same as my activity ... so you would know exactly what issues i m facing ... ur help is greatly appreciatedDistributor
Joshi -- i have created a custom header to imitate inbuilt ActionBar as i was stuck and project deadline is very near ... but please do let me know if you find its reason .. thanks for ur timeDistributor
Amir- I've gone through your code and you have to set android:focusable="true" in your Imageview so that first Listview will take focus when you d-pad down .Also view is not taking focus when d-pad down from Actionbar option menu though.Meanwhile you try not displaying option items in the Actionbar and show items when user presses Keyboard menu or use custom header as you did.Sea

© 2022 - 2024 — McMap. All rights reserved.