Skip to content

Commit 51873bb

Browse files
imhappikendrickumstattd
authored andcommitted
[DockedToolbar] Create DockedToolbarLayout
PiperOrigin-RevId: 723677429
1 parent 76936c4 commit 51873bb

File tree

7 files changed

+206
-0
lines changed

7 files changed

+206
-0
lines changed

lib/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def srcDirs = [
6161
'com/google/android/material/datepicker',
6262
'com/google/android/material/dialog',
6363
'com/google/android/material/divider',
64+
'com/google/android/material/dockedtoolbar',
6465
'com/google/android/material/drawable',
6566
'com/google/android/material/elevation',
6667
'com/google/android/material/expandable',
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2025 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+
package com.google.android.material.dockedtoolbar;
17+
18+
import com.google.android.material.R;
19+
20+
import static com.google.android.material.theme.overlay.MaterialThemeOverlay.wrap;
21+
22+
import android.content.Context;
23+
import android.content.res.ColorStateList;
24+
import androidx.appcompat.widget.TintTypedArray;
25+
import android.util.AttributeSet;
26+
import android.view.View;
27+
import android.widget.FrameLayout;
28+
import androidx.annotation.AttrRes;
29+
import androidx.annotation.ColorInt;
30+
import androidx.annotation.NonNull;
31+
import androidx.annotation.Nullable;
32+
import androidx.annotation.StyleRes;
33+
import com.google.android.material.internal.ThemeEnforcement;
34+
import com.google.android.material.shape.MaterialShapeDrawable;
35+
import com.google.android.material.shape.ShapeAppearanceModel;
36+
37+
/**
38+
* Provides an implementation of a docked toolbar.
39+
*
40+
* <p>Docked toolbars are pinned to the top or bottom and can be used to display contextual actions
41+
* relevant to the body content or the specific page.
42+
*
43+
* <p>The docked toolbar supports a custom {@link android.view.ViewGroup} child, and provides docked
44+
* toolbar styling such as background color, shape, etc.
45+
*/
46+
public class DockedToolbarLayout extends FrameLayout {
47+
48+
private static final String TAG = DockedToolbarLayout.class.getSimpleName();
49+
private static final int DEF_STYLE_RES = R.style.Widget_Material3_DockedToolbar;
50+
51+
public DockedToolbarLayout(@NonNull Context context) {
52+
this(context, null);
53+
}
54+
55+
public DockedToolbarLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
56+
this(context, attrs, R.attr.dockedToolbarStyle);
57+
}
58+
59+
public DockedToolbarLayout(
60+
@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
61+
this(context, attrs, defStyleAttr, DEF_STYLE_RES);
62+
}
63+
64+
public DockedToolbarLayout(
65+
@NonNull Context context,
66+
@Nullable AttributeSet attrs,
67+
@AttrRes int defStyleAttr,
68+
@StyleRes int defStyleRes) {
69+
super(wrap(context, attrs, defStyleAttr, defStyleRes), attrs, defStyleAttr);
70+
71+
// Ensure we are using the correctly themed context rather than the context that was passed in.
72+
context = getContext();
73+
74+
/* Custom attributes */
75+
TintTypedArray attributes =
76+
ThemeEnforcement.obtainTintedStyledAttributes(
77+
context, attrs, R.styleable.DockedToolbar, defStyleAttr, defStyleRes);
78+
79+
// Add a MaterialShapeDrawable as a background that supports tinting in every API level.
80+
if (attributes.hasValue(R.styleable.DockedToolbar_backgroundTint)) {
81+
@ColorInt
82+
int backgroundColor = attributes.getColor(R.styleable.DockedToolbar_backgroundTint, 0);
83+
84+
ShapeAppearanceModel shapeAppearanceModel =
85+
ShapeAppearanceModel.builder(context, attrs, defStyleAttr, defStyleRes).build();
86+
MaterialShapeDrawable materialShapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
87+
materialShapeDrawable.setFillColor(ColorStateList.valueOf(backgroundColor));
88+
89+
setBackground(materialShapeDrawable);
90+
}
91+
92+
setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
93+
attributes.recycle();
94+
}
95+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright (C) 2025 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<resources>
18+
<public name="dockedToolbarStyle" type="attr"/>
19+
<public name="Widget.Material3.DockedToolbar" type="style"/>
20+
</resources>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright (C) 2025 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<resources>
18+
<!-- Style to use for DockedToolbars in this theme. -->
19+
<attr name="dockedToolbarStyle" format="reference"/>
20+
21+
<declare-styleable name="DockedToolbar">
22+
<!-- Color of the docked toolbar container. -->
23+
<attr name="backgroundTint"/>
24+
</declare-styleable>
25+
</resources>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright (C) 2025 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<resources>
18+
<!-- Base style for Docked Toolbar. -->
19+
<style name="Base.Widget.Material3.DockedToolbar" parent="">
20+
<item name="enforceMaterialTheme">true</item>
21+
<item name="android:minHeight">@dimen/m3_comp_toolbar_docked_container_height</item>
22+
<item name="backgroundTint">@macro/m3_comp_toolbar_docked_container_color</item>
23+
<item name="shapeAppearance">@style/ShapeAppearance.M3.Comp.Toolbar.Docked.Container.Shape</item>
24+
<item name="android:paddingStart">@dimen/m3_comp_toolbar_docked_container_leading_space</item>
25+
<item name="android:paddingEnd">@dimen/m3_comp_toolbar_docked_container_trailing_space</item>
26+
</style>
27+
28+
<!-- Style for Docked Toolbar. -->
29+
<style name="Widget.Material3.DockedToolbar" parent="Base.Widget.Material3.DockedToolbar"/>
30+
31+
</resources>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright (C) 2025 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<!-- AUTOGENERATED FILE. DO NOT MODIFY. -->
18+
<!-- Version: 13.2.0 -->
19+
20+
<resources>
21+
22+
<!-- Generated from token set (md.comp.toolbar.docked) in context (platform=android, audience=3p). -->
23+
<!-- Group: Color -->
24+
<macro name="m3_comp_toolbar_docked_container_color">?attr/colorSurfaceContainer</macro>
25+
<!-- Group: Size -->
26+
<dimen name="m3_comp_toolbar_docked_container_height">64dp</dimen>
27+
<dimen name="m3_comp_toolbar_docked_container_leading_space">16dp</dimen>
28+
<dimen name="m3_comp_toolbar_docked_container_trailing_space">16dp</dimen>
29+
<!-- Group: Shape -->
30+
<style name="ShapeAppearance.M3.Comp.Toolbar.Docked.Container.Shape" parent="ShapeAppearance.M3.Sys.Shape.Corner.None"/>
31+
32+
</resources>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
<item name="collapsingToolbarLayoutMediumStyle">@style/Widget.Material3.CollapsingToolbar.Medium</item>
126126
<item name="collapsingToolbarLayoutLargeStyle">@style/Widget.Material3.CollapsingToolbar.Large</item>
127127
<item name="drawerLayoutStyle">@style/Widget.Material3.DrawerLayout</item>
128+
<item name="dockedToolbarStyle">@style/Widget.Material3.DockedToolbar</item>
128129
<item name="extendedFloatingActionButtonStyle">?attr/extendedFloatingActionButtonPrimaryStyle</item>
129130
<item name="extendedFloatingActionButtonPrimaryStyle">@style/Widget.Material3.ExtendedFloatingActionButton.Icon.Primary</item>
130131
<item name="extendedFloatingActionButtonSecondaryStyle">@style/Widget.Material3.ExtendedFloatingActionButton.Icon.Secondary</item>
@@ -432,6 +433,7 @@
432433
<item name="collapsingToolbarLayoutMediumStyle">@style/Widget.Material3.CollapsingToolbar.Medium</item>
433434
<item name="collapsingToolbarLayoutLargeStyle">@style/Widget.Material3.CollapsingToolbar.Large</item>
434435
<item name="drawerLayoutStyle">@style/Widget.Material3.DrawerLayout</item>
436+
<item name="dockedToolbarStyle">@style/Widget.Material3.DockedToolbar</item>
435437
<item name="extendedFloatingActionButtonStyle">?attr/extendedFloatingActionButtonPrimaryStyle</item>
436438
<item name="extendedFloatingActionButtonPrimaryStyle">@style/Widget.Material3.ExtendedFloatingActionButton.Icon.Primary</item>
437439
<item name="extendedFloatingActionButtonSecondaryStyle">@style/Widget.Material3.ExtendedFloatingActionButton.Icon.Secondary</item>

0 commit comments

Comments
 (0)