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
android android-actionbar android-support-library android-search android-searchmanager


source to share


No one has answered this question yet

Check out similar questions:

561
Set width and height of ImageView programmatically?
419
Get screen width and height in Android
369
Percentage Width in RelativeLayout
272
Full screen theme for AppCompat
147
Searching in Android with fragments
21
Search widget in action bar doesn't start my search activity
20
Implementing a widget in an Actionbar app in a ListFragment app
2
Wrong overflow menu layout
0
Finding custom list in android action bar
0
Is it possible to configure EditText (instead of SearchView) to search in Android (i.e. search with the system)?



All Articles
Loading...
X
Show
Funny
Dev
Pics