From 679be2d6c423d1d2c8f7d94901ddcffcb9510619 Mon Sep 17 00:00:00 2001 From: Adam Powell Date: Mon, 28 Mar 2016 09:38:41 -0700 Subject: [PATCH] Add contentInsetStartWithNavigation/EndWithActions Allow developers to set different content insets on toolbars and action bars to be used when navigation buttons or menu actions are present. Set the default values for these according to the material spec. Bug 19317855 Change-Id: I13e04e1f19f0982bf551a3027eb70904d6b4674c --- api/current.txt | 10 + api/system-current.txt | 10 + api/test-current.txt | 10 + core/java/android/widget/Toolbar.java | 191 ++++++++++++++++-- .../res/values-sw600dp/dimens_material.xml | 2 + core/res/res/values/attrs.xml | 8 + core/res/res/values/dimens_material.xml | 2 + core/res/res/values/public.xml | 2 + core/res/res/values/styles.xml | 1 + core/res/res/values/styles_material.xml | 1 + 10 files changed, 225 insertions(+), 12 deletions(-) diff --git a/api/current.txt b/api/current.txt index c4160443380f0..dd70a3298aeb7 100644 --- a/api/current.txt +++ b/api/current.txt @@ -416,9 +416,11 @@ package android { field public static final int contentAuthority = 16843408; // 0x1010290 field public static final int contentDescription = 16843379; // 0x1010273 field public static final int contentInsetEnd = 16843860; // 0x1010454 + field public static final int contentInsetEndWithActions = 16844070; // 0x1010526 field public static final int contentInsetLeft = 16843861; // 0x1010455 field public static final int contentInsetRight = 16843862; // 0x1010456 field public static final int contentInsetStart = 16843859; // 0x1010453 + field public static final int contentInsetStartWithNavigation = 16844069; // 0x1010525 field public static final int contextClickable = 16844007; // 0x10104e7 field public static final int contextPopupMenuStyle = 16844034; // 0x1010502 field public static final int controlX1 = 16843772; // 0x10103fc @@ -48290,9 +48292,15 @@ package android.widget { method public void collapseActionView(); method public void dismissPopupMenus(); method public int getContentInsetEnd(); + method public int getContentInsetEndWithActions(); method public int getContentInsetLeft(); method public int getContentInsetRight(); method public int getContentInsetStart(); + method public int getContentInsetStartWithNavigation(); + method public int getCurrentContentInsetEnd(); + method public int getCurrentContentInsetLeft(); + method public int getCurrentContentInsetRight(); + method public int getCurrentContentInsetStart(); method public android.graphics.drawable.Drawable getLogo(); method public java.lang.CharSequence getLogoDescription(); method public android.view.Menu getMenu(); @@ -48311,6 +48319,8 @@ package android.widget { method public void inflateMenu(int); method public boolean isOverflowMenuShowing(); method protected void onLayout(boolean, int, int, int, int); + method public void setContentInsetEndWithActions(int); + method public void setContentInsetStartWithNavigation(int); method public void setContentInsetsAbsolute(int, int); method public void setContentInsetsRelative(int, int); method public void setLogo(int); diff --git a/api/system-current.txt b/api/system-current.txt index e372d98a92a17..0fe632d27d88a 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -511,9 +511,11 @@ package android { field public static final int contentAuthority = 16843408; // 0x1010290 field public static final int contentDescription = 16843379; // 0x1010273 field public static final int contentInsetEnd = 16843860; // 0x1010454 + field public static final int contentInsetEndWithActions = 16844070; // 0x1010526 field public static final int contentInsetLeft = 16843861; // 0x1010455 field public static final int contentInsetRight = 16843862; // 0x1010456 field public static final int contentInsetStart = 16843859; // 0x1010453 + field public static final int contentInsetStartWithNavigation = 16844069; // 0x1010525 field public static final int contextClickable = 16844007; // 0x10104e7 field public static final int contextPopupMenuStyle = 16844034; // 0x1010502 field public static final int controlX1 = 16843772; // 0x10103fc @@ -51354,9 +51356,15 @@ package android.widget { method public void collapseActionView(); method public void dismissPopupMenus(); method public int getContentInsetEnd(); + method public int getContentInsetEndWithActions(); method public int getContentInsetLeft(); method public int getContentInsetRight(); method public int getContentInsetStart(); + method public int getContentInsetStartWithNavigation(); + method public int getCurrentContentInsetEnd(); + method public int getCurrentContentInsetLeft(); + method public int getCurrentContentInsetRight(); + method public int getCurrentContentInsetStart(); method public android.graphics.drawable.Drawable getLogo(); method public java.lang.CharSequence getLogoDescription(); method public android.view.Menu getMenu(); @@ -51375,6 +51383,8 @@ package android.widget { method public void inflateMenu(int); method public boolean isOverflowMenuShowing(); method protected void onLayout(boolean, int, int, int, int); + method public void setContentInsetEndWithActions(int); + method public void setContentInsetStartWithNavigation(int); method public void setContentInsetsAbsolute(int, int); method public void setContentInsetsRelative(int, int); method public void setLogo(int); diff --git a/api/test-current.txt b/api/test-current.txt index e4153f126cb88..d59fa272088de 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -416,9 +416,11 @@ package android { field public static final int contentAuthority = 16843408; // 0x1010290 field public static final int contentDescription = 16843379; // 0x1010273 field public static final int contentInsetEnd = 16843860; // 0x1010454 + field public static final int contentInsetEndWithActions = 16844070; // 0x1010526 field public static final int contentInsetLeft = 16843861; // 0x1010455 field public static final int contentInsetRight = 16843862; // 0x1010456 field public static final int contentInsetStart = 16843859; // 0x1010453 + field public static final int contentInsetStartWithNavigation = 16844069; // 0x1010525 field public static final int contextClickable = 16844007; // 0x10104e7 field public static final int contextPopupMenuStyle = 16844034; // 0x1010502 field public static final int controlX1 = 16843772; // 0x10103fc @@ -48364,9 +48366,15 @@ package android.widget { method public void collapseActionView(); method public void dismissPopupMenus(); method public int getContentInsetEnd(); + method public int getContentInsetEndWithActions(); method public int getContentInsetLeft(); method public int getContentInsetRight(); method public int getContentInsetStart(); + method public int getContentInsetStartWithNavigation(); + method public int getCurrentContentInsetEnd(); + method public int getCurrentContentInsetLeft(); + method public int getCurrentContentInsetRight(); + method public int getCurrentContentInsetStart(); method public android.graphics.drawable.Drawable getLogo(); method public java.lang.CharSequence getLogoDescription(); method public android.view.Menu getMenu(); @@ -48385,6 +48393,8 @@ package android.widget { method public void inflateMenu(int); method public boolean isOverflowMenuShowing(); method protected void onLayout(boolean, int, int, int, int); + method public void setContentInsetEndWithActions(int); + method public void setContentInsetStartWithNavigation(int); method public void setContentInsetsAbsolute(int, int); method public void setContentInsetsRelative(int, int); method public void setLogo(int); diff --git a/core/java/android/widget/Toolbar.java b/core/java/android/widget/Toolbar.java index 06daf61937c1f..5b0a90adde33e 100644 --- a/core/java/android/widget/Toolbar.java +++ b/core/java/android/widget/Toolbar.java @@ -106,6 +106,8 @@ import java.util.List; * @attr ref android.R.styleable#Toolbar_contentInsetLeft * @attr ref android.R.styleable#Toolbar_contentInsetRight * @attr ref android.R.styleable#Toolbar_contentInsetStart + * @attr ref android.R.styleable#Toolbar_contentInsetStartWithNavigation + * @attr ref android.R.styleable#Toolbar_contentInsetEndWithActions * @attr ref android.R.styleable#Toolbar_gravity * @attr ref android.R.styleable#Toolbar_logo * @attr ref android.R.styleable#Toolbar_logoDescription @@ -159,6 +161,8 @@ public class Toolbar extends ViewGroup { private int mTitleMarginBottom; private final RtlSpacingHelper mContentInsets = new RtlSpacingHelper(); + private int mContentInsetStartWithNavigation; + private int mContentInsetEndWithActions; private int mGravity = Gravity.START | Gravity.CENTER_VERTICAL; @@ -272,6 +276,11 @@ public class Toolbar extends ViewGroup { mContentInsets.setRelative(contentInsetStart, contentInsetEnd); } + mContentInsetStartWithNavigation = a.getDimensionPixelOffset( + R.styleable.Toolbar_contentInsetStartWithNavigation, RtlSpacingHelper.UNDEFINED); + mContentInsetEndWithActions = a.getDimensionPixelOffset( + R.styleable.Toolbar_contentInsetEndWithActions, RtlSpacingHelper.UNDEFINED); + mCollapseIcon = a.getDrawable(R.styleable.Toolbar_collapseIcon); mCollapseDescription = a.getText(R.styleable.Toolbar_collapseContentDescription); @@ -1055,7 +1064,7 @@ public class Toolbar extends ViewGroup { } /** - * Set the content insets for this toolbar relative to layout direction. + * Sets the content insets for this toolbar relative to layout direction. * *

The content inset affects the valid area for Toolbar content other than * the navigation button and menu. Insets define the minimum margin for these components @@ -1069,13 +1078,15 @@ public class Toolbar extends ViewGroup { * @see #getContentInsetEnd() * @see #getContentInsetLeft() * @see #getContentInsetRight() + * @attr ref android.R.styleable#Toolbar_contentInsetEnd + * @attr ref android.R.styleable#Toolbar_contentInsetStart */ public void setContentInsetsRelative(int contentInsetStart, int contentInsetEnd) { mContentInsets.setRelative(contentInsetStart, contentInsetEnd); } /** - * Get the starting content inset for this toolbar. + * Gets the starting content inset for this toolbar. * *

The content inset affects the valid area for Toolbar content other than * the navigation button and menu. Insets define the minimum margin for these components @@ -1088,13 +1099,14 @@ public class Toolbar extends ViewGroup { * @see #getContentInsetEnd() * @see #getContentInsetLeft() * @see #getContentInsetRight() + * @attr ref android.R.styleable#Toolbar_contentInsetStart */ public int getContentInsetStart() { return mContentInsets.getStart(); } /** - * Get the ending content inset for this toolbar. + * Gets the ending content inset for this toolbar. * *

The content inset affects the valid area for Toolbar content other than * the navigation button and menu. Insets define the minimum margin for these components @@ -1107,13 +1119,14 @@ public class Toolbar extends ViewGroup { * @see #getContentInsetStart() * @see #getContentInsetLeft() * @see #getContentInsetRight() + * @attr ref android.R.styleable#Toolbar_contentInsetEnd */ public int getContentInsetEnd() { return mContentInsets.getEnd(); } /** - * Set the content insets for this toolbar. + * Sets the content insets for this toolbar. * *

The content inset affects the valid area for Toolbar content other than * the navigation button and menu. Insets define the minimum margin for these components @@ -1127,13 +1140,15 @@ public class Toolbar extends ViewGroup { * @see #getContentInsetEnd() * @see #getContentInsetLeft() * @see #getContentInsetRight() + * @attr ref android.R.styleable#Toolbar_contentInsetLeft + * @attr ref android.R.styleable#Toolbar_contentInsetRight */ public void setContentInsetsAbsolute(int contentInsetLeft, int contentInsetRight) { mContentInsets.setAbsolute(contentInsetLeft, contentInsetRight); } /** - * Get the left content inset for this toolbar. + * Gets the left content inset for this toolbar. * *

The content inset affects the valid area for Toolbar content other than * the navigation button and menu. Insets define the minimum margin for these components @@ -1146,13 +1161,14 @@ public class Toolbar extends ViewGroup { * @see #getContentInsetStart() * @see #getContentInsetEnd() * @see #getContentInsetRight() + * @attr ref android.R.styleable#Toolbar_contentInsetLeft */ public int getContentInsetLeft() { return mContentInsets.getLeft(); } /** - * Get the right content inset for this toolbar. + * Gets the right content inset for this toolbar. * *

The content inset affects the valid area for Toolbar content other than * the navigation button and menu. Insets define the minimum margin for these components @@ -1165,11 +1181,160 @@ public class Toolbar extends ViewGroup { * @see #getContentInsetStart() * @see #getContentInsetEnd() * @see #getContentInsetLeft() + * @attr ref android.R.styleable#Toolbar_contentInsetRight */ public int getContentInsetRight() { return mContentInsets.getRight(); } + /** + * Gets the start content inset to use when a navigation button is present. + * + *

Different content insets are often called for when additional buttons are present + * in the toolbar, as well as at different toolbar sizes. The larger value of + * {@link #getContentInsetStart()} and this value will be used during layout.

+ * + * @return the start content inset used when a navigation icon has been set in pixels + * + * @see #setContentInsetStartWithNavigation(int) + * @attr ref android.R.styleable#Toolbar_contentInsetStartWithNavigation + */ + public int getContentInsetStartWithNavigation() { + return mContentInsetStartWithNavigation != RtlSpacingHelper.UNDEFINED + ? mContentInsetStartWithNavigation + : getContentInsetStart(); + } + + /** + * Sets the start content inset to use when a navigation button is present. + * + *

Different content insets are often called for when additional buttons are present + * in the toolbar, as well as at different toolbar sizes. The larger value of + * {@link #getContentInsetStart()} and this value will be used during layout.

+ * + * @param insetStartWithNavigation the inset to use when a navigation icon has been set + * in pixels + * + * @see #getContentInsetStartWithNavigation() + * @attr ref android.R.styleable#Toolbar_contentInsetStartWithNavigation + */ + public void setContentInsetStartWithNavigation(int insetStartWithNavigation) { + if (insetStartWithNavigation < 0) { + insetStartWithNavigation = RtlSpacingHelper.UNDEFINED; + } + if (insetStartWithNavigation != mContentInsetStartWithNavigation) { + mContentInsetStartWithNavigation = insetStartWithNavigation; + if (getNavigationIcon() != null) { + requestLayout(); + } + } + } + + /** + * Gets the end content inset to use when action buttons are present. + * + *

Different content insets are often called for when additional buttons are present + * in the toolbar, as well as at different toolbar sizes. The larger value of + * {@link #getContentInsetEnd()} and this value will be used during layout.

+ * + * @return the end content inset used when a menu has been set in pixels + * + * @see #setContentInsetEndWithActions(int) + * @attr ref android.R.styleable#Toolbar_contentInsetEndWithActions + */ + public int getContentInsetEndWithActions() { + return mContentInsetEndWithActions != RtlSpacingHelper.UNDEFINED + ? mContentInsetEndWithActions + : getContentInsetEnd(); + } + + /** + * Sets the start content inset to use when action buttons are present. + * + *

Different content insets are often called for when additional buttons are present + * in the toolbar, as well as at different toolbar sizes. The larger value of + * {@link #getContentInsetEnd()} and this value will be used during layout.

+ * + * @param insetEndWithActions the inset to use when a menu has been set in pixels + * + * @see #setContentInsetEndWithActions(int) + * @attr ref android.R.styleable#Toolbar_contentInsetEndWithActions + */ + public void setContentInsetEndWithActions(int insetEndWithActions) { + if (insetEndWithActions < 0) { + insetEndWithActions = RtlSpacingHelper.UNDEFINED; + } + if (insetEndWithActions != mContentInsetEndWithActions) { + mContentInsetEndWithActions = insetEndWithActions; + if (getNavigationIcon() != null) { + requestLayout(); + } + } + } + + /** + * Gets the content inset that will be used on the starting side of the bar in the current + * toolbar configuration. + * + * @return the current content inset start in pixels + * + * @see #getContentInsetStartWithNavigation() + */ + public int getCurrentContentInsetStart() { + return getNavigationIcon() != null + ? Math.max(getContentInsetStart(), Math.max(mContentInsetStartWithNavigation, 0)) + : getContentInsetStart(); + } + + /** + * Gets the content inset that will be used on the ending side of the bar in the current + * toolbar configuration. + * + * @return the current content inset end in pixels + * + * @see #getContentInsetEndWithActions() + */ + public int getCurrentContentInsetEnd() { + boolean hasActions = false; + if (mMenuView != null) { + final MenuBuilder mb = mMenuView.peekMenu(); + hasActions = mb != null && mb.hasVisibleItems(); + } + return hasActions + ? Math.max(getContentInsetEnd(), Math.max(mContentInsetEndWithActions, 0)) + : getContentInsetEnd(); + } + + /** + * Gets the content inset that will be used on the left side of the bar in the current + * toolbar configuration. + * + * @return the current content inset left in pixels + * + * @see #getContentInsetStartWithNavigation() + * @see #getContentInsetEndWithActions() + */ + public int getCurrentContentInsetLeft() { + return isLayoutRtl() + ? getCurrentContentInsetEnd() + : getCurrentContentInsetStart(); + } + + /** + * Gets the content inset that will be used on the right side of the bar in the current + * toolbar configuration. + * + * @return the current content inset right in pixels + * + * @see #getContentInsetStartWithNavigation() + * @see #getContentInsetEndWithActions() + */ + public int getCurrentContentInsetRight() { + return isLayoutRtl() + ? getCurrentContentInsetStart() + : getCurrentContentInsetEnd(); + } + private void ensureNavButtonView() { if (mNavButtonView == null) { mNavButtonView = new ImageButton(getContext(), null, 0, mNavButtonStyle); @@ -1406,7 +1571,7 @@ public class Toolbar extends ViewGroup { childState = combineMeasuredStates(childState, mCollapseButtonView.getMeasuredState()); } - final int contentInsetStart = getContentInsetStart(); + final int contentInsetStart = getCurrentContentInsetStart(); width += Math.max(contentInsetStart, navWidth); collapsingMargins[marginStartIndex] = Math.max(0, contentInsetStart - navWidth); @@ -1420,7 +1585,7 @@ public class Toolbar extends ViewGroup { childState = combineMeasuredStates(childState, mMenuView.getMeasuredState()); } - final int contentInsetEnd = getContentInsetEnd(); + final int contentInsetEnd = getCurrentContentInsetEnd(); width += Math.max(contentInsetEnd, menuWidth); collapsingMargins[marginEndIndex] = Math.max(0, contentInsetEnd - menuWidth); @@ -1543,10 +1708,12 @@ public class Toolbar extends ViewGroup { } } - collapsingMargins[0] = Math.max(0, getContentInsetLeft() - left); - collapsingMargins[1] = Math.max(0, getContentInsetRight() - (width - paddingRight - right)); - left = Math.max(left, getContentInsetLeft()); - right = Math.min(right, width - paddingRight - getContentInsetRight()); + final int contentInsetLeft = getCurrentContentInsetLeft(); + final int contentInsetRight = getCurrentContentInsetRight(); + collapsingMargins[0] = Math.max(0, contentInsetLeft - left); + collapsingMargins[1] = Math.max(0, contentInsetRight - (width - paddingRight - right)); + left = Math.max(left, contentInsetLeft); + right = Math.min(right, width - paddingRight - contentInsetRight); if (shouldLayout(mExpandedActionView)) { if (isRtl) { diff --git a/core/res/res/values-sw600dp/dimens_material.xml b/core/res/res/values-sw600dp/dimens_material.xml index 3bbb352bca8e6..1ec5c0fa5cb85 100644 --- a/core/res/res/values-sw600dp/dimens_material.xml +++ b/core/res/res/values-sw600dp/dimens_material.xml @@ -23,6 +23,8 @@ 64dp 24dp + + 80dp 8dp diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml index 00eb81a8ed987..d7381abed1fbf 100644 --- a/core/res/res/values/attrs.xml +++ b/core/res/res/values/attrs.xml @@ -7644,6 +7644,12 @@ i + + + + 16dp + + 72dp 16dp diff --git a/core/res/res/values/public.xml b/core/res/res/values/public.xml index ac29f92fe5d3c..0839187c48d1b 100644 --- a/core/res/res/values/public.xml +++ b/core/res/res/values/public.xml @@ -2711,6 +2711,8 @@ + + diff --git a/core/res/res/values/styles.xml b/core/res/res/values/styles.xml index 86b9f1d1b7d5c..790dcfa3f1270 100644 --- a/core/res/res/values/styles.xml +++ b/core/res/res/values/styles.xml @@ -1234,6 +1234,7 @@ please see styles_device_defaults.xml. ?attr/homeAsUpIndicator @string/toolbar_collapse_description 16dp + @dimen/action_bar_content_inset_with_nav true diff --git a/core/res/res/values/styles_material.xml b/core/res/res/values/styles_material.xml index 6d4936c14f95b..2420c1a96a6cb 100644 --- a/core/res/res/values/styles_material.xml +++ b/core/res/res/values/styles_material.xml @@ -944,6 +944,7 @@ please see styles_device_defaults.xml. @layout/action_bar_home_material center_vertical @dimen/action_bar_content_inset_material + @dimen/action_bar_content_inset_with_nav @dimen/action_bar_content_inset_material @dimen/action_bar_elevation_material ?attr/actionBarPopupTheme