Skip to content

Commit

Permalink
Icon size and animation issue (#33)
Browse files Browse the repository at this point in the history
* Add default unselectedColor

* Add iconSize attribute

* Fix animation for HorizontaMenuItem on portrait
  • Loading branch information
ismaeldivita authored Mar 22, 2020
1 parent 9a4459c commit 72d3331
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,7 @@ class ChipNavigationBar @JvmOverloads constructor(
else -> MenuOrientation.HORIZONTAL
}

val textAppearance = a.getResourceId(R.styleable.ChipNavigationBar_cnb_textAppearance, -1)
val radius = a.getDimension(R.styleable.ChipNavigationBar_cnb_radius, Float.MAX_VALUE)
val badgeColor = a.getColor(
R.styleable.ChipNavigationBar_cnb_badgeColor,
ContextCompat.getColor(context, R.color.cnb_default_badge_color)
)
val unselectedColor = a.getColor(
R.styleable.ChipNavigationBar_cnb_unselectedColor,
context.getValueFromAttr(R.attr.colorButtonNormal)
)
menuStyle = MenuStyle(
badgeColor = badgeColor,
unselectedColor = unselectedColor,
radius = radius,
textAppearance = textAppearance.takeIf { it > 0 }
)
menuStyle = MenuStyle(context, a)

a.recycle()
setMenuOrientation(orientation)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
package com.ismaeldivita.chipnavigation.model

import android.content.Context
import android.content.res.TypedArray
import androidx.annotation.ColorInt
import androidx.annotation.IdRes
import androidx.core.content.ContextCompat
import com.ismaeldivita.chipnavigation.R

class MenuStyle(
@ColorInt val badgeColor: Int,
@ColorInt val unselectedColor: Int,
@IdRes val textAppearance: Int?,
class MenuStyle(context: Context, attr: TypedArray) {

@ColorInt val badgeColor: Int
@ColorInt val unselectedColor: Int
@IdRes val textAppearance: Int?
val radius: Float
)
val iconSize: Int

init {
textAppearance = attr.getResourceId(R.styleable.ChipNavigationBar_cnb_textAppearance, -1)
.takeIf { it > 0 }

radius = attr.getDimension(R.styleable.ChipNavigationBar_cnb_radius, Float.MAX_VALUE)
badgeColor = attr.getColor(R.styleable.ChipNavigationBar_cnb_badgeColor,
ContextCompat.getColor(context, R.color.cnb_default_badge_color)
)
unselectedColor = attr.getColor(
R.styleable.ChipNavigationBar_cnb_unselectedColor,
ContextCompat.getColor(context, R.color.cnb_default_unselected_color)
)
iconSize = attr.getDimension(
R.styleable.ChipNavigationBar_cnb_iconSize,
context.resources.getDimension(R.dimen.cnb_icon_size)
).toInt()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.widget.LinearLayout
import android.widget.LinearLayout.LayoutParams
Expand Down Expand Up @@ -44,13 +45,16 @@ internal class HorizontalMenuItemView @JvmOverloads constructor(
unselectedColor = item.menuStyle.unselectedColor
)

icon.layoutParams.width = item.menuStyle.iconSize
icon.layoutParams.height = item.menuStyle.iconSize
icon.setImageResource(item.icon)
icon.setBadgeColor(item.menuStyle.badgeColor)
icon.setColorStateListAnimator(
color = item.iconColor,
unselectedColor = item.menuStyle.unselectedColor,
mode = item.tintMode
)

val containerBackground = GradientDrawable().apply {
cornerRadius = item.menuStyle.radius
setTint(item.backgroundColor)
Expand Down Expand Up @@ -82,6 +86,7 @@ internal class HorizontalMenuItemView @JvmOverloads constructor(
override fun setSelected(selected: Boolean) {
super.setSelected(selected)
val isPortrait = context.resources.configuration.orientation == ORIENTATION_PORTRAIT
val childCollapse = (parent as ViewGroup).childCount > 3

if (selected) {
/** Hack to fix the ripple issue before a scene transition on SDKs < P */
Expand All @@ -90,7 +95,7 @@ internal class HorizontalMenuItemView @JvmOverloads constructor(
container.visibility = View.VISIBLE

beginDelayedTransitionOnParent()
if (isPortrait) {
if (isPortrait && childCollapse) {

updateLayoutParams<LinearLayout.LayoutParams> {
width = WRAP_CONTENT
Expand All @@ -99,7 +104,7 @@ internal class HorizontalMenuItemView @JvmOverloads constructor(
}
title.visibility = View.VISIBLE
} else {
if (isPortrait) {
if (isPortrait && childCollapse) {
updateLayoutParams<LinearLayout.LayoutParams> {
width = 0
weight = 1F
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ internal class VerticalMenuItemView @JvmOverloads constructor(
color = item.textColor,
unselectedColor = item.menuStyle.unselectedColor
)

icon.layoutParams.width = item.menuStyle.iconSize
icon.layoutParams.height = item.menuStyle.iconSize
icon.setBadgeColor(item.menuStyle.badgeColor)
icon.setImageResource(item.icon)
icon.setColorStateListAnimator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

<com.ismaeldivita.chipnavigation.view.BadgeImageView
android:id="@+id/cnb_item_icon"
android:layout_width="@dimen/cnb_icon_size"
android:layout_height="@dimen/cnb_icon_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|start"
android:duplicateParentState="true"
tools:src="@android:drawable/btn_star_big_off" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

<com.ismaeldivita.chipnavigation.view.BadgeImageView
android:id="@+id/cnb_item_icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/cnb_double_space"
android:duplicateParentState="true"
tools:src="@android:drawable/btn_star_big_off"
Expand Down
1 change: 1 addition & 0 deletions chip-navigation-bar/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<attr name="cnb_unselectedColor" format="color" />
<attr name="cnb_radius" format="dimension" />
<attr name="cnb_textAppearance" format="reference" />
<attr name="cnb_iconSize" format="dimension" />
</declare-styleable>

<declare-styleable name="ChipMenuItem">
Expand Down
1 change: 1 addition & 0 deletions chip-navigation-bar/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="cnb_default_badge_color">#F44336</color>
<color name="cnb_default_unselected_color">#696969</color>
<public/>
</resources>

0 comments on commit 72d3331

Please sign in to comment.