material-components-android icon indicating copy to clipboard operation
material-components-android copied to clipboard

[Widget.Material3.Badge] Badge in TabLayout overlaps text

Open JohnGale87 opened this issue 3 years ago • 4 comments

Description: Material 3 appears to have regressed this issue. A badge containing numbers overlaps the text so much that it obscures the last letter of the label.

image

Expected behavior: The label should not be obscured

Source code:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/topAppBar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:title="@string/app_name" />

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed" />

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginHorizontal="@dimen/standard_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

MainActivity.kt - onCreate

TabLayoutMediator(tabs, viewPager) { tab, position ->
    val badge = tab.getOrCreateBadge()
    badge.number = 1
    tab.text = resources.getString(TAB_TITLES[position])
}.attach()

Android API version: Tested on API 29

Material Library version: com.google.android.material:material:1.5.0

Device: AVD - Nexus 4

JohnGale87 avatar Feb 10 '22 13:02 JohnGale87

Confirmed with designers. We are supposed to put badges at the trailing edge of the text. It seems like a bug in our implementation.

drchen avatar Aug 04 '22 15:08 drchen

FYI there's a small design update with badges that tweaks alignment. This issue will be exacerbated since the badge now has a smaller horizontal offset from the anchor.

Will speak with the current designer for badges about specs for badges and text, but for now you can use setHorizontalOffset to increase the horizontal offset of the badge

imhappi avatar Jan 30 '23 19:01 imhappi