Skip to content

Commit

Permalink
Merge pull request #2756 from mikepenz/feature/material_3
Browse files Browse the repository at this point in the history
Material 3 themeing
  • Loading branch information
mikepenz authored Oct 29, 2021
2 parents d3194c9 + 47d54c2 commit 5ab67d6
Show file tree
Hide file tree
Showing 30 changed files with 143 additions and 143 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@

## Latest releases 🛠

- Kotlin && New | [v8.4.5](https://github.com/mikepenz/MaterialDrawer/tree/v8.4.5)
- Kotlin | [v7.0.0](https://github.com/mikepenz/MaterialDrawer/tree/v7.0.0) | (Builder approach like v6.x)
- Kotlin && Material 3 | [v9.0.0-a01](https://github.com/mikepenz/MaterialDrawer/tree/v9.0.0-a01)
- Kotlin | [v8.4.5](https://github.com/mikepenz/MaterialDrawer/tree/v8.4.5)
- Java && AndroidX | [v6.1.2](https://github.com/mikepenz/MaterialDrawer/tree/v6.1.2)
- Java && AppCompat | [v6.0.9](https://github.com/mikepenz/MaterialDrawer/tree/v6.0.9)

Expand All @@ -64,7 +64,7 @@ implementation "com.mikepenz:materialdrawer:${lastestMaterialDrawerRelease}"
implementation "androidx.appcompat:appcompat:${versions.appcompat}"
implementation "androidx.recyclerview:recyclerview:${versions.recyclerView}"
implementation "androidx.annotation:annotation:${versions.annotation}"
implementation "com.google.android.material:material:${versions.material}"
implementation "com.google.android.material:material:1.5.0-alpha05" // requires at least 1.5.0-x
implementation "androidx.constraintlayout:constraintlayout:${versions.constraintLayout}"
// Add for NavController support
Expand Down Expand Up @@ -100,7 +100,7 @@ The `MaterialDrawerSliderView` has to be provided as child of the `DrawerLayout`
### 3. Add the `DrawerStyle` to your theme

```xml
<style name="SampleApp.DayNight" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<style name="SampleApp.DayNight" parent="Theme.Material3.DayNight.NoActionBar">
...
<item name="materialDrawerStyle">@style/Widget.MaterialDrawerStyle</item>
<item name="materialDrawerHeaderStyle">@style/Widget.MaterialDrawerHeaderStyle</item>
Expand Down Expand Up @@ -273,12 +273,12 @@ Create your custom style. If you don't need a custom theme see the next section,
<item name="materialDrawerInsetForeground">#4000</item>
<!-- MaterialDrawer specific values -->
<item name="materialDrawerBackground">?colorSurface</item>
<item name="materialDrawerPrimaryText">?android:textColorPrimary</item>
<item name="materialDrawerPrimaryIcon">?android:textColorSecondary</item>
<item name="materialDrawerSecondaryText">?android:textColorSecondary</item>
<item name="materialDrawerSecondaryIcon">?android:textColorSecondary</item>
<item name="materialDrawerDividerColor">?android:textColorHint</item>
<item name="materialDrawerSelectedBackgroundColor">?colorPrimary</item>
<item name="materialDrawerPrimaryText">@color/color_drawer_item_text</item>
<item name="materialDrawerPrimaryIcon">@color/color_drawer_item_text</item>
<item name="materialDrawerSecondaryText">@color/color_drawer_item_text</item>
<item name="materialDrawerSecondaryIcon">@color/color_drawer_item_text</item>
<item name="materialDrawerDividerColor">?colorOutline</item>
<item name="materialDrawerSelectedBackgroundColor">?colorSecondaryContainer</item>
</style>

// define a custom header style
Expand All @@ -289,7 +289,7 @@ Create your custom style. If you don't need a custom theme see the next section,
</style>

// define the custom styles for the theme
<style name="SampleApp" parent="Theme.MaterialComponents.Light.NoActionBar">
<style name="SampleApp" parent="Theme.Material3.Light.NoActionBar">
...
<item name="materialDrawerStyle">@style/Widget.MaterialDrawerStyleCustom</item>
<item name="materialDrawerHeaderStyle">@style/Widget.MaterialDrawerHeaderStyleCustom</item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.widget.ImageView
import androidx.multidex.MultiDexApplication
import coil.clear
import coil.load
import com.google.android.material.color.DynamicColors
import com.mikepenz.iconics.Iconics
import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.iconics.utils.backgroundColorRes
Expand All @@ -24,6 +25,8 @@ class CustomApplication : MultiDexApplication() {

Iconics.init(this)

DynamicColors.applyToActivitiesIfAvailable(this)

//initialize and create the image loader logic
/*
DrawerImageLoader.init(object : AbstractDrawerImageLoader() {
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/layout/activity_mini_drawer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.MaterialComponents.Dark" />
android:theme="@style/ThemeOverlay.Material3.Dark" />

<FrameLayout
android:layout_width="match_parent"
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/layout/activity_sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.MaterialComponents.Dark"
android:theme="@style/ThemeOverlay.Material3.Dark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/layout/activity_sample_crossfader.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.MaterialComponents.Dark"
android:theme="@style/ThemeOverlay.Material3.Dark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/layout/activity_sample_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.MaterialComponents.Dark" />
android:theme="@style/ThemeOverlay.Material3.Dark" />

<FrameLayout
android:id="@+id/fragment_container"
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/layout/activity_sample_fullscreen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.MaterialComponents.Dark"
android:theme="@style/ThemeOverlay.Material3.Dark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/layout/activity_sample_nav.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.MaterialComponents.Dark"
android:theme="@style/ThemeOverlay.Material3.Dark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Expand Down
27 changes: 4 additions & 23 deletions app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
<resources>

<!-- LIGHT -->
<style name="SampleApp.DayNight" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<style name="SampleApp.DayNight" parent="Theme.Material3.DayNight.NoActionBar">
<!-- ...and here we setting appcompat’s color theming attrs -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>

<item name="materialDrawerStyle">@style/Widget.MaterialDrawerStyle</item>
<item name="materialDrawerHeaderStyle">@style/Widget.MaterialDrawerHeaderStyle</item>

Expand All @@ -17,12 +13,7 @@
<item name="windowActionModeOverlay">true</item>
</style>

<style name="SampleApp.DayNight.Compact" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- ...and here we setting appcompat’s color theming attrs -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>

<style name="SampleApp.DayNight.Compact" parent="Theme.Material3.Light.NoActionBar">
<item name="materialDrawerStyle">@style/Widget.MaterialDrawerStyle</item>
<item name="materialDrawerHeaderStyle">@style/Widget.MaterialDrawerHeaderStyleCustom</item>

Expand All @@ -32,12 +23,7 @@
<item name="windowActionModeOverlay">true</item>
</style>

<style name="SampleApp.DayNight.Persistent" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- ...and here we setting appcompat’s color theming attrs -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>

<style name="SampleApp.DayNight.Persistent" parent="Theme.Material3.Light.NoActionBar">
<item name="materialDrawerStyle">@style/Widget.MaterialDrawerStyle</item>
<item name="materialDrawerHeaderStyle">@style/Widget.MaterialDrawerHeaderStylePersistent</item>

Expand All @@ -48,10 +34,5 @@
</style>


<style name="SampleApp.DayNight.DarkActionBar" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- ...and here we setting appcompat’s color theming attrs -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="SampleApp.DayNight.DarkActionBar" parent="Theme.Material3.DayNight" />
</resources>
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ buildscript {
multiDex : '2.0.1',
annotation : '1.1.0',
recyclerView : '1.2.1',
core : '1.6.0',
material : '1.4.0',
core : '1.7.0',
material : '1.5.0-alpha05',
appcompat : '1.3.1',
drawerlayout : '1.1.1',
constraintLayout : '2.1.1',
Expand All @@ -40,7 +40,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.2.0-alpha02'
classpath 'com.android.tools.build:gradle:7.2.0-alpha03'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:${versions.navigation}"
classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:${versions.detekt}"
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-rc-1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package com.mikepenz.materialdrawer.model
import android.content.Context
import android.content.res.ColorStateList
import android.view.View
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import com.google.android.material.shape.ShapeAppearanceModel
import com.mikepenz.materialdrawer.R
import com.mikepenz.materialdrawer.holder.ImageHolder
import com.mikepenz.materialdrawer.holder.StringHolder
import com.mikepenz.materialdrawer.model.interfaces.Describable
Expand Down Expand Up @@ -72,6 +75,14 @@ abstract class BaseDescribeableDrawerItem<T, VH : BaseViewHolder> : BaseDrawerIt
ImageHolder.applyMultiIconTo(icon, selectedIcon, iconColor, isIconTinted, viewHolder.icon)
}

if (viewHolder.icon.isVisible) {
viewHolder.name.updatePadding(left = 0)
viewHolder.description.updatePadding(left = 0)
} else {
viewHolder.name.updatePadding(left = ctx.resources.getDimensionPixelSize(R.dimen.material_drawer_item_primary_icon_padding_left))
viewHolder.description.updatePadding(left = ctx.resources.getDimensionPixelSize(R.dimen.material_drawer_item_primary_icon_padding_left))
}

//for android API 17 --> Padding not applied via xml
viewHolder.view.setDrawerVerticalPadding(level)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.LayoutRes
import androidx.core.view.ViewCompat
import androidx.recyclerview.widget.RecyclerView
import com.mikepenz.materialdrawer.R
import com.mikepenz.materialdrawer.holder.BadgeStyle
import com.mikepenz.materialdrawer.holder.ImageHolder
import com.mikepenz.materialdrawer.holder.StringHolder
import com.mikepenz.materialdrawer.model.interfaces.*
import com.mikepenz.materialdrawer.util.getPrimaryDrawerIconColor
import com.mikepenz.materialdrawer.util.getSelectableBackground
import com.mikepenz.materialdrawer.util.setDrawerVerticalPadding
import com.mikepenz.materialdrawer.util.themeDrawerItem

/**
* Describes a [IProfile] being used with the [com.mikepenz.materialdrawer.widget.AccountHeaderView]
Expand Down Expand Up @@ -68,8 +67,10 @@ open class ProfileSettingDrawerItem : AbstractDrawerItem<ProfileSettingDrawerIte
val color = this.textColor ?: getColor(ctx)
val iconColor = this.iconColor ?: ctx.getPrimaryDrawerIconColor()
val descriptionColor = this.descriptionTextColor ?: getColor(ctx)
val shapeAppearanceModel = getShapeAppearanceModel(ctx)

ViewCompat.setBackground(holder.view, ctx.getSelectableBackground(selectedColor, isSelectedBackgroundAnimated))
//set the background for the item
themeDrawerItem(ctx, holder.view, selectedColor, isSelectedBackgroundAnimated, shapeAppearanceModel, isSelected = isSelected)

StringHolder.applyTo(this.name, holder.name)
holder.name.setTextColor(color)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ fun themeDrawerItem(
val selected: Drawable
val unselected: Drawable

// Material 2.0 styling
// Material 3.0 styling
val paddingTopBottom = ctx.resources.getDimensionPixelSize(paddingTopBottomRes)
val paddingStart = ctx.resources.getDimensionPixelSize(paddingStartRes)
val paddingEnd = ctx.resources.getDimensionPixelSize(paddingEndRes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import android.util.TypedValue
import androidx.annotation.*
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.ColorUtils
import com.mikepenz.materialdrawer.R


Expand All @@ -36,32 +35,33 @@ internal fun Context.getSecondaryDrawerIconColor(): ColorStateList {
return createDrawerItemColorStateList(this, R.styleable.MaterialDrawerSliderView_materialDrawerSecondaryIcon)!!
}

fun createDrawerItemColorStateList(ctx: Context, @StyleableRes styleableRes: Int, @StyleableRes selectedStyleable: Int = R.styleable.MaterialDrawerSliderView_materialDrawerSelectedBackgroundColor): ColorStateList? {
fun createDrawerItemColorStateList(ctx: Context, @StyleableRes styleableRes: Int): ColorStateList? {
val a = ctx.obtainStyledAttributes(null, R.styleable.MaterialDrawerSliderView, R.attr.materialDrawerStyle, R.style.Widget_MaterialDrawerStyle)
val baseColor = a.getColorStateList(styleableRes) ?: return null
val selectedColor = a.getColor(selectedStyleable, ctx.getThemeColor(R.attr.colorPrimary))
val baseColor = a.getColorStateList(styleableRes)
a.recycle()

val defaultColor = baseColor.defaultColor
return ColorStateList(
arrayOf(DISABLED_STATE_SET, CHECKED_STATE_SET, SELECTED_STATE_SET, EMPTY_STATE_SET),
intArrayOf(baseColor.getColorForState(DISABLED_STATE_SET, defaultColor), selectedColor, selectedColor, defaultColor)
)
return baseColor
}

@ColorInt
fun Context.getDividerColor(): Int {
return resolveStyledValue {
it.getColor(R.styleable.MaterialDrawerSliderView_materialDrawerDividerColor, getThemeColor(R.attr.materialDrawerDividerColor, getSupportColor(R.color.material_drawer_divider)))
it.getColor(
R.styleable.MaterialDrawerSliderView_materialDrawerDividerColor,
getThemeColor(R.attr.materialDrawerDividerColor, getSupportColor(R.color.material_drawer_divider))
)
}
}

@ColorInt
internal fun Context.getSelectedColor(): Int {
val color = resolveStyledValue {
it.getColor(R.styleable.MaterialDrawerSliderView_materialDrawerSelectedBackgroundColor, getThemeColor(R.attr.materialDrawerSelectedBackgroundColor, getSupportColor(R.color.material_drawer_selected)))
it.getColor(
R.styleable.MaterialDrawerSliderView_materialDrawerSelectedBackgroundColor,
getThemeColor(R.attr.materialDrawerSelectedBackgroundColor, getSupportColor(R.color.material_drawer_selected))
)
}
return ColorUtils.setAlphaComponent(color, (255 * getSupportFloat(R.dimen.material_drawer_selected_background_alpha)).toInt())
return color
}

internal fun Context.getHeaderSelectionTextColor(): ColorStateList {
Expand All @@ -80,7 +80,12 @@ internal fun <T> Context.resolveStyledHeaderValue(resolver: (typedArray: TypedAr
return resolveStyledValue(R.styleable.AccountHeaderView, R.attr.materialDrawerHeaderStyle, R.style.Widget_MaterialDrawerHeaderStyle, resolver)
}

internal fun <T> Context.resolveStyledValue(attrs: IntArray = R.styleable.MaterialDrawerSliderView, defStyleAttr: Int = R.attr.materialDrawerStyle, defStyleRes: Int = R.style.Widget_MaterialDrawerStyle, resolver: (typedArray: TypedArray) -> T): T {
internal fun <T> Context.resolveStyledValue(
attrs: IntArray = R.styleable.MaterialDrawerSliderView,
defStyleAttr: Int = R.attr.materialDrawerStyle,
defStyleRes: Int = R.style.Widget_MaterialDrawerStyle,
resolver: (typedArray: TypedArray) -> T
): T {
val a = obtainStyledAttributes(null, attrs, defStyleAttr, defStyleRes)
val value = resolver.invoke(a)
a.recycle()
Expand Down
5 changes: 5 additions & 0 deletions materialdrawer/src/main/res/color/color_drawer_item_text.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?colorOnSecondaryContainer" android:state_selected="true" />
<item android:color="?colorOnSurfaceVariant" />
</selector>
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
android:id="@+id/material_drawer_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="@dimen/material_drawer_vertical_item_padding"
android:layout_marginTop="@dimen/material_drawer_padding"
android:layout_marginRight="@dimen/material_drawer_vertical_item_padding"
android:layout_marginBottom="@dimen/material_drawer_padding" />
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
android:id="@+id/material_drawer_icon"
android:layout_width="@dimen/material_drawer_item_primary_icon"
android:layout_height="@dimen/material_drawer_item_primary"
android:paddingStart="0dp"
android:paddingLeft="0dp"
android:paddingStart="@dimen/material_drawer_item_primary_icon_padding_left"
android:paddingLeft="@dimen/material_drawer_item_primary_icon_padding_left"
android:paddingTop="@dimen/material_drawer_item_primary_icon_padding"
android:paddingEnd="@dimen/material_drawer_item_primary_icon_padding_right"
android:paddingRight="@dimen/material_drawer_item_primary_icon_padding_right"
Expand Down Expand Up @@ -69,6 +69,8 @@
android:layout_height="16dp"
android:layout_marginStart="@dimen/material_drawer_padding"
android:layout_marginLeft="@dimen/material_drawer_padding"
android:layout_marginEnd="@dimen/material_drawer_item_primary_icon_padding"
android:layout_marginRight="@dimen/material_drawer_item_primary_icon_padding"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/material_drawer_barrier"
Expand Down
Loading

0 comments on commit 5ab67d6

Please sign in to comment.