0

I want to open a new activity whenever the search icon is clicked on the mainactivity. This new activity called SearchableActivity will handle all the relevant search results in the listview. I have followed the steps described in this post.

This is what I currently have:

Main activity display

View after search icon is clicked

View after enter is pressed on text query into the search

I want to open up the SearchableActivity when the user enters into SearchView.

Here is what my code looks like:

AndroidManifest.xml

     <activity
            android:name=".MainActivity"
            android:launchMode="singleTop">
            <!-- meta tag points to the activity which displays the results -->
            <meta-data
                android:name="android.app.default_searchable"
                android:value=".SearchableActivity" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SearchableActivity"
            android:parentActivityName=".MainActivity">
            <!-- Parent activity meta-data to support 4.0 and lower -->
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
            <!-- meta tag and intent filter go into results activity -->
            <meta-data android:name="android.app.searchable"
                android:resource="@xml/searchable" />
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
        </activity>

MainActivity

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.options_menu, menu)
        val manager = getSystemService(SEARCH_SERVICE) as SearchManager
        (menu?.findItem(R.id.search)?.actionView as SearchView).apply {
            setSearchableInfo(manager.getSearchableInfo(ComponentName(this.context, SearchableActivity::class.java)))

        }
        return true
    }

SearchableActivity

class SearchableActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_searchable)
        Toast.makeText(this@SearchableActivity, "SA created", Toast.LENGTH_LONG).show()
        handleIntent(intent)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        if (intent !== null)
            handleIntent(intent)
    }

    private fun handleIntent(intent: Intent) {
        if (Intent.ACTION_SEARCH == intent.action) {
            val query = intent.getStringExtra(SearchManager.QUERY)
            Toast.makeText(this@SearchableActivity, "QUERY: $query", Toast.LENGTH_LONG).show()
        }
    }
}
faizanjehangir
  • 2,771
  • 6
  • 45
  • 83

2 Answers2

0

there is a method that handles option menu item click it is called

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    if(item.itemId == R.id.myItemId){
        startActivity(Intent(applicationContext, searchAcctivity::class.java))
    }
    return super.onOptionsItemSelected(item)
}

this code is written in kotlin, it shouldn't be that different in java just press ctrl + o and it should open a list of override methods search for "onOptionsItemSelected"

MoTahir
  • 863
  • 7
  • 22
0

I'm not sure what you're trying to do but you can simply pass in the Search query to the intent of the activity at its creation.

First, add a companion object to your SearchableActivity :

  companion object {
        private const val EXTRA_QUERY = "SEARCH_QUERY"

        fun newIntent(context: Context?, query: String): Intent {
            return Intent(context, SearchableActivity::class.java)
                .putExtra(EXTRA_QUERY, query)
        }
    }

My understanding is that you want to open a new activity with the search query from MainActivity, next thing is handling the intent:

         private fun handleIntent(intent: Intent) {
                if (Intent.ACTION_SEARCH == intent.action) {
                    val query = intent.getStringExtra(EXTRA_QUERY)
                    Toast.makeText(this@SearchableActivity, "QUERY: $query", Toast.LENGTH_LONG).show()

            SearchableActivity.newIntent(this, query)            
    }
  }

..et voila.

Koch
  • 555
  • 4
  • 15