Skip to content

Commit

Permalink
use addMenuProvider to add menu
Browse files Browse the repository at this point in the history
  • Loading branch information
sdex committed Nov 21, 2022
1 parent a97587b commit 495e1e1
Showing 1 changed file with 35 additions and 26 deletions.
61 changes: 35 additions & 26 deletions app/src/main/java/com/sdex/activityrunner/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package com.sdex.activityrunner
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.widget.SearchView
import androidx.appcompat.widget.SearchView.*
import androidx.core.view.MenuProvider
import com.google.android.material.behavior.SwipeDismissBehavior
import com.google.android.material.snackbar.BaseTransientBottomBar
import com.google.android.material.snackbar.Snackbar
Expand Down Expand Up @@ -46,6 +48,34 @@ class MainActivity : BaseActivity() {

ApplicationsListJob.enqueueWork(this, Intent())

addMenuProvider(object : MenuProvider {
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
menuInflater.inflate(R.menu.main, menu)
configureSearchView(menu)
}

override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
return when (menuItem.itemId) {
R.id.action_launch_intent -> {
IntentBuilderActivity.start(this@MainActivity, null)
true
}

R.id.action_about -> {
AboutActivity.start(this@MainActivity)
true
}

R.id.action_settings -> {
SettingsActivity.start(this@MainActivity)
true
}

else -> false
}
}
})

adapter = ApplicationsListAdapter(this, appPreferences).apply {
itemClickListener = object : ApplicationsListAdapter.ItemClickListener {
override fun onItemClick(item: ApplicationModel) {
Expand Down Expand Up @@ -89,13 +119,14 @@ class MainActivity : BaseActivity() {
adapter.update()
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.main, menu)
private fun configureSearchView(menu: Menu) {
val searchItem = menu.findItem(R.id.action_search)
val searchView = searchItem.actionView as SearchView
// expand the view to the full width: https://stackoverflow.com/a/34050959/2894324
searchView.maxWidth = Int.MAX_VALUE
searchView.queryHint = getString(R.string.action_search_hint)
searchView.apply {
maxWidth = Int.MAX_VALUE
queryHint = getString(R.string.action_search_hint)
}

val searchQuery = viewModel.searchQuery.value
if (!searchQuery.isNullOrEmpty()) {
Expand Down Expand Up @@ -126,28 +157,6 @@ class MainActivity : BaseActivity() {
return true
}
})
return super.onCreateOptionsMenu(menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_launch_intent -> {
IntentBuilderActivity.start(this, null)
true
}

R.id.action_about -> {
AboutActivity.start(this)
true
}

R.id.action_settings -> {
SettingsActivity.start(this)
true
}

else -> super.onOptionsItemSelected(item)
}
}

companion object {
Expand Down

0 comments on commit 495e1e1

Please sign in to comment.