Skip to content

[CollapsingToolbarLayout] Exposes title textAlignment property #1186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.appcompat.widget.Toolbar;

import android.text.Layout.Alignment;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
Expand Down Expand Up @@ -178,6 +180,13 @@ public CollapsingToolbarLayout(@NonNull Context context, @Nullable AttributeSet
R.styleable.CollapsingToolbarLayout_collapsedTitleGravity,
GravityCompat.START | Gravity.CENTER_VERTICAL));

if (a.hasValue(R.styleable.CollapsingToolbarLayout_titleTextAlignment)) {
int id = a.getInt(
R.styleable.CollapsingToolbarLayout_titleTextAlignment, 0);
Alignment alignment = Alignment.values()[id];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to explicitly create a conversion method here to prevent the dependency on enum orders, which can be fragile.

collapsingTextHelper.setTextAlignment(alignment);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setExpandedTitleTextAlignment(alignment);

}

expandedMarginStart =
expandedMarginTop =
expandedMarginEnd =
Expand Down Expand Up @@ -916,6 +925,24 @@ public int getExpandedTitleGravity() {
return collapsingTextHelper.getExpandedTextGravity();
}

/**
* Sets the text alignment of the expanded title.
*
* @attr ref com.google.android.material.R.styleable#CollapsingToolbarLayout_titleTextAlignment
*/
public void setExpandedTitleTextAlignment(@Nullable Alignment alignment) {
collapsingTextHelper.setTextAlignment(alignment);
}

/**
* Returns the text alignment for title when expanded.
*
* @attr ref com.google.android.material.R.styleable#CollapsingToolbarLayout_titleTextAlignment
*/
public Alignment getExpandedTitleTextAlignment() {
return collapsingTextHelper.getTextAlignment();
}

/**
* Set the typeface to use for the collapsed title.
*
Expand Down
10 changes: 10 additions & 0 deletions lib/java/com/google/android/material/appbar/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@
<flag name="end" value="0x00800005"/>
</attr>

<!-- The alignment of the title text. -->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a description to explain that the value here should be corresponding to the enum android.text.Layout.Alignment.

<attr name="titleTextAlignment" format="enum">
<!-- Align to the start of the paragraph, e.g. ALIGN_NORMAL. -->
<enum name="textStart" value="0" />
<!-- Align to the end of the paragraph, e.g. ALIGN_OPPOSITE. -->
<enum name="textEnd" value="1" />
<!-- Center the paragraph, e.g. ALIGN_CENTER. -->
<enum name="center" value="2" />
</attr>

<!-- Whether the CollapsingToolbarLayout should draw its own shrinking/growing title. -->
<attr name="titleEnabled" format="boolean"/>
<!-- The title to show when titleEnabled is set to true. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import androidx.core.text.TextDirectionHeuristicsCompat;
import androidx.core.view.GravityCompat;
import androidx.core.view.ViewCompat;

import android.text.Layout.Alignment;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.TextUtils;
Expand Down Expand Up @@ -134,6 +136,7 @@ public final class CollapsingTextHelper {
private float expandedShadowDy;
private ColorStateList expandedShadowColor;
private StaticLayout textLayout;
private Alignment textAlignment;
private float collapsedTextBlend;
private float expandedTextBlend;
private float expandedFirstLineDrawX;
Expand All @@ -149,6 +152,8 @@ public CollapsingTextHelper(View view) {
collapsedBounds = new Rect();
expandedBounds = new Rect();
currentBounds = new RectF();

textAlignment = ALIGN_NORMAL;
}

public void setTextSizeInterpolator(TimeInterpolator interpolator) {
Expand Down Expand Up @@ -826,7 +831,7 @@ private StaticLayout createStaticLayout(int maxLines, float availableWidth, bool
StaticLayoutBuilderCompat.obtain(text, textPaint, (int) availableWidth)
.setEllipsize(TruncateAt.END)
.setIsRtl(isRtl)
.setAlignment(ALIGN_NORMAL)
.setAlignment(textAlignment)
.setIncludePad(false)
.setMaxLines(maxLines)
.build();
Expand Down Expand Up @@ -888,6 +893,14 @@ public CharSequence getText() {
return text;
}

public void setTextAlignment(Alignment alignment) {
textAlignment = alignment;
}

public Alignment getTextAlignment() {
return textAlignment;
}

private void clearTexture() {
if (expandedTitleTexture != null) {
expandedTitleTexture.recycle();
Expand Down