77540

List view Focus while using D-PAD

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); }

<img src="https://i.stack.imgur.com/XVuCd.jpg" alt="enter image description here">

Answer1:

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.

Answer2:

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.

Recommend

  • Incompatible types of fragment
  • Update ListView in Master/Detail after a form is saved
  • Resetting checkboxes in Android's Alert Dialog
  • Highlight ListView item at creation time via code
  • jQuery & CSS - Cut text by height, no truncate
  • Is storing an OAuth token in cookies bad practise?
  • How can I encode a filename according to RFC 2231?
  • Fancy dynamic list in Android: TableLayout vs ListView
  • React Native fetch is not a function
  • UWP change ComboBox position when opening
  • Underlying type for Tuple in Swift
  • how to set id for each item in ListView
  • Galaxy S4 Android Custom Camera Issue
  • Flash radiobutton: how do I get the selected radiobutton?
  • WPF version of .ScaleControl?
  • How can I restyle a word when rendering a pdf with pdf.js?
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • Conversion from string “a” to type 'Boolean' is not valid
  • Excel's Macro-Recorder usage
  • How can I extract results of aggregate queries in slick?
  • OOP Javascript - Is “get property” method necessary?
  • How to use carriage return with multiple line?
  • PHP - How to update data to MySQL when click a radio button
  • Weird JavaScript statement, what does it mean?
  • Data Validation Drop Down Box Arrow Disappearing
  • How to set the response of a form post action to a iframe source?
  • Java static initializers and reflection
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • Change div Background jquery
  • need help with bizarre java.net.HttpURLConnection behavior
  • Qt: Run a script BEFORE make
  • Bitwise OR returns boolean when one of operands is nil
  • unknown Exception android
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • reshape alternating columns in less time and using less memory
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app