diff --git a/OsmAnd/src/net/osmand/AndroidUtils.java b/OsmAnd/src/net/osmand/AndroidUtils.java index 3c8eed9206..3a91f288ef 100644 --- a/OsmAnd/src/net/osmand/AndroidUtils.java +++ b/OsmAnd/src/net/osmand/AndroidUtils.java @@ -425,6 +425,17 @@ public class AndroidUtils { : ctx.getResources().getColor(R.color.text_color_secondary_light)); } + public static int getPopupMenuWidth(Context ctx, float textSize, List titles) { + int width = 0; + for (String title : titles) { + int titleWidth = getTextWidth(textSize, title); + if (titleWidth > width) { + width = titleWidth; + } + } + return width + dpToPx(ctx, 34); + } + public static int getTextWidth(float textSize, String text) { Paint paint = new Paint(); paint.setTextSize(textSize); diff --git a/OsmAnd/src/net/osmand/plus/SimplePopUpMenuItemAdapter.java b/OsmAnd/src/net/osmand/plus/SimplePopUpMenuItemAdapter.java index 3dadf98f60..406f3d265c 100644 --- a/OsmAnd/src/net/osmand/plus/SimplePopUpMenuItemAdapter.java +++ b/OsmAnd/src/net/osmand/plus/SimplePopUpMenuItemAdapter.java @@ -12,10 +12,12 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import static net.osmand.plus.SimplePopUpMenuItemAdapter.SimplePopUpMenuItem; + import java.util.List; public class SimplePopUpMenuItemAdapter - extends ArrayAdapter { + extends ArrayAdapter { private List items; @@ -35,7 +37,7 @@ public class SimplePopUpMenuItemAdapter public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { LayoutInflater inflater = LayoutInflater.from(getContext()); if (convertView == null) { - convertView = inflater.inflate(R.layout.popup_menu_item, null); + convertView = inflater.inflate(R.layout.popup_menu_item, parent, false); } SimplePopUpMenuItem item = getItem(position); if (item != null) { diff --git a/OsmAnd/src/net/osmand/plus/UiUtilities.java b/OsmAnd/src/net/osmand/plus/UiUtilities.java index d93bdbc6dc..bef961ce16 100644 --- a/OsmAnd/src/net/osmand/plus/UiUtilities.java +++ b/OsmAnd/src/net/osmand/plus/UiUtilities.java @@ -55,10 +55,13 @@ import net.osmand.plus.widgets.TextViewEx; import org.apache.commons.logging.Log; +import java.util.ArrayList; import java.util.List; import gnu.trove.map.hash.TLongObjectHashMap; +import static net.osmand.plus.SimplePopUpMenuItemAdapter.SimplePopUpMenuItem; + public class UiUtilities { private static final Log LOG = PlatformUtil.getLog(UiUtilities.class); @@ -363,15 +366,15 @@ public class UiUtilities { } return screenOrientation; } - + public static void setupSnackbar(Snackbar snackbar, boolean nightMode) { setupSnackbar(snackbar, nightMode, null, null, null, null); } - + public static void setupSnackbar(Snackbar snackbar, boolean nightMode, Integer maxLines) { setupSnackbar(snackbar, nightMode, null, null, null, maxLines); } - + public static void setupSnackbar(Snackbar snackbar, boolean nightMode, @ColorRes Integer backgroundColor, @ColorRes Integer messageColor, @ColorRes Integer actionColor, Integer maxLines) { if (snackbar == null) { @@ -556,7 +559,7 @@ public class UiUtilities { // label behavior slider.setLabelBehavior(Slider.LABEL_GONE); } - + public static void setupDialogButton(boolean nightMode, View buttonView, DialogButtonType buttonType, @StringRes int buttonTextId) { setupDialogButton(nightMode, buttonView, buttonType, buttonView.getContext().getString(buttonTextId)); } @@ -651,23 +654,18 @@ public class UiUtilities { } } - public static ListPopupWindow createListPopupWindow(Context themedCtx, View v, - List items, + public static ListPopupWindow createListPopupWindow(Context themedCtx, + View v, int minWidth, + List items, final AdapterView.OnItemClickListener listener) { - int contentPadding = themedCtx.getResources().getDimensionPixelSize(R.dimen.content_padding); int contentPaddingHalf = themedCtx.getResources().getDimensionPixelSize(R.dimen.content_padding_half); + int defaultListTextSize = themedCtx.getResources().getDimensionPixelSize(R.dimen.default_list_text_size); - Paint paint = new Paint(); - paint.setTextSize(themedCtx.getResources().getDimensionPixelSize(R.dimen.default_list_text_size)); - CharSequence longestTitle = ""; - for (SimplePopUpMenuItemAdapter.SimplePopUpMenuItem item : items) { - if (item.getTitle().length() > longestTitle.length()) { - longestTitle = item.getTitle(); - } + List titles = new ArrayList<>(); + for (SimplePopUpMenuItem item : items) { + titles.add(String.valueOf(item.getTitle())); } - float titleTextWidth = paint.measureText(longestTitle.toString()); - float itemWidth = titleTextWidth + contentPadding; - float minWidth = v.getWidth(); + float itemWidth = AndroidUtils.getPopupMenuWidth(themedCtx, defaultListTextSize, titles); SimplePopUpMenuItemAdapter adapter = new SimplePopUpMenuItemAdapter(themedCtx, R.layout.popup_menu_item, items); diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/TrackDetailsMenu.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/TrackDetailsMenu.java index 39e5dd1bac..4e19df5fcf 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/TrackDetailsMenu.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/TrackDetailsMenu.java @@ -32,7 +32,6 @@ import net.osmand.data.RotatedTileBox; import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; -import net.osmand.plus.SimplePopUpMenuItemAdapter; import net.osmand.plus.UiUtilities; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.helpers.AndroidUiHelper; @@ -48,6 +47,8 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; +import static net.osmand.plus.SimplePopUpMenuItemAdapter.SimplePopUpMenuItem; + public class TrackDetailsMenu { @Nullable @@ -490,6 +491,9 @@ public class TrackDetailsMenu { if (mapActivity == null || gpxItem == null) { return; } + final OsmandApplication app = mapActivity.getMyApplication(); + final UiUtilities ic = app.getUIUtilities(); + final boolean nightMode = app.getDaynightHelper().isNightModeForMapControls(); GPXTrackAnalysis analysis = gpxItem.analysis; if (analysis == null || gpxItem.chartTypes == null) { parentView.setVisibility(View.GONE); @@ -576,9 +580,6 @@ public class TrackDetailsMenu { } }); - final OsmandApplication app = mapActivity.getMyApplication(); - final UiUtilities ic = app.getUIUtilities(); - GpxUiHelper.setupGPXChart(app, chart, 4); List dataSets = new ArrayList<>(); @@ -652,15 +653,15 @@ public class TrackDetailsMenu { yAxis.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - Context themedContext = UiUtilities.getThemedContext(v.getContext(), true); - List items = new ArrayList<>(); + Context themedContext = UiUtilities.getThemedContext(v.getContext(), nightMode); + List items = new ArrayList<>(); for (GPXDataSetType[] types : availableTypes) { - items.add(new SimplePopUpMenuItemAdapter.SimplePopUpMenuItem( + items.add(new SimplePopUpMenuItem( GPXDataSetType.getName(app, types), GPXDataSetType.getImageDrawable(app, types))); } UiUtilities.createListPopupWindow( - themedContext, v, items, new AdapterView.OnItemClickListener() { + themedContext, v, v.getWidth(), items, new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { GpxDisplayItem gpxItem = getGpxItem(); @@ -695,14 +696,14 @@ public class TrackDetailsMenu { xAxis.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - Context themedContext = UiUtilities.getThemedContext(v.getContext(), true); - List items = new ArrayList<>(); + Context themedContext = UiUtilities.getThemedContext(v.getContext(), nightMode); + List items = new ArrayList<>(); for (GPXDataSetAxisType type : GPXDataSetAxisType.values()) { - items.add(new SimplePopUpMenuItemAdapter.SimplePopUpMenuItem( + items.add(new SimplePopUpMenuItem( app.getString(type.getStringId()), type.getImageDrawable(app))); } UiUtilities.createListPopupWindow(themedContext, - v, items, new AdapterView.OnItemClickListener() { + v, v.getWidth(), items, new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { GpxDisplayItem gpxItem = getGpxItem(); diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/TrackDetailsMenuFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/TrackDetailsMenuFragment.java index fad7483291..843992ec9f 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/TrackDetailsMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/TrackDetailsMenuFragment.java @@ -30,6 +30,7 @@ public class TrackDetailsMenuFragment extends BaseOsmAndFragment { private TrackDetailsMenu menu; private View mainView; private boolean paused = true; + private boolean nightMode; @Nullable private MapActivity getMapActivity() { @@ -46,7 +47,7 @@ public class TrackDetailsMenuFragment extends BaseOsmAndFragment { Bundle savedInstanceState) { MapActivity mapActivity = requireMapActivity(); menu = mapActivity.getTrackDetailsMenu(); - boolean nightMode = mapActivity.getMyApplication().getDaynightHelper().isNightModeForMapControls(); + nightMode = mapActivity.getMyApplication().getDaynightHelper().isNightModeForMapControls(); ContextThemeWrapper context = new ContextThemeWrapper(mapActivity, !nightMode ? R.style.OsmandLightTheme : R.style.OsmandDarkTheme); View view = LayoutInflater.from(context).inflate(R.layout.track_details, container, false); @@ -192,7 +193,6 @@ public class TrackDetailsMenuFragment extends BaseOsmAndFragment { if (ctx != null) { boolean portraitMode = AndroidUiHelper.isOrientationPortrait(ctx); boolean landscapeLayout = !portraitMode; - boolean nightMode = ctx.getMyApplication().getDaynightHelper().isNightModeForMapControls(); if (!landscapeLayout) { AndroidUtils.setBackground(ctx, mainView, nightMode, R.drawable.bg_bottom_menu_light, R.drawable.bg_bottom_menu_dark); } else { diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/ShowAlongTheRouteBottomSheet.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/ShowAlongTheRouteBottomSheet.java index 34c655ae0b..7272b7a2ea 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/ShowAlongTheRouteBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/ShowAlongTheRouteBottomSheet.java @@ -81,9 +81,8 @@ public class ShowAlongTheRouteBottomSheet extends MenuBottomSheetDialogFragment } int expandType = args.getInt(EXPAND_TYPE_KEY, -1); - final int themeRes = nightMode ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme; - final View titleView = View.inflate(new ContextThemeWrapper(getContext(), themeRes), - R.layout.bottom_sheet_item_toolbar_title, null); + final View titleView = UiUtilities.getInflater(ctx, nightMode) + .inflate(R.layout.bottom_sheet_item_toolbar_title, null); TextView textView = (TextView) titleView.findViewById(R.id.title); textView.setText(R.string.show_along_the_route); diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java index 0ef39d0299..75664e9f5d 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java @@ -173,6 +173,7 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC private SearchUICore searchUICore; private SearchResultListener defaultResultListener; private String searchQuery; + private boolean nightMode; private LatLon centerLatLon; private net.osmand.Location location = null; @@ -229,6 +230,7 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); app = getMyApplication(); + nightMode = !app.getSettings().isLightContent(); navigationInfo = new NavigationInfo(app); accessibilityAssistant = new AccessibilityAssistant(getActivity()); boolean isLightTheme = app.getSettings().OSMAND_THEME.get() == OsmandSettings.OSMAND_LIGHT_THEME; @@ -242,7 +244,6 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC Bundle savedInstanceState) { final MapActivity mapActivity = getMapActivity(); final View view = inflater.inflate(R.layout.search_dialog_fragment, container, false); - final boolean nightMode = !app.getSettings().isLightContent(); toolbarController = new QuickSearchToolbarController(); toolbarController.setOnBackButtonClickListener(new OnClickListener() {