Skip to content

Instantly share code, notes, and snippets.

@kevinpelgrims
Last active January 12, 2023 04:52
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kevinpelgrims/8685c8e1a68e3cd9cff9 to your computer and use it in GitHub Desktop.
Save kevinpelgrims/8685c8e1a68e3cd9cff9 to your computer and use it in GitHub Desktop.
ActionBar tab badge
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<solid android:color="@color/badge_background" />
</shape>
actionBar.addTab(actionBar.newTab()
.setCustomView(TabUtils.renderTabView(NotificationsActivity.this, R.string.tab_invitations, R.drawable.tab_orange, numberOfNotifications))
.setTabListener(tabListener));
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tab_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textAppearance="@style/Widget.AppCompat.Light.ActionBar.TabText"/>
<TextView
android:id="@+id/tab_badge"
android:layout_width="@dimen/badgeSizeHome"
android:layout_height="@dimen/badgeSizeHome"
android:background="@drawable/badge_background"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:layout_gravity="top|right"
android:gravity="center"
android:textColor="@color/white"
android:textSize="8dp"/>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent"/>
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_orange_background"/>
<item android:state_pressed="true" android:drawable="@drawable/tab_orange_background_pressed"/>
<item android:state_selected="true" android:drawable="@drawable/tab_orange_background_pressed"/>
</selector>
public static View renderTabView(Context context, int titleResource, int backgroundResource, int badgeNumber) {
FrameLayout view = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.tab_badge, null);
// We need to manually set the LayoutParams here because we don't have a view root
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
((TextView) view.findViewById(R.id.tab_text)).setText(titleResource);
view.findViewById(R.id.tab_text).setBackgroundResource(backgroundResource);
updateTabBadge((TextView) view.findViewById(R.id.tab_badge), badgeNumber);
return view;
}
public static void updateTabBadge(ActionBar.Tab tab, int badgeNumber) {
updateTabBadge((TextView) tab.getCustomView().findViewById(R.id.tab_badge), badgeNumber);
}
private static void updateTabBadge(TextView view, int badgeNumber) {
if (badgeNumber > 0) {
view.setVisibility(View.VISIBLE);
view.setText(Integer.toString(badgeNumber));
}
else {
view.setVisibility(View.GONE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment