material-components / material-components-android

Modular and customizable Material Design UI components for Android
Apache License 2.0
16.37k stars 3.07k forks source link

[CollapsingToolbarLayout] MultiLine (maxLines) not applying correct gravity to text #1276

Closed saantiaguilera closed 3 years ago

saantiaguilera commented 4 years ago

Description: When using maxLines in alpha06 with expandedTitleGravity set to center. Text isn't centered.

Expected behavior: The expanded title should be centered (or the desired gravity should be applied)

Source code:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:fitsSystemWindows="true">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/ui_fullscreenmodal_appbarlayout_height"
        android:stateListAnimator="@animator/appbar_elevation_zero"
        android:fitsSystemWindows="true" >

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:expandedTitleGravity="center"
            app:contentScrim="@color/white"
            app:expandedTitleTextAppearance="@style/CollapsingToolbarStyle.Expanded"
            app:collapsedTitleTextAppearance="@style/CollapsingToolbarStyle.Collapsed"
            app:title="long enough text to use 2 lines"
            app:maxLines="2"
            app:layout_scrollFlags="scroll|enterAlwaysCollapsed">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:gravity="center"
                app:layout_collapseMode="none" />

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

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

    <androidx.core.widget.NestedScrollView
        android:id="@+id/terms_and_conditions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:gravity="center"
            android:clickable="true"
            android:text="some large text"/>

    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Android API version: Android 7.0 (api 24)

Material Library version: 1.2.0-alpha06

Device: Samsung SM-G925I

To help us triage faster, please check to make sure you are using the latest version of the library.

We also happily accept pull requests.

saantiaguilera commented 4 years ago

Making a clone of the CollapsingToolbarLayout and using in the helper ALIGN_CENTER seems to fix the issue. As soon as I finish my sprint I will try to come up with a PullRequest fixing it here (if it's not already done).

If someone can iterate this faster than me, the line is this one

Cheers guys :)

saantiaguilera commented 4 years ago

I found other issues (which I might try to fix also, if not at least I document them):

Code for more visibility:

  // In CollapsingTextHelper.
  private void drawMultinlineTransition(
      @NonNull Canvas canvas, float currentExpandedX, float x, float y, float ascent) {
    int originalAlpha = textPaint.getAlpha();
    // positon expanded text appropriately
    canvas.translate(currentExpandedX, y);
    // Expanded text
    textPaint.setAlpha((int) (expandedTextBlend * originalAlpha));
    textLayout.draw(canvas);

    if (expandedTextBlend != 1) { // --> This is how I avoided the blurry first expanded line.
      // position the overlays
      canvas.translate(x - currentExpandedX, 0);

      // Collapsed text
      textPaint.setAlpha((int) (collapsedTextBlend * originalAlpha));
      canvas.drawText(
              textToDrawCollapsed, 0, textToDrawCollapsed.length(), 0, -ascent / scale, textPaint);

      // Remove ellipsis for Cross-section animation
      String tmp = textToDrawCollapsed.toString().trim();
      if (tmp.endsWith(ELLIPSIS_NORMAL)) {
        tmp = tmp.substring(0, tmp.length() - 1);
      }
      // Cross-section between both texts (should stay at original alpha)
      textPaint.setAlpha(originalAlpha);
      canvas.drawText(
              tmp, 0, Math.min(textLayout.getLineEnd(0), tmp.length()), 0, -ascent / scale, textPaint);
    }
  }
Entreco commented 4 years ago

We have the same issue, using version 1.2.0! We want 2 lines and gravity to be centered. Let me know if you have found more details, otherwise I will try to find time to investigate myself.

damirasol commented 3 years ago

Can confirm bug still happens on version 1.2.1

foodpoison commented 3 years ago

from what I can tell the last version of the library that didn't have this issue was 1.2.0-beta01

I ended up taking a hackish approach, changing maxLine to be larger than one if the length of the title is larger than a certain number.

PedroMoniz commented 3 years ago

Bug still exists in 1.3.0-alpha03, I currently have a need for this feature as it breaks the UI.

gookman commented 3 years ago

👍

PineapplePie commented 3 years ago

I'm facing the same issue in 1.4.0-beta01

AlexandreGorobets commented 3 years ago

Why thet cannot apply pull request fix, or chage StaticLayout.setAlignment(Alignment.ALIGN_NORMAL) to setAlignment(Alignment.ALIGN_CENTER) if expandedTextGravity == center|center_horizontal ?

omiwrench commented 3 years ago

Bug still exists in 1.5.0-alpha01.