From 308060cea6677669046bf7e654b9c441ee69f2c3 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 12 Aug 2022 16:59:33 +0200 Subject: [PATCH] add back button to search --- .../github/libretube/activities/MainActivity.kt | 17 ++++++++++++++++- app/src/main/res/menu/action_bar.xml | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/github/libretube/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/activities/MainActivity.kt index 724c13462..901945509 100644 --- a/app/src/main/java/com/github/libretube/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/MainActivity.kt @@ -214,7 +214,6 @@ class MainActivity : BaseActivity() { // stuff for the search in the topBar val searchItem = menu.findItem(R.id.action_search) searchView = searchItem.actionView as SearchView - searchView.maxWidth = Integer.MAX_VALUE val searchViewModel = ViewModelProvider(this)[SearchViewModel::class.java] @@ -245,6 +244,22 @@ class MainActivity : BaseActivity() { } }) + searchItem.setOnActionExpandListener( + object : MenuItem.OnActionExpandListener { + override fun onMenuItemActionExpand(p0: MenuItem?): Boolean { + return true + } + + override fun onMenuItemActionCollapse(p0: MenuItem?): Boolean { + val currentFragmentId = navController.currentDestination?.id + if (currentFragmentId == R.id.searchFragment || currentFragmentId == R.id.searchResultFragment) { + onBackPressed() + } + return true + } + } + ) + searchView.setOnCloseListener { if (navController.currentDestination?.id == R.id.searchFragment) { searchViewModel.setQuery(null) diff --git a/app/src/main/res/menu/action_bar.xml b/app/src/main/res/menu/action_bar.xml index 2359a5310..9d47ac612 100644 --- a/app/src/main/res/menu/action_bar.xml +++ b/app/src/main/res/menu/action_bar.xml @@ -8,7 +8,7 @@ android:icon="@drawable/ic_search" android:title="@string/search_hint" app:actionViewClass="androidx.appcompat.widget.SearchView" - app:showAsAction="ifRoom" /> + app:showAsAction="ifRoom|collapseActionView" />