Search code examples
androidactionbarsherlocksearchview

trigger SearchView of xml layout and get the text


I dont know whether i can do like this or not

I have added a Sherlock.widget.SearchView to my xml layout

    <com.actionbarsherlock.widget.SearchView
        android:id="@+id/searchView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="45dp" >
    </com.actionbarsherlock.widget.SearchView>

then m getting xml layouts id to my activiy

    etSearch = (SearchView) findViewById(R.id.searchView1);
         etSearch.setSubmitButtonEnabled(true);

now i want to trigger searchview and get the text entered from searcbar and pass it to another activity.


Solution

  • You must implement the OnQueryTextListener interface:

    //YourActivity

    SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
            public boolean onQueryTextChange(String newText) {
                return true;
            }
    
            public boolean onQueryTextSubmit(String query) {
                // Called when the user submits the query
                Intent intent = new Intent(YourActivity.this, NextActivity.class);
                intent.putExtra("query", query);
                YourActivity.this.startActivity(intent);
                return true;
            }
     };
    
     searchView.setOnQueryTextListener(queryTextListener);
    

    //NextActivity

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String value = extras.getString("query");
    }