Skip to content

Commit 26bba01

Browse files
committed
Fix search icon disappearing from toolbar after opening menu
1 parent 7eead93 commit 26bba01

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

Diff for: onebusaway-android/src/main/java/org/onebusaway/android/ui/HomeActivity.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
import androidx.appcompat.app.AlertDialog;
116116
import androidx.appcompat.app.AppCompatActivity;
117117
import androidx.appcompat.widget.AppCompatImageButton;
118+
import androidx.appcompat.widget.SearchView;
118119
import androidx.cardview.widget.CardView;
119120
import androidx.core.content.ContextCompat;
120121
import androidx.fragment.app.FragmentManager;
@@ -145,6 +146,7 @@ public class HomeActivity extends AppCompatActivity
145146
ArrivalsListFragment.Listener, NavigationDrawerCallbacks, WeatherRequestListener , RegionCallback,
146147
ObaRegionsTask.Callback {
147148

149+
private Menu mOptionsMenu;
148150

149151
interface SlidingPanelController {
150152

@@ -830,7 +832,9 @@ private void hideSlidingPanel() {
830832
@Override
831833
public boolean onCreateOptionsMenu(Menu menu) {
832834
getMenuInflater().inflate(R.menu.main_options, menu);
833-
835+
836+
mOptionsMenu = menu;
837+
834838
UIUtils.setupSearch(this, menu);
835839

836840
// Initialize fragment menu visibility here, so we don't have overlap between the various fragments
@@ -1276,6 +1280,16 @@ public void onSortBySelected() {
12761280

12771281
@Override
12781282
public void onBackPressed() {
1283+
// First, check if search view is expanded and collapse it if it is
1284+
MenuItem searchItem = mOptionsMenu != null ? mOptionsMenu.findItem(R.id.action_search) : null;
1285+
if (searchItem != null) {
1286+
SearchView searchView = (SearchView) searchItem.getActionView();
1287+
if (searchView != null && !searchView.isIconified()) {
1288+
searchView.setIconified(true);
1289+
return;
1290+
}
1291+
}
1292+
12791293
// Collapse the panel when the user presses the back button
12801294
if (mSlidingPanel != null) {
12811295
// Collapse the sliding panel if its anchored or expanded

Diff for: onebusaway-android/src/main/java/org/onebusaway/android/ui/MyRecentStopsAndRoutesActivity.java

+7
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,11 @@ private ShortcutInfoCompat getShortcut() {
142142
new Intent(this, MyRecentStopsAndRoutesActivity.class),
143143
R.drawable.ic_history);
144144
}
145+
146+
@Override
147+
public void onBackPressed() {
148+
// Use the same behavior as the toolbar's back arrow (up button)
149+
super.onBackPressed();
150+
NavHelp.goHome(this, false);
151+
}
145152
}

Diff for: onebusaway-android/src/main/java/org/onebusaway/android/util/UIUtils.java

+7
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import android.text.format.DateUtils;
5959
import android.text.method.LinkMovementMethod;
6060
import android.text.style.ClickableSpan;
61+
import android.util.DisplayMetrics;
6162
import android.util.Log;
6263
import android.util.TypedValue;
6364
import android.view.Menu;
@@ -162,6 +163,12 @@ public void onFocusChange(View view, boolean queryTextFocused) {
162163
}
163164
}
164165
});
166+
167+
// Limit the search view width to 70% of screen width to show kebab menu
168+
DisplayMetrics metrics = activity.getResources().getDisplayMetrics();
169+
int screenWidth = metrics.widthPixels;
170+
int searchViewWidth = (int) (screenWidth * 0.7);
171+
searchView.setMaxWidth(searchViewWidth);
165172
}
166173

167174
public static void showProgress(Fragment fragment, boolean visible) {

Diff for: onebusaway-android/src/main/res/menu/main_options.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<item android:id="@+id/action_search"
2121
android:title="@string/map_option_search"
2222
android:icon="@drawable/ic_action_search"
23-
app:showAsAction="collapseActionView|ifRoom"
23+
app:showAsAction="always"
2424
app:actionViewClass="androidx.appcompat.widget.SearchView"/>
2525
<item android:id="@+id/recent_stops_routes"
2626
android:title="@string/my_recent_menu_title"/>

0 commit comments

Comments
 (0)