Eylen Eylen - 1 month ago 13
Android Question

Set SearchView width and hint color in XML

I have added the SearchView functionality to the toolbar and I wanted to make it full width when expanded and also change the hint text color to white instead of black.
Finally I've been able to do it programmatically with this:

@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 any way to achieve this through styling in xml or some other configuration? I prefer to have the functionality and styling independent.

I have tried with searchViewStyle and creating a custom style for SearchView but hasn't worked:

<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>

kNo kNo
Answer

Try this in 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 layout:

<?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>