Skip to content

Commit 241aa5c

Browse files
dsn5ftraajkumars
authored andcommittedJun 23, 2022
[CollapsingToolbarLayout] Added experimental API to allow generic customizations of the StaticLayout.Builder used for the title text
Example usage: ``` collapsingToolbarLayout.setStaticLayoutBuilderConfigurer( builder -> builder.setLineBreakConfig( new LineBreakConfig.Builder() .setLineBreakWordStyle(LineBreakConfig.LINE_BREAK_WORD_STYLE_PHRASE) .build())); ``` PiperOrigin-RevId: 455625977
1 parent 0cb355b commit 241aa5c

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed
 

‎lib/java/com/google/android/material/appbar/CollapsingToolbarLayout.java

+29
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,24 @@ public int getHyphenationFrequency() {
13981398
return collapsingTextHelper.getHyphenationFrequency();
13991399
}
14001400

1401+
/**
1402+
* Sets the {@link StaticLayoutBuilderConfigurer} for the {@link android.text.StaticLayout} of the
1403+
* title text.
1404+
*
1405+
* <p>Note that the updates to the {@link android.text.StaticLayout.Builder} in the configure
1406+
* method (e.g., the text, alignment, max lines, line spacing, etc.) will take precedence over and
1407+
* overwrite any previous configurations made by the {@link CollapsingToolbarLayout} to the same
1408+
* properties.
1409+
*
1410+
* @hide
1411+
*/
1412+
@RestrictTo(LIBRARY_GROUP)
1413+
@RequiresApi(VERSION_CODES.M)
1414+
public void setStaticLayoutBuilderConfigurer(
1415+
@Nullable StaticLayoutBuilderConfigurer staticLayoutBuilderConfigurer) {
1416+
collapsingTextHelper.setStaticLayoutBuilderConfigurer(staticLayoutBuilderConfigurer);
1417+
}
1418+
14011419
/**
14021420
* Sets whether {@code TextDirectionHeuristics} should be used to determine whether the title text
14031421
* is RTL. Experimental Feature.
@@ -1732,4 +1750,15 @@ public void onOffsetChanged(AppBarLayout layout, int verticalOffset) {
17321750
collapsingTextHelper.setExpansionFraction(Math.abs(verticalOffset) / (float) expandRange);
17331751
}
17341752
}
1753+
1754+
/**
1755+
* Interface that allows for further customization of the provided {@link
1756+
* android.text.StaticLayout}.
1757+
*
1758+
* @hide
1759+
*/
1760+
@RestrictTo(LIBRARY_GROUP)
1761+
@RequiresApi(VERSION_CODES.M)
1762+
public interface StaticLayoutBuilderConfigurer
1763+
extends com.google.android.material.internal.StaticLayoutBuilderConfigurer {}
17351764
}

‎lib/java/com/google/android/material/internal/CollapsingTextHelper.java

+11
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ public final class CollapsingTextHelper {
176176
private float lineSpacingAdd = StaticLayoutBuilderCompat.DEFAULT_LINE_SPACING_ADD;
177177
private float lineSpacingMultiplier = StaticLayoutBuilderCompat.DEFAULT_LINE_SPACING_MULTIPLIER;
178178
private int hyphenationFrequency = StaticLayoutBuilderCompat.DEFAULT_HYPHENATION_FREQUENCY;
179+
@Nullable private StaticLayoutBuilderConfigurer staticLayoutBuilderConfigurer;
179180

180181
public CollapsingTextHelper(View view) {
181182
this.view = view;
@@ -1075,6 +1076,7 @@ private StaticLayout createStaticLayout(int maxLines, float availableWidth, bool
10751076
.setMaxLines(maxLines)
10761077
.setLineSpacing(lineSpacingAdd, lineSpacingMultiplier)
10771078
.setHyphenationFrequency(hyphenationFrequency)
1079+
.setStaticLayoutBuilderConfigurer(staticLayoutBuilderConfigurer)
10781080
.build();
10791081
} catch (StaticLayoutBuilderCompatException e) {
10801082
Log.e(TAG, e.getCause().getMessage(), e);
@@ -1220,6 +1222,15 @@ public int getHyphenationFrequency() {
12201222
return hyphenationFrequency;
12211223
}
12221224

1225+
@RequiresApi(VERSION_CODES.M)
1226+
public void setStaticLayoutBuilderConfigurer(
1227+
@Nullable StaticLayoutBuilderConfigurer staticLayoutBuilderConfigurer) {
1228+
if (this.staticLayoutBuilderConfigurer != staticLayoutBuilderConfigurer) {
1229+
this.staticLayoutBuilderConfigurer = staticLayoutBuilderConfigurer;
1230+
recalculate(/* forceRecalculate= */ true);
1231+
}
1232+
}
1233+
12231234
/**
12241235
* Returns true if {@code value} is 'close' to it's closest decimal value. Close is currently
12251236
* defined as it's difference being < 0.00001.

‎lib/java/com/google/android/material/internal/StaticLayoutBuilderCompat.java

+15
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ final class StaticLayoutBuilderCompat {
8484
private boolean includePad;
8585
private boolean isRtl;
8686
@Nullable private TextUtils.TruncateAt ellipsize;
87+
@Nullable private StaticLayoutBuilderConfigurer staticLayoutBuilderConfigurer;
8788

8889
private StaticLayoutBuilderCompat(CharSequence source, TextPaint paint, int width) {
8990
this.source = source;
@@ -219,6 +220,17 @@ public StaticLayoutBuilderCompat setEllipsize(@Nullable TextUtils.TruncateAt ell
219220
return this;
220221
}
221222

223+
/**
224+
* Set the {@link StaticLayoutBuilderConfigurer} which allows additional custom configurations on
225+
* the static layout.
226+
*/
227+
@NonNull
228+
public StaticLayoutBuilderCompat setStaticLayoutBuilderConfigurer(
229+
@Nullable StaticLayoutBuilderConfigurer staticLayoutBuilderConfigurer) {
230+
this.staticLayoutBuilderConfigurer = staticLayoutBuilderConfigurer;
231+
return this;
232+
}
233+
222234
/** A method that allows to create a StaticLayout with maxLines on all supported API levels. */
223235
public StaticLayout build() throws StaticLayoutBuilderCompatException {
224236
if (source == null) {
@@ -259,6 +271,9 @@ public StaticLayout build() throws StaticLayoutBuilderCompatException {
259271
if (maxLines > 1) {
260272
builder.setHyphenationFrequency(hyphenationFrequency);
261273
}
274+
if (staticLayoutBuilderConfigurer != null) {
275+
staticLayoutBuilderConfigurer.configure(builder);
276+
}
262277
return builder.build();
263278
}
264279

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (C) 2022 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.android.material.internal;
18+
19+
import android.text.StaticLayout;
20+
import androidx.annotation.NonNull;
21+
import androidx.annotation.RestrictTo;
22+
import androidx.annotation.RestrictTo.Scope;
23+
24+
/**
25+
* Interface that allows for further customization of the provided {@link
26+
* android.text.StaticLayout}.
27+
*
28+
* @hide
29+
*/
30+
@RestrictTo(Scope.LIBRARY_GROUP)
31+
public interface StaticLayoutBuilderConfigurer {
32+
void configure(@NonNull StaticLayout.Builder builder);
33+
}

0 commit comments

Comments
 (0)
Please sign in to comment.