Frage


Is there any search bar widget available just like android default search bar?. i just want search bar like default one and im going to handle searching functionality. I can do this by using edit text widget but i want to know is there any ready made widget is available?

War es hilfreich?

Andere Tipps

In your onCreate method you can use:

onCreate method:

// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
handleIntent(getIntent());

OnCreateOptionsMenu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.feed, 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()));

    return true;
}

Menu:

<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="br.com.srtorcedor.FeedActivity">

    <item android:id="@+id/action_search"
        android:icon="@drawable/abc_ic_search"
        android:title="@string/search_title"
        android:showAsAction="always"
        android:actionViewClass="android.widget.SearchView"/>

</menu>

Searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_label"
    android:hint="@string/search_hint" >
</searchable>

In the fragment, you need to put the following methods:

private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        doSearch(query);
    }
}


public void onNewIntent(Intent intent) {
    setIntent(intent);
    handleIntent(intent);
}

private void doSearch(String queryStr) {
    Log.i("Your search: ",queryStr);
}

Doing it, you will see the search widget on the action bar. I hope it can work with y'all.

you can the make the edit text acts like a search bar using the TextWatcher class.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top