Skip to content

Commit

Permalink
[Snackbar] Support setting max lines programmatically
Browse files Browse the repository at this point in the history
Resolves #2018

PiperOrigin-RevId: 427305265
  • Loading branch information
drchen authored and raajkumars committed Feb 9, 2022
1 parent ae16efd commit 4c7be52
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions lib/java/com/google/android/material/snackbar/Snackbar.java
Expand Up @@ -36,6 +36,7 @@
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.accessibility.AccessibilityManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import androidx.annotation.ColorInt;
Expand Down Expand Up @@ -298,9 +299,7 @@ private static ViewGroup findSuitableParent(View view) {
*/
@NonNull
public Snackbar setText(@NonNull CharSequence message) {
final SnackbarContentLayout contentLayout = (SnackbarContentLayout) view.getChildAt(0);
final TextView tv = contentLayout.getMessageView();
tv.setText(message);
getMessageView().setText(message);
return this;
}

Expand Down Expand Up @@ -334,8 +333,7 @@ public Snackbar setAction(@StringRes int resId, View.OnClickListener listener) {
@NonNull
public Snackbar setAction(
@Nullable CharSequence text, @Nullable final View.OnClickListener listener) {
final SnackbarContentLayout contentLayout = (SnackbarContentLayout) this.view.getChildAt(0);
final TextView tv = contentLayout.getActionView();
final TextView tv = getActionView();
if (TextUtils.isEmpty(text) || listener == null) {
tv.setVisibility(View.GONE);
tv.setOnClickListener(null);
Expand Down Expand Up @@ -383,9 +381,7 @@ public int getDuration() {
*/
@NonNull
public Snackbar setTextColor(ColorStateList colors) {
final SnackbarContentLayout contentLayout = (SnackbarContentLayout) view.getChildAt(0);
final TextView tv = contentLayout.getMessageView();
tv.setTextColor(colors);
getMessageView().setTextColor(colors);
return this;
}

Expand All @@ -395,9 +391,17 @@ public Snackbar setTextColor(ColorStateList colors) {
*/
@NonNull
public Snackbar setTextColor(@ColorInt int color) {
final SnackbarContentLayout contentLayout = (SnackbarContentLayout) view.getChildAt(0);
final TextView tv = contentLayout.getMessageView();
tv.setTextColor(color);
getMessageView().setTextColor(color);
return this;
}

/**
* Sets the max line count of the message specified in {@link #setText(CharSequence)} and {@link
* #setText(int)}.
*/
@NonNull
public Snackbar setTextMaxLines(int maxLines) {
getMessageView().setMaxLines(maxLines);
return this;
}

Expand All @@ -407,9 +411,7 @@ public Snackbar setTextColor(@ColorInt int color) {
*/
@NonNull
public Snackbar setActionTextColor(ColorStateList colors) {
final SnackbarContentLayout contentLayout = (SnackbarContentLayout) view.getChildAt(0);
final TextView tv = contentLayout.getActionView();
tv.setTextColor(colors);
getActionView().setTextColor(colors);
return this;
}

Expand All @@ -419,8 +421,7 @@ public Snackbar setActionTextColor(ColorStateList colors) {
*/
@NonNull
public Snackbar setMaxInlineActionWidth(@Dimension int width) {
final SnackbarContentLayout contentLayout = (SnackbarContentLayout) view.getChildAt(0);
contentLayout.setMaxInlineActionWidth(width);
getContentLayout().setMaxInlineActionWidth(width);
return this;
}

Expand All @@ -430,9 +431,7 @@ public Snackbar setMaxInlineActionWidth(@Dimension int width) {
*/
@NonNull
public Snackbar setActionTextColor(@ColorInt int color) {
final SnackbarContentLayout contentLayout = (SnackbarContentLayout) view.getChildAt(0);
final TextView tv = contentLayout.getActionView();
tv.setTextColor(color);
getActionView().setTextColor(color);
return this;
}

Expand Down Expand Up @@ -516,4 +515,16 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
}
}
}

private TextView getMessageView() {
return getContentLayout().getMessageView();
}

private Button getActionView() {
return getContentLayout().getActionView();
}

private SnackbarContentLayout getContentLayout() {
return (SnackbarContentLayout) view.getChildAt(0);
}
}

0 comments on commit 4c7be52

Please sign in to comment.