@@ -2,6 +2,7 @@ package com.shreyaspatil.material.navigationview
22
33import android.content.Context
44import android.graphics.drawable.Drawable
5+ import android.os.Build
56import android.util.AttributeSet
67import android.util.Log
78import androidx.appcompat.content.res.AppCompatResources
@@ -19,7 +20,7 @@ class MaterialNavigationView @JvmOverloads constructor(
1920 defStyleAttr : Int = R .style.Widget_NavigationView
2021) : NavigationView(context, attrs, defStyleAttr) {
2122
22- private var itemStyle: Int = ITEM_STYLE_ROUND_RIGHT
23+ private var itemStyle: Int = ITEM_STYLE_DEFAULT
2324
2425 init {
2526 // Init itemStyle
@@ -31,7 +32,7 @@ class MaterialNavigationView @JvmOverloads constructor(
3132
3233 itemStyle = a.getInteger(
3334 R .styleable.MaterialNavigationView_itemStyle ,
34- ITEM_STYLE_ROUND_RECTANGLE
35+ ITEM_STYLE_DEFAULT
3536 )
3637
3738 // Recycler it.
@@ -48,7 +49,6 @@ class MaterialNavigationView @JvmOverloads constructor(
4849 * @see ITEM_STYLE_ROUND_RECTANGLE (2)
4950 */
5051 fun getItemStyle (): Int {
51- // Obtain Styled Attribute
5252 return itemStyle
5353 }
5454
@@ -59,7 +59,10 @@ class MaterialNavigationView @JvmOverloads constructor(
5959 * @see ITEM_STYLE_ROUND_RECTANGLE (2)
6060 */
6161 fun setItemStyle (itemStyle : Int ) {
62- if (itemStyle == ITEM_STYLE_ROUND_RIGHT || itemStyle == ITEM_STYLE_ROUND_RECTANGLE ) {
62+ if (itemStyle == ITEM_STYLE_ROUND_RIGHT ||
63+ itemStyle == ITEM_STYLE_ROUND_RECTANGLE ||
64+ itemStyle == ITEM_STYLE_DEFAULT
65+ ) {
6366 this .itemStyle = itemStyle
6467 itemBackground = navigationItemBackground()
6568 } else {
@@ -70,25 +73,32 @@ class MaterialNavigationView @JvmOverloads constructor(
7073 private fun navigationItemBackground (): Drawable ? {
7174 // Set Resource
7275 val resource = when (itemStyle) {
76+ ITEM_STYLE_DEFAULT -> R .drawable.navigation_item_background_default
7377 ITEM_STYLE_ROUND_RIGHT -> R .drawable.navigation_item_background_rounded_right
7478 else -> R .drawable.navigation_item_background_rounded_rect
7579 }
7680
77- var background =
78- AppCompatResources .getDrawable(context, resource)
81+ var background = AppCompatResources .getDrawable(context, resource)
7982 if (background != null ) {
8083 val tint = AppCompatResources .getColorStateList(
8184 context, R .color.navigation_item_background_tint
8285 )
86+
8387 background = DrawableCompat .wrap(background.mutate())
84- background.setTintList(tint)
88+
89+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
90+ background.setTintList(tint)
91+ } else {
92+ DrawableCompat .setTintList(background, tint)
93+ }
8594 }
8695
8796 return background
8897 }
8998
9099 companion object {
91- const val ITEM_STYLE_ROUND_RIGHT = 1
92- const val ITEM_STYLE_ROUND_RECTANGLE = 2
100+ const val ITEM_STYLE_DEFAULT = 1
101+ const val ITEM_STYLE_ROUND_RIGHT = 2
102+ const val ITEM_STYLE_ROUND_RECTANGLE = 3
93103 }
94104}
0 commit comments