Complete search widget

I am targeting API level 8+ and I would like to have full width search at the top of the action bar, extended by default and not resettable. Here's what I did:

I have an ActionBar where I am setting 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's my search.xml inside the 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 leaves extra space between the Back button and the Search widget. Can you help me with this?

search widget left margin

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

+3


source to share





All Articles