Skip to content

Commit 111cd00

Browse files
wcshidsn5ft
authored andcommitted
Update TabLayout demos in MDC catalog to include badging.
PiperOrigin-RevId: 250356916
1 parent 5fdfd9d commit 111cd00

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

catalog/java/io/material/catalog/tabs/TabsControllableDemoFragment.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import androidx.annotation.StringRes;
2828
import com.google.android.material.tabs.TabLayout;
2929
import com.google.android.material.tabs.TabLayout.LabelVisibility;
30+
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener;
31+
import com.google.android.material.tabs.TabLayout.Tab;
3032
import androidx.appcompat.widget.SwitchCompat;
3133
import android.view.LayoutInflater;
3234
import android.view.View;
@@ -74,6 +76,7 @@ public View onCreateDemoView(
7476
setupViewPager();
7577
setAllTabLayoutIcons(ICON_DRAWABLE_RES);
7678
setAllTabLayoutText(LABEL_STRING_RES);
79+
setAllTabLayoutBadges();
7780

7881
SwitchCompat iconsToggle = view.findViewById(R.id.toggle_icons_switch);
7982
iconsToggle.setOnCheckedChangeListener(
@@ -211,6 +214,33 @@ private void setTabLayoutText(TabLayout tabLayout, @StringRes int stringResId) {
211214
}
212215
}
213216

217+
private void setAllTabLayoutBadges() {
218+
for (TabLayout tabLayout : tabLayouts) {
219+
setupBadging(tabLayout);
220+
tabLayout.addOnTabSelectedListener(
221+
new OnTabSelectedListener() {
222+
@Override
223+
public void onTabSelected(Tab tab) {
224+
tab.removeBadge();
225+
}
226+
227+
@Override
228+
public void onTabUnselected(Tab tab) {}
229+
230+
@Override
231+
public void onTabReselected(Tab tab) {
232+
tab.removeBadge();
233+
}
234+
});
235+
}
236+
}
237+
238+
private void setupBadging(TabLayout tabLayout) {
239+
tabLayout.getTabAt(0).showBadge().setNumber(1);
240+
tabLayout.getTabAt(1).showBadge().setNumber(88);
241+
tabLayout.getTabAt(2).showBadge().setNumber(999);
242+
}
243+
214244
private void setLabelVisibility(TabLayout tabLayout, @LabelVisibility int mode) {
215245
for (int i = 0; i < tabLayout.getTabCount(); i++) {
216246
tabLayout.getTabAt(i).setTabLabelVisibility(mode);

catalog/java/io/material/catalog/tabs/TabsMainDemoFragment.java

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,92 @@
2121
import android.os.Bundle;
2222
import androidx.annotation.LayoutRes;
2323
import androidx.annotation.Nullable;
24+
import com.google.android.material.badge.BadgeDrawable;
25+
import com.google.android.material.button.MaterialButton;
26+
import com.google.android.material.tabs.TabLayout;
27+
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener;
28+
import com.google.android.material.tabs.TabLayout.Tab;
2429
import android.view.LayoutInflater;
2530
import android.view.View;
31+
import android.view.View.OnClickListener;
2632
import android.view.ViewGroup;
2733
import io.material.catalog.feature.DemoFragment;
34+
import io.material.catalog.feature.DemoUtils;
35+
import java.util.List;
2836

2937
/** A fragment that displays the main tabs demo for the Catalog app. */
3038
public class TabsMainDemoFragment extends DemoFragment {
3139

40+
private List<TabLayout> tabLayouts;
41+
3242
@Nullable
3343
@Override
3444
public View onCreateDemoView(
3545
LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
36-
return layoutInflater.inflate(getTabsContent(), viewGroup, false /* attachToRoot */);
46+
View view = layoutInflater.inflate(getTabsContent(), viewGroup, /* attachToRoot= */ false);
47+
48+
tabLayouts = DemoUtils.findViewsWithType(view, TabLayout.class);
49+
MaterialButton incrementBadgeNumberButton =
50+
view.findViewById(R.id.increment_badge_number_button);
51+
incrementBadgeNumberButton.setOnClickListener(
52+
new OnClickListener() {
53+
@Override
54+
public void onClick(View v) {
55+
incrementBadgeNumber();
56+
}
57+
});
58+
59+
setupBadging();
60+
return view;
3761
}
3862

3963
@LayoutRes
4064
protected int getTabsContent() {
4165
return R.layout.cat_tabs_main_content;
4266
}
67+
68+
private void setupBadging() {
69+
for (TabLayout tabLayout : tabLayouts) {
70+
// An icon only badge will be displayed.
71+
tabLayout.getTabAt(0).showBadge();
72+
73+
// A badge with the text "99" will be displayed.
74+
tabLayout.getTabAt(1).showBadge().setNumber(99);
75+
76+
// A badge with the text "999+" will be displayed.
77+
tabLayout.getTabAt(2).showBadge().setNumber(9999);
78+
79+
tabLayout.addOnTabSelectedListener(
80+
new OnTabSelectedListener() {
81+
@Override
82+
public void onTabSelected(Tab tab) {
83+
clearAndHideBadge(tab.getPosition());
84+
}
85+
86+
@Override
87+
public void onTabUnselected(Tab tab) {}
88+
89+
@Override
90+
public void onTabReselected(Tab tab) {
91+
clearAndHideBadge(tab.getPosition());
92+
}
93+
});
94+
}
95+
}
96+
97+
private void incrementBadgeNumber() {
98+
for (TabLayout tabLayout : tabLayouts) {
99+
// Increase the badge number on the first tab position.
100+
// In case the first tab has been selected and the badge was hidden, call #showBadge() instead
101+
// of #getBadge() to ensure the badge is visible.
102+
BadgeDrawable badgeDrawable = tabLayout.getTabAt(0).showBadge();
103+
badgeDrawable.setNumber(badgeDrawable.getNumber() + 1);
104+
}
105+
}
106+
107+
private void clearAndHideBadge(int tabPosition) {
108+
for (TabLayout tabLayout : tabLayouts) {
109+
tabLayout.getTabAt(tabPosition).removeBadge();
110+
}
111+
}
43112
}

catalog/java/io/material/catalog/tabs/res/layout/cat_tabs_main_content.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,11 @@
9696

9797
</com.google.android.material.tabs.TabLayout>
9898

99+
<Button
100+
android:id="@+id/increment_badge_number_button"
101+
android:layout_width="wrap_content"
102+
android:layout_height="wrap_content"
103+
android:layout_marginVertical="8dp"
104+
android:layout_gravity="center_horizontal"
105+
android:text="@string/cat_tabs_increment_badge_number"/>
99106
</LinearLayout>

catalog/java/io/material/catalog/tabs/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,7 @@
6767
<string name="cat_tabs_label_plugins">Plugins</string>
6868
<string name="cat_tabs_label_more">More</string>
6969

70+
<!-- Badge increment button label -->
71+
<string name="cat_tabs_increment_badge_number" description="The title of a button to increment the notification number. [CHAR_LIMIT=20]">+Badge number</string>
72+
7073
</resources>

0 commit comments

Comments
 (0)