Set width and color of SearchView tooltip in XML

I added SearchView functionality to the toolbar and I wanted to make it full width when expanding it, and also change the tooltip text color to white instead of black. Finally, I was able to do it programmatically:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setMaxWidth(4000);
    ((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(getResources().getColor(R.color.md_text_white));
    searchView.setIconifiedByDefault(false);
    return true;
}

      

Is there a way to achieve this through styling in xml or some other configuration? I prefer to have independent functionality and style.

I tried with searchViewStyle and created a custom style for the SearchView but didn't work:

<style name="AppTheme" parent="Base.AppTheme">
    <item name="searchViewStyle">@style/SearchViewTheme</item>
</style>
<style name="SearchViewTheme" parent="Widget.AppCompat.Light.SearchView">
    <item name="android:maxWidth">10000dp</item>
</style>

      

This is my menu_main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu 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"
    tools:context=".MainActivity">

    <item
        android:id="@+id/action_search"
        android:title="@string/action_search"
        android:orderInCategory="100"
        android:icon="@drawable/ic_action_search"
        app:showAsAction="collapseActionView|ifRoom"
        app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>

      

+3


source to share


1 answer


Try this on the menu:

<?xml version="1.0" encoding="utf-8"?>
<menu 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"
tools:context=".MainActivity">

    <item
        android:id="@+id/action_search"
        android:title="@string/action_search"
        android:orderInCategory="100"
        android:icon="@drawable/ic_action_search"
        app:showAsAction="collapseActionView|ifRoom"
        app:actionLayout="@layout/searchview_layout"
    />

</menu>

      

With this arrangement:



<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.SearchView     
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/search_view_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/SearchViewTheme"/>

      

And this style:

<style name="SearchViewTheme" parent="Widget.AppCompat.Light.SearchView">
    <item name="android:maxWidth">10000dp</item>
    <item name="android:textColorHint">@android:color/white</item>
</style>

      

+4


source







All Articles