Search Widget Full Width

455 Views Asked by At

I'm targetting API level 8+ and I'd like to have a full width search view on top of the action bar, expanded by default and not collapsable. Here's what I did:

I've an ActionBar activity where I set inside onCreate:

final ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);

then onCreateOptionsMenu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.search, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchItem = menu.findItem(R.id.action_search);

    SearchView searchView = (SearchView) MenuItemCompat
            .getActionView(searchItem);
    searchView.setSearchableInfo(searchManager
            .getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);
    MenuItemCompat.expandActionView(searchItem);

    return true;
}

Here my search.xml inside menu folder:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/action_search"
        android:icon="@drawable/search"
        android:title="@string/search_product"
        myapp:actionViewClass="android.support.v7.widget.SearchView"
        myapp:showAsAction="always">
    </item>

</menu>

and my searchable.xml:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:hint="@string/search_product"
    android:label="@string/searchable"
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" >

</searchable>

This way, some extra space remains between back button and search widget. Can you help me whit this?

search widget left margin

P.S. Why search icon is not INSIDE the widget? If I set setIconifiedByDefault to true and then click on the icon, the image remains inside the text space...

0

There are 0 best solutions below