0

I have a SearchView button in my Toolbar and whenever I click the button, the SearchView expands and the keyboard pops up like it should. However, when it is in this initial state, there is no blinking cursor. I can type something and it will display the inputted text but it won't show the cursor. But once I touch the text area, then it displays the blinking cursor.

So how can I get SearchView to display the blinking cursor when the icon is clicked, without ever touching the text area?

By touching the "text area" I mean the area pointed by the arrow just to avoid confusion:

enter image description here

And here is my SearchView xml code if it's any help:

<android.support.v7.widget.SearchView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/search_view_toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:queryHint="@string/search">

</android.support.v7.widget.SearchView>

I'm accessing the SearchView as a MenuItem object in OnCreateOptionsMenu(). Any help is appreciated.

David Velasquez
  • 2,346
  • 1
  • 26
  • 46

2 Answers2

2

The cursor isn't visible because it has the same color with the actionbar. Try something like this

SearchView searchView = (SearchView) ((AppCompatActivity) context).findViewById(R.id.search_view);
final EditText e = (EditText) sitesSearchView.findViewById(searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null));
try {
   Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
   mCursorDrawableRes.setAccessible(true);
   mCursorDrawableRes.set(e, 0); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
} catch (Exception ex) {}

** EDIT **

try this

searchView.onActionViewExpanded();
Community
  • 1
  • 1
Vasileios Pallas
  • 4,801
  • 4
  • 33
  • 50
  • Then why is it that when I touch the text area, the cursor becomes visible? The cursor color becomes the color set in colorAccent in my style theme. – David Velasquez Feb 17 '16 at 02:26
0
<style name="AppTheme.NoActionBar2" parent="AppTheme">
    <item name="colorPrimary">@color/main_color</item>
    <item name="colorPrimaryDark">@color/main_color</item>
    <item name="colorAccent">@color/white</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>`enter code here`
    <item name="android:textColorSecondary">#ffffff</item>
    <item name="searchViewStyle">@style/Widget.SearchView</item>
</style>
Ryan M
  • 18,333
  • 31
  • 67
  • 74
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Ryan M Nov 11 '21 at 08:27