Change button icon color on tap in transport route context menu

This commit is contained in:
Alexander Sytnyk 2018-01-12 10:47:57 +02:00
parent 1549a60eff
commit 11b14d4e44
3 changed files with 52 additions and 25 deletions

View file

@ -138,17 +138,14 @@ public class AndroidUtils {
public static StateListDrawable createStateListDrawable(Context ctx, boolean night,
@DrawableRes int lightNormal, @DrawableRes int lightPressed,
@DrawableRes int darkNormal, @DrawableRes int darkPressed) {
return createStateListDrawable(night,
ContextCompat.getDrawable(ctx, lightNormal), ContextCompat.getDrawable(ctx, lightPressed),
ContextCompat.getDrawable(ctx, darkNormal), ContextCompat.getDrawable(ctx, darkPressed));
return createStateListDrawable(ContextCompat.getDrawable(ctx, night ? darkNormal : lightNormal),
ContextCompat.getDrawable(ctx, night ? darkPressed : lightPressed));
}
public static StateListDrawable createStateListDrawable(boolean night,
Drawable lightNormal, Drawable lightPressed,
Drawable darkNormal, Drawable darkPressed) {
public static StateListDrawable createStateListDrawable(Drawable normal, Drawable pressed) {
StateListDrawable res = new StateListDrawable();
res.addState(new int[]{android.R.attr.state_pressed}, night ? darkPressed : lightPressed);
res.addState(new int[]{}, night ? darkNormal : lightNormal);
res.addState(new int[]{android.R.attr.state_pressed}, pressed);
res.addState(new int[]{}, normal);
return res;
}

View file

@ -1,7 +1,9 @@
package net.osmand.plus.mapcontextmenu;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.AsyncTask;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
@ -9,6 +11,7 @@ import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import net.osmand.AndroidUtils;
import net.osmand.IndexConstants;
import net.osmand.NativeLibrary.RenderedObject;
import net.osmand.aidl.maplayer.point.AMapPoint;
@ -573,18 +576,39 @@ public abstract class MenuController extends BaseMenuController {
int resId = left ? leftIconId : rightIconId;
if (resId != 0) {
if (needColorizeIcon) {
return getIcon(resId, getColorRes());
return enabled ? getNormalIcon(resId) : getDisabledIcon(resId);
}
return ContextCompat.getDrawable(getMapActivity(), resId);
}
return null;
}
private int getColorRes() {
if (enabled) {
return isLight() ? R.color.map_widget_blue : R.color.osmand_orange;
public void updateStateListDrawableIcon(@DrawableRes int resId, boolean left) {
if (left) {
leftIcon = enabled ? getStateListDrawable(resId) : null;
leftIconId = enabled ? 0 : resId;
} else {
rightIcon = enabled ? getStateListDrawable(resId) : null;
rightIconId = enabled ? 0 : resId;
}
return isLight() ? R.color.ctx_menu_controller_disabled_text_color_light : R.color.ctx_menu_controller_disabled_text_color_dark;
}
private Drawable getDisabledIcon(@DrawableRes int iconResId) {
return getIcon(iconResId, isLight() ? R.color.ctx_menu_controller_disabled_text_color_light
: R.color.ctx_menu_controller_disabled_text_color_dark);
}
private Drawable getNormalIcon(@DrawableRes int iconResId) {
return getIcon(iconResId, isLight() ? R.color.map_widget_blue : R.color.osmand_orange);
}
private Drawable getPressedIcon(@DrawableRes int iconResId) {
return getIcon(iconResId, isLight() ? R.color.ctx_menu_controller_button_text_color_light_p
: R.color.ctx_menu_controller_button_text_color_dark_p);
}
private StateListDrawable getStateListDrawable(@DrawableRes int iconResId) {
return AndroidUtils.createStateListDrawable(getNormalIcon(iconResId), getPressedIcon(iconResId));
}
public abstract void buttonPressed();

View file

@ -58,7 +58,7 @@ public class TransportRouteController extends MenuController {
}
};
leftTitleButtonController.caption = mapActivity.getString(R.string.shared_string_previous);
leftTitleButtonController.leftIconId = R.drawable.ic_arrow_back;
updateLeftTitleButtonIcon();
rightTitleButtonController = new TitleButtonController() {
@Override
@ -70,7 +70,7 @@ public class TransportRouteController extends MenuController {
}
};
rightTitleButtonController.caption = mapActivity.getString(R.string.shared_string_next);
rightTitleButtonController.rightIconId = R.drawable.ic_arrow_forward;
updateRightTitleButtonIcon();
}
@Override
@ -144,6 +144,14 @@ public class TransportRouteController extends MenuController {
resetRoute();
}
private void updateLeftTitleButtonIcon() {
leftTitleButtonController.updateStateListDrawableIcon(R.drawable.ic_arrow_back, true);
}
private void updateRightTitleButtonIcon() {
rightTitleButtonController.updateStateListDrawableIcon(R.drawable.ic_arrow_forward, false);
}
public void onAcquireNewController(PointDescription pointDescription, Object object) {
if (object instanceof TransportRouteStop) {
resetRoute();
@ -151,19 +159,17 @@ public class TransportRouteController extends MenuController {
}
private void updateControllers() {
boolean previousStopEnabled = false;
final int previousStop = getPreviousStop();
if (previousStop != -1) {
previousStopEnabled = true;
boolean previousStopEnabled = getPreviousStop() != -1;
if (leftTitleButtonController.enabled != previousStopEnabled) {
leftTitleButtonController.enabled = previousStopEnabled;
updateLeftTitleButtonIcon();
}
leftTitleButtonController.enabled = previousStopEnabled;
boolean nextStopEnabled = false;
final int nextStop = getNextStop();
if (nextStop != -1) {
nextStopEnabled = true;
boolean nextStopEnabled = getNextStop() != -1;
if (rightTitleButtonController.enabled != nextStopEnabled) {
rightTitleButtonController.enabled = nextStopEnabled;
updateRightTitleButtonIcon();
}
rightTitleButtonController.enabled = nextStopEnabled;
}
private void showTransportStop(TransportStop stop) {