2

In my Image there is a sidebar and ListView there, I access every Button by using D-PAD. When side bar search button has focus and I press right D-PAD button, I want the focus to go to the ListView's first item. But, in my case it goes to the second item. If Down arrow has focus and i press right button it goes to 4th list item.So , i need to prevent this also.How to move the focus to the 1st one?

if (Search != null && Search.hasFocus()) {
            // Search.setNextFocusRightId(R.id.listView);
//          listView.smoothScrollToPosition(0);
            listView.setChoiceMode(1);
            listView.setItemChecked(0, true);
        }

enter image description here

learner
  • 3,092
  • 2
  • 21
  • 33
  • could you brief what you actually want? @Boopathi. and what is the sidebar? is it a view or something? post the layout xml if you could. – Vigneshwaran Murugesan Oct 17 '14 at 10:26
  • @Boopathi by default focus always on first item of listview. i think you have if condition in getView() method Adapter. – Lokesh Oct 18 '14 at 06:52
  • http://dev.booknow.club/api/category.php?format=json http://dev.booknow.club/api/listing.php?category_id=1&format=json – learner Oct 18 '14 at 08:19

2 Answers2

2

Make a dummy view without background and place it in the same layout as your listview. Make it focusable and set nextFocusRight of your Search view to id/dummy.

<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:choiceMode="singleChoice"
    android:listSelector="@null"
    android:background="@null"
    ></ListView>
<View
    android:id="@+id/dummy"
    android:layout_width="1dp"
    android:layout_height="1dp"
    android:focusable="true"
    />

Your searchView:

<YourSearchView
 android:nextFocusItemRight=@+id/dummy/>

Then in the code, set an OnFocusChangedListener to the dummy view which will indicate that the ListView is supposed to gain focus. Inside the listener, request focus for the listview and set selection to 0 as follows:

View dummy = rootView.findViewById(R.id.dummy);
        dummy.setOnFocusChangeListener(new View.OnFocusChangeListener()
        {
            @Override public void onFocusChange(View v, boolean hasFocus)
            {
                if (hasFocus)
                {
                    listView.post(new Runnable() {
                        @Override
                        public void run() {
                            listView.requestFocus();
                            listView.setSelection(0);
                        }
                    });
                }
            }
        });

Make sure you use @+id for nextFocusItemRight instead of @id. Hope it helps, otherwise post a comment.

vandus
  • 3,248
  • 3
  • 30
  • 44
  • i tried above idea,it is not working.If search button has focus i click right D-Pad button ,now dummy view and list view both getting focused. – learner Oct 09 '14 at 10:57
  • did you make dummy view focusable in xml? – vandus Oct 09 '14 at 10:57
  • How do you know they both have focus? They actually cannot have focus at the same time. Try making the dummy view be to the left of listview. (android:toLeftOf=listview). – vandus Oct 09 '14 at 11:01
  • I am using linear layout, android:toLeftOf is not available – learner Oct 09 '14 at 11:09
  • Then just place the dummy view before the listview. – vandus Oct 09 '14 at 11:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62743/discussion-between-vandus-and-boopathi). – vandus Oct 09 '14 at 11:11
2

The best thing to do is Override the dispatchKeyevent in your Activity class then set the focus/selection to the place wherever you like in Listview .

    @Override
public boolean dispatchKeyEvent(KeyEvent event) {




    boolean handled  =  super.dispatchKeyEvent(event);

    if(!handled  &&  event.getAction() ==  KeyEvent.ACTION_UP)
    {
        if(yourParentViewGroup.getFocusedChild() is  your SearchView &&  event.getKeyCode() ==  KeyEvent.KEYCODE_DPAD_RIGHT )
        {

            yourListvIew.setSelction(0);
            handled = true;
        }

    }

    return handled;

But remember to return "true" if you are going to handle the Focus or selection logic else it will invoke android focusfinder algorithm and focus/selection will shift to the nearest View.

manjusg
  • 2,275
  • 1
  • 22
  • 31