Skip to content

Commit 27abfdb

Browse files
imhappiraajkumars
authored andcommittedJun 27, 2023
[Badge] Deprecate bottom badge gravities and update docs and usages in catalog
PiperOrigin-RevId: 543527285
1 parent ec509cc commit 27abfdb

File tree

8 files changed

+29
-25
lines changed

8 files changed

+29
-25
lines changed
 

‎catalog/java/io/material/catalog/bottomnav/BottomNavigationDemoFragment.java

-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ public abstract class BottomNavigationDemoFragment extends DemoFragment {
5050
new int[] {
5151
BadgeDrawable.TOP_END,
5252
BadgeDrawable.TOP_START,
53-
BadgeDrawable.BOTTOM_END,
54-
BadgeDrawable.BOTTOM_START
5553
};
5654

5755
private int numVisibleChildren = 3;

‎catalog/java/io/material/catalog/bottomnav/res/values/arrays.xml

-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,5 @@
1919
<string-array name="cat_bottom_nav_badge_gravity_titles">
2020
<item>@string/cat_bottom_nav_badge_gravity_top_end</item>
2121
<item>@string/cat_bottom_nav_badge_gravity_top_start</item>
22-
<item>@string/cat_bottom_nav_badge_gravity_bottom_end</item>
23-
<item>@string/cat_bottom_nav_badge_gravity_bottom_start</item>
2422
</string-array>
2523
</resources>

‎catalog/java/io/material/catalog/bottomnav/res/values/strings.xml

-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@
5353

5454
<string name="cat_bottom_nav_badge_gravity_top_end">Top end</string>
5555
<string name="cat_bottom_nav_badge_gravity_top_start">Top start</string>
56-
<string name="cat_bottom_nav_badge_gravity_bottom_end">Bottom end</string>
57-
<string name="cat_bottom_nav_badge_gravity_bottom_start">Bottom start</string>
5856

5957
<string name="cat_bottom_nav_badge_gravity_spinner_label">Badge gravity</string>
6058
<string name="cat_bottom_nav_label_active_bold" translatable="false">Bold active label</string>

‎catalog/java/io/material/catalog/navigationrail/NavigationRailDemoFragment.java

-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ public class NavigationRailDemoFragment extends DemoFragment {
5050
new int[] {
5151
BadgeDrawable.TOP_END,
5252
BadgeDrawable.TOP_START,
53-
BadgeDrawable.BOTTOM_END,
54-
BadgeDrawable.BOTTOM_START
5553
};
5654

5755
private int numVisibleChildren = 3;

‎catalog/java/io/material/catalog/navigationrail/res/values/arrays.xml

-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,5 @@
1919
<string-array name="cat_navigation_rail_badge_gravity_titles">
2020
<item>@string/cat_navigation_rail_badge_gravity_top_end</item>
2121
<item>@string/cat_navigation_rail_badge_gravity_top_start</item>
22-
<item>@string/cat_navigation_rail_badge_gravity_bottom_end</item>
23-
<item>@string/cat_navigation_rail_badge_gravity_bottom_start</item>
2422
</string-array>
2523
</resources>

‎catalog/java/io/material/catalog/navigationrail/res/values/strings.xml

-4
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@
8989
description="Label for the option that will cause badges to be positioned at the end, above the icon. [CHAR_LIMIT=50]">Top end</string>
9090
<string name="cat_navigation_rail_badge_gravity_top_start"
9191
description="Label for the option that will cause badges to be positioned at the start, above the icon. [CHAR_LIMIT=50]">Top start</string>
92-
<string name="cat_navigation_rail_badge_gravity_bottom_end"
93-
description="Label for the option that will cause badges to be positioned at the end, below the icon. [CHAR_LIMIT=50]">Bottom end</string>
94-
<string name="cat_navigation_rail_badge_gravity_bottom_start"
95-
description="Label for the option that will cause badges to be positioned at starting, below the icon. [CHAR_LIMIT=50]">Bottom start</string>
9692
<string name="cat_navigation_rail_label_active_bold"
9793
description="Label for controlling whether the active label is bold. [CHAR_LIMIT=50]" translatable="false">Bold active label</string>
9894

‎docs/components/BadgeDrawable.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ can specify a `FrameLayout` to display the badge instead.
7676

7777
### `BadgeDrawable` Gravity Modes
7878

79-
`BadgeDrawable` provides four pre-packaged gravity modes that control how the
80-
badge aligns with its anchor view. By default (`TOP_END`) badge aligns with the
81-
top and end edges of the anchor (with some offsets). The other options are
82-
`TOP_START`, `BOTTOM_START` and `BOTTOM_END`.
79+
`BadgeDrawable` offers two gravity modes to control how the badge aligns with
80+
its anchor view. By default, (`TOP_END`) badge aligns with the top and end edges
81+
of the anchor (with some offsets). Alternatively, you can use `TOP_START` to
82+
align the badge with the top and start edges of the anchor. Note that
83+
`BOTTOM_START` and `BOTTOM_END` are deprecated and not recommended for use.
8384

8485
### `BadgeDrawable` center offsets
8586

‎lib/java/com/google/android/material/badge/BadgeDrawable.java

+24-7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import android.graphics.Rect;
3131
import android.graphics.drawable.Drawable;
3232
import android.text.TextUtils;
33+
import android.util.Log;
3334
import android.view.Gravity;
3435
import android.view.View;
3536
import android.view.ViewGroup;
@@ -116,15 +117,17 @@
116117
* </pre>
117118
*
118119
* <p>By default, {@code BadgeDrawable} is aligned to the top and end edges of its anchor view (with
119-
* some offsets). Call {@link #setBadgeGravity(int)} to change it to one of the other supported
120-
* modes. To adjust the badge's offsets w.r.t. the anchor's center, use {@link
120+
* some offsets). Call {@link #setBadgeGravity(int)} to change it to {@link #TOP_START}, the other
121+
* supported mode. To adjust the badge's offsets w.r.t. the anchor's center, use {@link
121122
* BadgeDrawable#setHorizontalOffset(int)}, {@link BadgeDrawable#setVerticalOffset(int)}
122123
*
123124
* <p>Note: This is still under development and may not support the full range of customization
124125
* Material Android components generally support (e.g. themed attributes).
125126
*/
126127
public class BadgeDrawable extends Drawable implements TextDrawableDelegate {
127128

129+
private static final String TAG = "Badge";
130+
128131
/** Position the badge can be set to. */
129132
@IntDef({
130133
TOP_END,
@@ -141,11 +144,21 @@ public class BadgeDrawable extends Drawable implements TextDrawableDelegate {
141144
/** The badge is positioned along the top and start edges of its anchor view */
142145
public static final int TOP_START = Gravity.TOP | Gravity.START;
143146

144-
/** The badge is positioned along the bottom and end edges of its anchor view */
145-
public static final int BOTTOM_END = Gravity.BOTTOM | Gravity.END;
147+
/**
148+
* The badge is positioned along the bottom and end edges of its anchor view
149+
*
150+
* @deprecated Bottom badge gravities are deprecated in favor of top gravities; use {@link
151+
* #TOP_START} or {@link #TOP_END} instead.
152+
*/
153+
@Deprecated public static final int BOTTOM_END = Gravity.BOTTOM | Gravity.END;
146154

147-
/** The badge is positioned along the bottom and start edges of its anchor view */
148-
public static final int BOTTOM_START = Gravity.BOTTOM | Gravity.START;
155+
/**
156+
* The badge is positioned along the bottom and start edges of its anchor view
157+
*
158+
* @deprecated Bottom badge gravities are deprecated in favor of top gravities; use {@link
159+
* #TOP_START} or {@link #TOP_END} instead.
160+
*/
161+
@Deprecated public static final int BOTTOM_START = Gravity.BOTTOM | Gravity.START;
149162

150163
@StyleRes private static final int DEFAULT_STYLE = R.style.Widget_MaterialComponents_Badge;
151164
@AttrRes private static final int DEFAULT_THEME_ATTR = R.attr.badgeStyle;
@@ -664,9 +677,13 @@ public int getBadgeGravity() {
664677
/**
665678
* Sets this badge's gravity with respect to its anchor view.
666679
*
667-
* @param gravity Constant representing one of 4 possible {@link BadgeGravity} values.
680+
* @param gravity Constant representing one of the possible {@link BadgeGravity} values. There are
681+
* two recommended gravities: {@link #TOP_START} and {@link #TOP_END}.
668682
*/
669683
public void setBadgeGravity(@BadgeGravity int gravity) {
684+
if (gravity == BOTTOM_START || gravity == BOTTOM_END) {
685+
Log.w(TAG, "Bottom badge gravities are deprecated; please use a top gravity instead.");
686+
}
670687
if (state.getBadgeGravity() != gravity) {
671688
state.setBadgeGravity(gravity);
672689
onBadgeGravityUpdated();

0 commit comments

Comments
 (0)
Please sign in to comment.