Bug 1325096 - Fix menu button padding on select devices r?maliu
On some devices, ImageView appears to lose it's padding when calling
setBackground[Drawable](). We therefore need to save and restore the
padding on those devices.
Known affected devices are various Asus Transformer tablets,
and also the Motorola Droid 4, running various Android 4.X versions.
We therefore make this workaround conditional on Android 4 to minimise
the number of devices where this workaround is used. (Having the conditional
should also make it easy to notice when we can remove the workaround, i.e.
whenever Android 5 becomes the baseline.)
MozReview-Commit-ID: B88gWwEx48x
--- a/mobile/android/base/java/org/mozilla/gecko/util/ViewUtil.java
+++ b/mobile/android/base/java/org/mozilla/gecko/util/ViewUtil.java
@@ -22,17 +22,43 @@ import java.util.Locale;
public class ViewUtil {
/**
* Enable a circular touch ripple for a given view. This is intended for borderless views,
* such as (3-dot) menu buttons.
*
* Because of platform limitations a square ripple is used on Android 4.
*/
- public static void enableTouchRipple(View view) {
+ public static void enableTouchRipple(final View view) {
+ // On certain older devices (e.g. ASUS TF200T, Motorola Droid 4), setting a background
+ // drawable results in the padding getting wiped. We work around this by saving the pre-existing
+ // padding, followed by restoring it at the end in view.setPadding().
+
+ // Unfortunately the IDE and compiler aren't clever enough for us to be able to make
+ // these final (and uninitialised). So we just use garbage values instead:
+ int paddingLeft = -1;
+ int paddingTop = -1;
+ int paddingRight = -1;
+ int paddingBottom = -1;
+
+ if (!AppConstants.Versions.feature21Plus) {
+ paddingLeft = view.getPaddingLeft();
+ paddingTop = view.getPaddingTop();
+ paddingRight = view.getPaddingRight();
+ paddingBottom = view.getPaddingBottom();
+ }
+
+ setTouchRipple(view);
+
+ if (!AppConstants.Versions.feature21Plus) {
+ view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
+ }
+ }
+
+ private static void setTouchRipple(final View view) {
final TypedArray backgroundDrawableArray;
if (AppConstants.Versions.feature21Plus) {
backgroundDrawableArray = view.getContext().obtainStyledAttributes(new int[] { R.attr.selectableItemBackgroundBorderless });
} else {
backgroundDrawableArray = view.getContext().obtainStyledAttributes(new int[] { R.attr.selectableItemBackground });
}
// This call is deprecated, but the replacement setBackground(Drawable) isn't available