John Joe
John Joe

Reputation: 12803

Intent to next activity when search widget is clicked

Can someone tell me how to achieve this ?

I wanted to add a search widget in my app bar. When it is clicked, it will intent to B and allow user to type. The related result will displayed below.

I've tried many method, but when I click the search widget, it change to editText, but still remains in the same page. How can I make it intent to another activity when it is change to editText ? Thanks .

Activity A

  @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.create_menu, 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;
        }

@Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case R.id.action_search:
                Intent intent=new Intent(A.this, B.class);
                startActivity(intent);
                return true;

Activity B

  public class SearchResultActivity extends ActionBarActivity {


        TextView text;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_result);
            text=(TextView)findViewById(R.id.textView1);
            handleIntent(getIntent());
        }


 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.create_menu, menu);
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));

        return true;
    }




        @Override
        protected void onNewIntent(Intent intent) {
            setIntent(intent);
            handleIntent(intent);
        }

        private void handleIntent(Intent intent) {

            if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
                String query = intent.getStringExtra(SearchManager.QUERY);
                text.setText(query);
                //use the query to search
            }
        }
    }

This is what I want to achieve.

Activity A

enter image description here

When search icon is clicked, change to editText in Activity B and display the related result below the app bar.

Activity B

enter image description here

Upvotes: 2

Views: 2268

Answers (1)

Harin
Harin

Reputation: 2423

Please follow below steps:

first.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/m1"
    android:title="Search"
    android:showAsAction="always"
    android:icon="@android:drawable/ic_menu_search"/>

MainActivityFirst.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.first,menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId()==R.id.m1){
        Intent i = new Intent(this,MainActivitySecond.class);
        startActivity(i);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

second.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@+id/search"
    android:icon="@android:drawable/ic_menu_search"
    android:title="Search"
    android:showAsAction="always"
    android:actionViewClass="android.widget.SearchView"/>

MainActivitySecond.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.second, menu);
    SearchView searchView =
    (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setIconified(false);
        return super.onCreateOptionsMenu(menu);
    }

searchView.setIconified(false) will show the search view expanded in second activity, here you can play with search view, suggestions etc. For more follow Creating search interface in android

Hope it will solve your issue.

Upvotes: 4

Related Questions