Skip to content

Commit

Permalink
Add support for Snackbar message text styling via snackbarTextViewSty…
Browse files Browse the repository at this point in the history
…le attr

PiperOrigin-RevId: 272198481
  • Loading branch information
dsn5ft authored and ldjcmu committed Oct 1, 2019
1 parent 0593519 commit b997092
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<item name="sliderStyle">@style/Widget.MaterialComponents.Slider</item>
<item name="snackbarStyle">@style/Widget.MaterialComponents.Snackbar</item>
<item name="snackbarButtonStyle">@style/Widget.MaterialComponents.Button.TextButton.Snackbar</item>
<item name="snackbarTextViewStyle">@style/Widget.MaterialComponents.Snackbar.TextView</item>
<item name="tabStyle">@style/Widget.MaterialComponents.TabLayout</item>
<item name="textInputStyle">@style/Widget.MaterialComponents.TextInputLayout.FilledBox</item>

Expand Down Expand Up @@ -138,6 +139,7 @@
<item name="sliderStyle">@style/Widget.MaterialComponents.Slider</item>
<item name="snackbarStyle">@style/Widget.MaterialComponents.Snackbar</item>
<item name="snackbarButtonStyle">@style/Widget.MaterialComponents.Button.TextButton.Snackbar</item>
<item name="snackbarTextViewStyle">@style/Widget.MaterialComponents.Snackbar.TextView</item>
<item name="tabStyle">@style/Widget.MaterialComponents.TabLayout</item>
<item name="textInputStyle">@style/Widget.MaterialComponents.TextInputLayout.FilledBox</item>

Expand Down
13 changes: 12 additions & 1 deletion lib/java/com/google/android/material/snackbar/Snackbar.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class Snackbar extends BaseTransientBottomBar<Snackbar> {
private boolean hasAction;

private static final int[] SNACKBAR_BUTTON_STYLE_ATTR = new int[] {R.attr.snackbarButtonStyle};
private static final int[] SNACKBAR_CONTENT_STYLE_ATTRS =
new int[] {R.attr.snackbarButtonStyle, R.attr.snackbarTextViewStyle};

/**
* Callback class for {@link Snackbar} instances.
Expand Down Expand Up @@ -160,7 +162,7 @@ public static Snackbar make(
final SnackbarContentLayout content =
(SnackbarContentLayout)
inflater.inflate(
hasSnackbarButtonStyleAttr(parent.getContext())
hasSnackbarContentStyleAttrs(parent.getContext())
? R.layout.mtrl_layout_snackbar_include
: R.layout.design_layout_snackbar_include,
parent,
Expand All @@ -176,13 +178,22 @@ public static Snackbar make(
* snackbarButtonStyle}. This method helps to check if a valid {@code snackbarButtonStyle} is set
* within the current context, so that we know whether we can use the attribute.
*/
@Deprecated
protected static boolean hasSnackbarButtonStyleAttr(@NonNull Context context) {
TypedArray a = context.obtainStyledAttributes(SNACKBAR_BUTTON_STYLE_ATTR);
int snackbarButtonStyleResId = a.getResourceId(0, -1);
a.recycle();
return snackbarButtonStyleResId != -1;
}

private static boolean hasSnackbarContentStyleAttrs(@NonNull Context context) {
TypedArray a = context.obtainStyledAttributes(SNACKBAR_CONTENT_STYLE_ATTRS);
int snackbarButtonStyleResId = a.getResourceId(0, -1);
int snackbarTextViewStyleResId = a.getResourceId(1, -1);
a.recycle();
return snackbarButtonStyleResId != -1 && snackbarTextViewStyleResId != -1;
}

/**
* Make a Snackbar to display a message.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
<resources>
<public name="snackbarStyle" type="attr"/>
<public name="snackbarButtonStyle" type="attr"/>
<public name="snackbarTextViewStyle" type="attr"/>
<public name="Widget.Design.Snackbar" type="style"/>
<public name="Widget.MaterialComponents.Snackbar" type="style"/>
<public name="Widget.MaterialComponents.Snackbar.FullWidth" type="style"/>
<public name="Widget.MaterialComponents.Snackbar.TextView" type="style"/>
<public name="animationMode" type="attr"/>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,11 @@

<TextView
android:id="@+id/snackbar_text"
style="?attr/snackbarTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical|left|start"
android:paddingTop="@dimen/design_snackbar_padding_vertical"
android:paddingBottom="@dimen/design_snackbar_padding_vertical"
android:paddingLeft="@dimen/design_snackbar_padding_horizontal"
android:paddingRight="@dimen/design_snackbar_padding_horizontal"
android:alpha="@dimen/material_emphasis_high_type"
android:ellipsize="end"
android:maxLines="@integer/design_snackbar_text_max_lines"
android:textAlignment="viewStart"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="?attr/colorSurface"/>
android:layout_gravity="center_vertical|left|start"/>

<Button
android:id="@+id/snackbar_action"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<attr name="snackbarStyle" format="reference"/>
<!-- Style to use for action button within a Snackbar in this theme. -->
<attr name="snackbarButtonStyle" format="reference"/>
<!-- Style to use for message text within a Snackbar in this theme. -->
<attr name="snackbarTextViewStyle" format="reference"/>
</declare-styleable>

<declare-styleable name="SnackbarLayout">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">

<style name="TextAppearance.Design.Snackbar.Message" parent="android:TextAppearance">
<item name="android:textSize">@dimen/design_snackbar_text_size</item>
Expand Down Expand Up @@ -48,4 +48,17 @@
<item name="actionTextColorAlpha">@dimen/mtrl_snackbar_action_text_color_alpha</item>
</style>

<style name="Widget.MaterialComponents.Snackbar.TextView" parent="Widget.AppCompat.TextView">
<item name="android:alpha">@dimen/material_emphasis_high_type</item>
<item name="android:ellipsize">end</item>
<item name="android:maxLines">@integer/design_snackbar_text_max_lines</item>
<item name="android:textAlignment" tools:ignore="NewApi">viewStart</item>
<item name="android:textAppearance">?attr/textAppearanceBody2</item>
<item name="android:textColor">?attr/colorSurface</item>
<item name="android:paddingTop">@dimen/design_snackbar_padding_vertical</item>
<item name="android:paddingBottom">@dimen/design_snackbar_padding_vertical</item>
<item name="android:paddingLeft">@dimen/design_snackbar_padding_horizontal</item>
<item name="android:paddingRight">@dimen/design_snackbar_padding_horizontal</item>
</style>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import android.os.Build.VERSION;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.support.v7.widget.AppCompatAutoCompleteTextView;
import androidx.appcompat.widget.AppCompatAutoCompleteTextView;
import androidx.appcompat.widget.ListPopupWindow;
import android.util.AttributeSet;
import android.view.View;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.appcompat.app.AppCompatViewInflater;
import android.support.v7.widget.AppCompatAutoCompleteTextView;
import androidx.appcompat.widget.AppCompatAutoCompleteTextView;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.appcompat.widget.AppCompatRadioButton;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<item name="sliderStyle">@style/Widget.MaterialComponents.Slider</item>
<item name="snackbarStyle">@style/Widget.MaterialComponents.Snackbar</item>
<item name="snackbarButtonStyle">@style/Widget.MaterialComponents.Button.TextButton.Snackbar</item>
<item name="snackbarTextViewStyle">@style/Widget.MaterialComponents.Snackbar.TextView</item>
<item name="tabStyle">@style/Widget.MaterialComponents.TabLayout</item>
<item name="textInputStyle">@style/Widget.MaterialComponents.TextInputLayout.FilledBox</item>

Expand Down Expand Up @@ -138,6 +139,7 @@
<item name="sliderStyle">@style/Widget.MaterialComponents.Slider</item>
<item name="snackbarStyle">@style/Widget.MaterialComponents.Snackbar</item>
<item name="snackbarButtonStyle">@style/Widget.MaterialComponents.Button.TextButton.Snackbar</item>
<item name="snackbarTextViewStyle">@style/Widget.MaterialComponents.Snackbar.TextView</item>
<item name="tabStyle">@style/Widget.MaterialComponents.TabLayout</item>
<item name="textInputStyle">@style/Widget.MaterialComponents.TextInputLayout.FilledBox</item>

Expand Down

0 comments on commit b997092

Please sign in to comment.