Akshat Akshat - 1 year ago 91
Android Question

SearchView - Show starting of the text when suggestion is selected and set

I guess it must be simple but can't figure it out myself.

I have a searchview in my app which on typing gives out some suggestions. On selection(tap) of a particular suggestion from the list dropped down, the suggestion text is set into the searchview, but the focus/cursor is at the end of the text. Image below you can see the cursor is at the end.enter image description here

Is there anyway to set the cursor to the start of the text in searchview? So that when a text is set, the starting of the text is displayed and not the end part.


Answer Source

1 - Actually searchView uses textview as edittext where we set query. An EditText is a TextView, it inherits TextView. So first of all find id of that textview as below.

TextView searchText = (TextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);

or use this method

int id = searchView.getContext()
               .getIdentifier("android:id/search_src_text", null, null); 
         EditText editText = (EditText) searchView.findViewById(id);

2 - Now use this edittext to search your data.

3 - After getting result of your query you can use editText.setSelection(0) method to set focus/cursor on 0 position....

It works for me, hope this will help you too....

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download