Skip to content

Commit b997092

Browse files
dsn5ftldjcmu
authored andcommitted
Add support for Snackbar message text styling via snackbarTextViewStyle attr
PiperOrigin-RevId: 272198481
1 parent 0593519 commit b997092

9 files changed

Lines changed: 38 additions & 15 deletions

File tree

lib/java/com/google/android/material/dialog/res/values/themes_base_bridge.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<item name="sliderStyle">@style/Widget.MaterialComponents.Slider</item>
6868
<item name="snackbarStyle">@style/Widget.MaterialComponents.Snackbar</item>
6969
<item name="snackbarButtonStyle">@style/Widget.MaterialComponents.Button.TextButton.Snackbar</item>
70+
<item name="snackbarTextViewStyle">@style/Widget.MaterialComponents.Snackbar.TextView</item>
7071
<item name="tabStyle">@style/Widget.MaterialComponents.TabLayout</item>
7172
<item name="textInputStyle">@style/Widget.MaterialComponents.TextInputLayout.FilledBox</item>
7273

@@ -138,6 +139,7 @@
138139
<item name="sliderStyle">@style/Widget.MaterialComponents.Slider</item>
139140
<item name="snackbarStyle">@style/Widget.MaterialComponents.Snackbar</item>
140141
<item name="snackbarButtonStyle">@style/Widget.MaterialComponents.Button.TextButton.Snackbar</item>
142+
<item name="snackbarTextViewStyle">@style/Widget.MaterialComponents.Snackbar.TextView</item>
141143
<item name="tabStyle">@style/Widget.MaterialComponents.TabLayout</item>
142144
<item name="textInputStyle">@style/Widget.MaterialComponents.TextInputLayout.FilledBox</item>
143145

lib/java/com/google/android/material/snackbar/Snackbar.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public class Snackbar extends BaseTransientBottomBar<Snackbar> {
6666
private boolean hasAction;
6767

6868
private static final int[] SNACKBAR_BUTTON_STYLE_ATTR = new int[] {R.attr.snackbarButtonStyle};
69+
private static final int[] SNACKBAR_CONTENT_STYLE_ATTRS =
70+
new int[] {R.attr.snackbarButtonStyle, R.attr.snackbarTextViewStyle};
6971

7072
/**
7173
* Callback class for {@link Snackbar} instances.
@@ -160,7 +162,7 @@ public static Snackbar make(
160162
final SnackbarContentLayout content =
161163
(SnackbarContentLayout)
162164
inflater.inflate(
163-
hasSnackbarButtonStyleAttr(parent.getContext())
165+
hasSnackbarContentStyleAttrs(parent.getContext())
164166
? R.layout.mtrl_layout_snackbar_include
165167
: R.layout.design_layout_snackbar_include,
166168
parent,
@@ -176,13 +178,22 @@ public static Snackbar make(
176178
* snackbarButtonStyle}. This method helps to check if a valid {@code snackbarButtonStyle} is set
177179
* within the current context, so that we know whether we can use the attribute.
178180
*/
181+
@Deprecated
179182
protected static boolean hasSnackbarButtonStyleAttr(@NonNull Context context) {
180183
TypedArray a = context.obtainStyledAttributes(SNACKBAR_BUTTON_STYLE_ATTR);
181184
int snackbarButtonStyleResId = a.getResourceId(0, -1);
182185
a.recycle();
183186
return snackbarButtonStyleResId != -1;
184187
}
185188

189+
private static boolean hasSnackbarContentStyleAttrs(@NonNull Context context) {
190+
TypedArray a = context.obtainStyledAttributes(SNACKBAR_CONTENT_STYLE_ATTRS);
191+
int snackbarButtonStyleResId = a.getResourceId(0, -1);
192+
int snackbarTextViewStyleResId = a.getResourceId(1, -1);
193+
a.recycle();
194+
return snackbarButtonStyleResId != -1 && snackbarTextViewStyleResId != -1;
195+
}
196+
186197
/**
187198
* Make a Snackbar to display a message.
188199
*

lib/java/com/google/android/material/snackbar/res-public/values/public.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
<resources>
1919
<public name="snackbarStyle" type="attr"/>
2020
<public name="snackbarButtonStyle" type="attr"/>
21+
<public name="snackbarTextViewStyle" type="attr"/>
2122
<public name="Widget.Design.Snackbar" type="style"/>
2223
<public name="Widget.MaterialComponents.Snackbar" type="style"/>
2324
<public name="Widget.MaterialComponents.Snackbar.FullWidth" type="style"/>
25+
<public name="Widget.MaterialComponents.Snackbar.TextView" type="style"/>
2426
<public name="animationMode" type="attr"/>
2527
</resources>

lib/java/com/google/android/material/snackbar/res/layout/mtrl_layout_snackbar_include.xml

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,11 @@
2525

2626
<TextView
2727
android:id="@+id/snackbar_text"
28+
style="?attr/snackbarTextViewStyle"
2829
android:layout_width="wrap_content"
2930
android:layout_height="wrap_content"
3031
android:layout_weight="1"
31-
android:layout_gravity="center_vertical|left|start"
32-
android:paddingTop="@dimen/design_snackbar_padding_vertical"
33-
android:paddingBottom="@dimen/design_snackbar_padding_vertical"
34-
android:paddingLeft="@dimen/design_snackbar_padding_horizontal"
35-
android:paddingRight="@dimen/design_snackbar_padding_horizontal"
36-
android:alpha="@dimen/material_emphasis_high_type"
37-
android:ellipsize="end"
38-
android:maxLines="@integer/design_snackbar_text_max_lines"
39-
android:textAlignment="viewStart"
40-
android:textAppearance="?attr/textAppearanceBody2"
41-
android:textColor="?attr/colorSurface"/>
32+
android:layout_gravity="center_vertical|left|start"/>
4233

4334
<Button
4435
android:id="@+id/snackbar_action"

lib/java/com/google/android/material/snackbar/res/values/attrs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
<attr name="snackbarStyle" format="reference"/>
2222
<!-- Style to use for action button within a Snackbar in this theme. -->
2323
<attr name="snackbarButtonStyle" format="reference"/>
24+
<!-- Style to use for message text within a Snackbar in this theme. -->
25+
<attr name="snackbarTextViewStyle" format="reference"/>
2426
</declare-styleable>
2527

2628
<declare-styleable name="SnackbarLayout">

lib/java/com/google/android/material/snackbar/res/values/styles.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
~ See the License for the specific language governing permissions and
1515
~ limitations under the License.
1616
-->
17-
<resources>
17+
<resources xmlns:tools="http://schemas.android.com/tools">
1818

1919
<style name="TextAppearance.Design.Snackbar.Message" parent="android:TextAppearance">
2020
<item name="android:textSize">@dimen/design_snackbar_text_size</item>
@@ -48,4 +48,17 @@
4848
<item name="actionTextColorAlpha">@dimen/mtrl_snackbar_action_text_color_alpha</item>
4949
</style>
5050

51+
<style name="Widget.MaterialComponents.Snackbar.TextView" parent="Widget.AppCompat.TextView">
52+
<item name="android:alpha">@dimen/material_emphasis_high_type</item>
53+
<item name="android:ellipsize">end</item>
54+
<item name="android:maxLines">@integer/design_snackbar_text_max_lines</item>
55+
<item name="android:textAlignment" tools:ignore="NewApi">viewStart</item>
56+
<item name="android:textAppearance">?attr/textAppearanceBody2</item>
57+
<item name="android:textColor">?attr/colorSurface</item>
58+
<item name="android:paddingTop">@dimen/design_snackbar_padding_vertical</item>
59+
<item name="android:paddingBottom">@dimen/design_snackbar_padding_vertical</item>
60+
<item name="android:paddingLeft">@dimen/design_snackbar_padding_horizontal</item>
61+
<item name="android:paddingRight">@dimen/design_snackbar_padding_horizontal</item>
62+
</style>
63+
5164
</resources>

lib/java/com/google/android/material/textview/MaterialAutoCompleteTextView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import android.os.Build.VERSION;
2323
import androidx.annotation.NonNull;
2424
import androidx.annotation.Nullable;
25-
import android.support.v7.widget.AppCompatAutoCompleteTextView;
25+
import androidx.appcompat.widget.AppCompatAutoCompleteTextView;
2626
import androidx.appcompat.widget.ListPopupWindow;
2727
import android.util.AttributeSet;
2828
import android.view.View;

lib/java/com/google/android/material/theme/MaterialComponentsViewInflater.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import androidx.annotation.Nullable;
2727
import androidx.annotation.RestrictTo;
2828
import androidx.appcompat.app.AppCompatViewInflater;
29-
import android.support.v7.widget.AppCompatAutoCompleteTextView;
29+
import androidx.appcompat.widget.AppCompatAutoCompleteTextView;
3030
import androidx.appcompat.widget.AppCompatButton;
3131
import androidx.appcompat.widget.AppCompatCheckBox;
3232
import androidx.appcompat.widget.AppCompatRadioButton;

lib/java/com/google/android/material/theme/res/values/themes_base_bridge.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<item name="sliderStyle">@style/Widget.MaterialComponents.Slider</item>
6868
<item name="snackbarStyle">@style/Widget.MaterialComponents.Snackbar</item>
6969
<item name="snackbarButtonStyle">@style/Widget.MaterialComponents.Button.TextButton.Snackbar</item>
70+
<item name="snackbarTextViewStyle">@style/Widget.MaterialComponents.Snackbar.TextView</item>
7071
<item name="tabStyle">@style/Widget.MaterialComponents.TabLayout</item>
7172
<item name="textInputStyle">@style/Widget.MaterialComponents.TextInputLayout.FilledBox</item>
7273

@@ -138,6 +139,7 @@
138139
<item name="sliderStyle">@style/Widget.MaterialComponents.Slider</item>
139140
<item name="snackbarStyle">@style/Widget.MaterialComponents.Snackbar</item>
140141
<item name="snackbarButtonStyle">@style/Widget.MaterialComponents.Button.TextButton.Snackbar</item>
142+
<item name="snackbarTextViewStyle">@style/Widget.MaterialComponents.Snackbar.TextView</item>
141143
<item name="tabStyle">@style/Widget.MaterialComponents.TabLayout</item>
142144
<item name="textInputStyle">@style/Widget.MaterialComponents.TextInputLayout.FilledBox</item>
143145

0 commit comments

Comments
 (0)