Saturday, September 8, 2012

Expanded Search View In Android

This example shows how you can create search view onto the action bar and set it tobe expanded always.

Algorithm:
1.)  Create a new project by File-> New -> Android Project name it ExpandedSearchView.

2.)  Create and write following into menu\searchview_in_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/action_search"

         android:title="Search"
         android:icon="@android:drawable/ic_menu_search"
         android:showAsAction="always"
         android:actionViewClass="android.widget.SearchView" />
</menu>

3.)  Write following into main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical">
    <TextView
           android:id="@+id/status_text"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:gravity="center_horizontal"/>
</LinearLayout>

4.)  Run for output.


Steps:
1.) Create a project named ExpandedSearchView and set the information as stated in the image.

Build Target: Android 4.0
Application Name: ExpandedSearchView
Package Name: com. example. ExpandedSearchView
Activity Name: ExpandedSearchView
Min SDK Version: 14


2.) Open ExpandedSearchView.java file and write following code there:

package com.example.ExpandedSearchView;
import java.util.List;
import android.app.Activity;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.SearchView;
import android.widget.TextView;
public class ExpandedSearchView extends Activity implementsSearchView.OnQueryTextListener {
    private SearchView mSearchView;
    private TextView mStatusView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.main);
        mStatusView = (TextView) findViewById(R.id.status_text);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.searchview_in_menu, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        mSearchView = (SearchView) searchItem.getActionView();
        setupSearchView(searchItem);
        return true;
    }
    private void setupSearchView(MenuItem searchItem) {
        if (isAlwaysExpanded()) {
            mSearchView.setIconifiedByDefault(false);
        } else {
            searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
                    | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
        }
        SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
        if (searchManager != null) {
            List<SearchableInfo> searchables =searchManager.getSearchablesInGlobalSearch();
            SearchableInfo info =searchManager.getSearchableInfo(getComponentName());
            for (SearchableInfo inf : searchables) {
                if (inf.getSuggestAuthority() != null
                        && inf.getSuggestAuthority().startsWith("applications")){
                    info = inf;
                }
            }
            mSearchView.setSearchableInfo(info);
        }
        mSearchView.setOnQueryTextListener(this);
    }
    public boolean onQueryTextChange(String newText) {
        mStatusView.setText("Query = " + newText);
        return false;
    }
    public boolean onQueryTextSubmit(String query) {
        mStatusView.setText("Query = " + query + " : submitted");
        return false;
    }
    public boolean onClose() {
        mStatusView.setText("Closed!");
        return false;
    }
    protected boolean isAlwaysExpanded() {
        return true;
    }
}

3.) Compile and build the project.
Output

No comments:

Post a Comment