Toolbar + SearchView + textSelection = bad position of the text selection bar

Background

I am trying to use my application to have more constructive design and therefore I have a toolbar that is set as the activity's actionBar.

I have a SearchView that allows you to search for the listView items below.

Problem

Thing is, you can select text in the SearchView (for copying, cutting, etc.), but when that happens, the toolbar will display the text selection toolbar on top of it, making it and the text itself hidden:

Before selecting text:

enter image description here

After selecting text:

enter image description here

What i tried

I tried to disable text selection using this code:

  final EditText searchTextView=(EditText)searchView.findViewById(R.id.search_src_text);
  if(searchTextView!=null&&VERSION.SDK_INT>=VERSION_CODES.HONEYCOMB)
    searchTextView.setTextIsSelectable(false);

      

But it didn't do anything. I also tried to find how to listen to an even text selection (so I can set the bar to marginTop or something), but I couldn't find it.

The only thing I managed to do was use this code, which tells me when the text selection bar appears (but not when it disappears):

  searchTextView.setOnCreateContextMenuListener(new OnCreateContextMenuListener()
  {
  @Override
  public void onCreateContextMenu(final ContextMenu menu,final View v,final ContextMenuInfo menuInfo)
    {
    Log.d("AppLog","onCreateContextMenu");
    }
  });

      

It won't help. I can't even close the menu. I think it won't even help, as some devices may show something else instead of the toolbar (for example, on LG devices where they have a small popup).

Code

The layout is basically a vertical LinearLayout where the first item is the toolbar:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <android.support.v7.widget.Toolbar
    android:id="@+id/activity_app_list__toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:colorControlNormal="?attr/colorControlNormal"
    android:minHeight="?attr/actionBarSize"
    android:theme="?attr/actionBarTheme"
    tools:ignore="UnusedAttribute"/>
  ...

      

The theme used is set to hide the normal action bar:

  <style name="AppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
  ...

      

The action bar XML menu is pretty simple and contains three action items, with the first one being searchView:

<item
    android:id="@+id/menuItem_search"
    android:icon="?attr/app_search_menu_icon"
    android:title="@string/search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always|collapseActionView"/>

      

SearchView handling is done with a custom class I created that even supports older Android versions. This is the main function that handles searchView:

  @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
  public void init(final MenuItem searchMenuItem,final int hintResId,final OnQueryTextListener onQueryTextListener,final OnActionExpandListener onActionExpandListener)
    {
    this._searchMenuItem=searchMenuItem;
    if(_searchView==null)
      {
      _searchView=(SearchView)MenuItemCompat.getActionView(searchMenuItem);
      if(_searchView==null)
        {
        MenuItemCompat.setShowAsAction(searchMenuItem,MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW|MenuItem.SHOW_AS_ACTION_ALWAYS);
        MenuItemCompat.setActionView(searchMenuItem,_searchView=new SearchView(_context));
        }
      _searchView.setQueryHint(_context.getString(hintResId));
      if(VERSION.SDK_INT<VERSION_CODES.HONEYCOMB)
        {
        final EditText searchTextView=(EditText)_searchView.findViewById(R.id.search_src_text);
        if(searchTextView!=null)
          {
          searchTextView.setScroller(new Scroller(_context));
          searchTextView.setMaxLines(1);
          searchTextView.setVerticalScrollBarEnabled(true);
          searchTextView.setMovementMethod(new ScrollingMovementMethod());
          final int searchTextColorResId=App.getResIdFromAttribute(_context,android.R.attr.textColorPrimary);
          if(searchTextColorResId!=0)
            searchTextView.setTextColor(_context.getResources().getColor(searchTextColorResId));
          else
            {
            // TODO workaround for some v2.3 devices that can't get the correct color. remove this when stopping the support for v2.3
            TextView searchBadge=(TextView)_searchView.findViewById(R.id.search_badge);
            if(searchBadge!=null)
              searchTextView.setTextColor(searchBadge.getTextColors());
            }
          }
        }
      _searchView.setOnQueryTextListener(onQueryTextListener);
      MenuItemCompat.setOnActionExpandListener(searchMenuItem,onActionExpandListener);
      }
    }

      

The function is called at the end of the "onCreateOptionsMenu" of any action / fragment that the searchView must have, for example:

    _searchHolder.init(menu.findItem(R.id.menuItem_search),R.string.search_for_apps,onQueryTextListener,onActionExpandListener);

      

Question

How can I solve this problem? Obviously, this is because the toolbar is just a view, so the text selection bar appears above it, but is there a way to fix this?

How do I avoid an extra toolbar on top of the one I was using?

Is there a way to support all devices in this regard?

Looking at google apps, it seems like they don't usually use the official searchView, but one of them. Only on Youtube seems to be official, but it looks like they are using the official actionBar as well (plus on strange color when selecting text).

+3


source to share


2 answers


I found in many apps (Google messenger, Contacts, etc.) text selection is disabled in search mode. You can do this by setting your toolbar theme as.

<style name="toolbarTheme" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="android:autoCompleteTextViewStyle">@style/SearchViewStyle</item>
        <item name="android:textColor">@android:color/white</item>
        <item name="android:textColorPrimary">@android:color/white</item>
        <item name="android:editTextColor">@android:color/white</item>        
</style>

<style name="SearchViewStyle" parent="@android:style/Widget.Holo.Light.AutoCompleteTextView">
        <item name="android:longClickable">false</item>
</style>

      



Or if you want youtube to add this line to your theme

<item name="windowActionModeOverlay">false</item>

      

+1


source


Thanks to Max's answer, I tried to find the AppCompat style files by clicking on the links one by one I found Widget.AppCompat.AutoCompleteTextView

. Override it in the toolbar theme with

<item name="android:autoCompleteTextViewStyle">@style/AppTheme.SearchViewStyle</item>

      




<style name="AppTheme.SearchViewStyle" parent="Widget.AppCompat.AutoCompleteTextView">
    <item name="android:longClickable">false</item>
</style>

      

Disables longClick on SearchView. This works on API 14 devices (haven't tried previous versions) with at least API 22.1.1.

0


source







All Articles