Skip to content
This repository has been archived by the owner on Sep 3, 2023. It is now read-only.

Commit

Permalink
Add md_color_widget attribute which applies to checkbox prompts and s…
Browse files Browse the repository at this point in the history
…ingle/multichoice list controls. Resolves #1761.
  • Loading branch information
afollestad committed Mar 23, 2019
1 parent 2b12f36 commit 488d915
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import android.view.View.MeasureSpec.EXACTLY
import android.view.View.MeasureSpec.UNSPECIFIED
import android.view.View.MeasureSpec.makeMeasureSpec
import androidx.viewpager.widget.ViewPager
import com.afollestad.materialdialogs.utils.MDUtil.ifNotZero

internal class WrapContentViewPager(
context: Context,
Expand Down Expand Up @@ -51,8 +52,8 @@ internal class WrapContentViewPager(
if (maxChildHeight > maxAllowedHeightFromParent) {
maxChildHeight = maxAllowedHeightFromParent
}
if (maxChildHeight != 0) {
newHeightSpec = makeMeasureSpec(maxChildHeight, EXACTLY)
maxChildHeight.ifNotZero {
newHeightSpec = makeMeasureSpec(it, EXACTLY)
}

super.onMeasure(widthMeasureSpec, newHeightSpec)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ import android.view.View
import android.widget.CheckBox
import androidx.annotation.CheckResult
import androidx.annotation.StringRes
import androidx.core.widget.CompoundButtonCompat
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.R
import com.afollestad.materialdialogs.assertOneSet
import com.afollestad.materialdialogs.utils.MDUtil.ifNotZero
import com.afollestad.materialdialogs.utils.MDUtil.maybeSetTextColor
import com.afollestad.materialdialogs.utils.MDUtil.resolveString
import com.afollestad.materialdialogs.utils.createColorSelector
import com.afollestad.materialdialogs.utils.resolveColor

typealias BooleanCallback = ((Boolean) -> Unit)?

Expand Down Expand Up @@ -57,6 +61,13 @@ typealias BooleanCallback = ((Boolean) -> Unit)?
}
maybeSetTextColor(windowContext, R.attr.md_color_content)
bodyFont?.let { this.typeface = it }

resolveColor(attr = R.attr.md_color_widget).ifNotZero {
CompoundButtonCompat.setButtonTintList(
this,
createColorSelector(checked = it)
)
}
}
return this
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import androidx.appcompat.widget.AppCompatButton
import com.afollestad.materialdialogs.R
import com.afollestad.materialdialogs.Theme.Companion.inferTheme
import com.afollestad.materialdialogs.Theme.LIGHT
import com.afollestad.materialdialogs.utils.MDUtil.ifNotZero
import com.afollestad.materialdialogs.utils.MDUtil.resolveColor
import com.afollestad.materialdialogs.utils.MDUtil.resolveDrawable
import com.afollestad.materialdialogs.utils.MDUtil.resolveInt
Expand Down Expand Up @@ -82,11 +83,10 @@ class DialogActionButton(
// Selector
val bgDrawable = resolveDrawable(baseContext, attr = R.attr.md_button_selector)
if (SDK_INT >= LOLLIPOP && bgDrawable is RippleDrawable) {
val rippleColor = resolveColor(context = baseContext, attr = R.attr.md_ripple_color) {
resolveColor(context = baseContext, attr = R.attr.md_ripple_color) {
resolveColor(appContext, attr = R.attr.colorPrimary).adjustAlpha(.12f)
}
if (rippleColor != 0) {
bgDrawable.setColor(valueOf(rippleColor))
}.ifNotZero {
bgDrawable.setColor(valueOf(it))
}
}
background = bgDrawable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.view.View.OnClickListener
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.widget.AppCompatCheckBox
import androidx.core.widget.CompoundButtonCompat
import androidx.recyclerview.widget.RecyclerView
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.R
Expand All @@ -28,11 +29,14 @@ import com.afollestad.materialdialogs.actions.hasActionButtons
import com.afollestad.materialdialogs.actions.setActionButtonEnabled
import com.afollestad.materialdialogs.list.MultiChoiceListener
import com.afollestad.materialdialogs.list.getItemSelector
import com.afollestad.materialdialogs.utils.MDUtil.ifNotZero
import com.afollestad.materialdialogs.utils.MDUtil.maybeSetTextColor
import com.afollestad.materialdialogs.utils.appendAll
import com.afollestad.materialdialogs.utils.createColorSelector
import com.afollestad.materialdialogs.utils.inflate
import com.afollestad.materialdialogs.utils.pullIndices
import com.afollestad.materialdialogs.utils.removeAll
import com.afollestad.materialdialogs.utils.MDUtil.maybeSetTextColor
import com.afollestad.materialdialogs.utils.resolveColor

/** @author Aidan Follestad (afollestad) */
internal class MultiChoiceViewHolder(
Expand Down Expand Up @@ -124,6 +128,13 @@ internal class MultiChoiceDialogAdapter(
adapter = this
)
viewHolder.titleView.maybeSetTextColor(dialog.windowContext, R.attr.md_color_content)
dialog.resolveColor(attr = R.attr.md_color_widget)
.ifNotZero {
CompoundButtonCompat.setButtonTintList(
viewHolder.controlView,
dialog.createColorSelector(checked = it)
)
}
return viewHolder
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.view.View.OnClickListener
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.widget.AppCompatRadioButton
import androidx.core.widget.CompoundButtonCompat
import androidx.recyclerview.widget.RecyclerView
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.R
Expand All @@ -28,8 +29,11 @@ import com.afollestad.materialdialogs.actions.hasActionButtons
import com.afollestad.materialdialogs.actions.setActionButtonEnabled
import com.afollestad.materialdialogs.list.SingleChoiceListener
import com.afollestad.materialdialogs.list.getItemSelector
import com.afollestad.materialdialogs.utils.inflate
import com.afollestad.materialdialogs.utils.MDUtil.ifNotZero
import com.afollestad.materialdialogs.utils.MDUtil.maybeSetTextColor
import com.afollestad.materialdialogs.utils.createColorSelector
import com.afollestad.materialdialogs.utils.inflate
import com.afollestad.materialdialogs.utils.resolveColor

/** @author Aidan Follestad (afollestad) */
internal class SingleChoiceViewHolder(
Expand Down Expand Up @@ -103,6 +107,13 @@ internal class SingleChoiceDialogAdapter(
adapter = this
)
viewHolder.titleView.maybeSetTextColor(dialog.windowContext, R.attr.md_color_content)
dialog.resolveColor(attr = R.attr.md_color_widget)
.ifNotZero {
CompoundButtonCompat.setButtonTintList(
viewHolder.controlView,
dialog.createColorSelector(checked = it)
)
}
return viewHolder
}

Expand All @@ -113,8 +124,8 @@ internal class SingleChoiceDialogAdapter(
position: Int
) {
holder.isEnabled = !disabledIndices.contains(position)

holder.controlView.isChecked = currentSelection == position

holder.titleView.text = items[position]
holder.itemView.background = dialog.getItemSelector()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.afollestad.materialdialogs.R.attr
import com.afollestad.materialdialogs.assertOneSet
import com.afollestad.materialdialogs.internal.list.PlainListDialogAdapter
import com.afollestad.materialdialogs.utils.MDUtil.resolveDrawable
import com.afollestad.materialdialogs.utils.MDUtil.ifNotZero
import com.afollestad.materialdialogs.utils.getStringArray
import com.afollestad.materialdialogs.utils.resolveColor

Expand Down Expand Up @@ -108,9 +109,8 @@ import com.afollestad.materialdialogs.utils.resolveColor
fun MaterialDialog.getItemSelector(): Drawable? {
val drawable = resolveDrawable(context = context, attr = attr.md_item_selector)
if (SDK_INT >= LOLLIPOP && drawable is RippleDrawable) {
val rippleColor = resolveColor(attr = R.attr.md_ripple_color)
if (rippleColor != 0) {
drawable.setColor(valueOf(rippleColor))
resolveColor(attr = R.attr.md_ripple_color).ifNotZero {
drawable.setColor(valueOf(it))
}
}
return drawable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
*/
package com.afollestad.materialdialogs.utils

import android.content.res.ColorStateList
import android.graphics.Color
import androidx.annotation.AttrRes
import androidx.annotation.CheckResult
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.RestrictTo
import androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.R
import com.afollestad.materialdialogs.utils.MDUtil.resolveColor

@ColorInt @CheckResult
Expand All @@ -34,3 +38,16 @@ internal fun Int.adjustAlpha(alpha: Float): Int {
val newAlpha = (255 * alpha).toInt()
return Color.argb(newAlpha, Color.red(this), Color.green(this), Color.blue(this))
}

@RestrictTo(LIBRARY_GROUP) internal fun MaterialDialog.createColorSelector(
@ColorInt unchecked: Int = resolveColor(windowContext, attr = R.attr.colorControlNormal),
@ColorInt checked: Int = resolveColor(windowContext, attr = R.attr.colorControlActivated)
): ColorStateList {
return ColorStateList(
arrayOf(
intArrayOf(-android.R.attr.state_checked),
intArrayOf(android.R.attr.state_checked)
),
intArrayOf(unchecked, checked)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,14 @@ object MDUtil {
@AttrRes attrRes: Int?
) {
if (attrRes == null) return
val color = resolveColor(context, attr = attrRes)
if (color != 0) {
this?.setTextColor(color)
resolveColor(context, attr = attrRes).ifNotZero {
this?.setTextColor(it)
}
}

@RestrictTo(LIBRARY_GROUP) fun Int?.ifNotZero(block: (value: Int) -> Unit) {

This comment has been minimized.

Copy link
@PaulWoitaschek

PaulWoitaschek Mar 23, 2019

Contributor

You should make it an inline function

This comment has been minimized.

Copy link
@afollestad

afollestad Mar 23, 2019

Author Owner

Good idea

This comment has been minimized.

Copy link
@PaulWoitaschek

PaulWoitaschek Mar 24, 2019

Contributor

Also, everything marked with RestrictTo should get the internal modifier so it's not discoverable.

This comment has been minimized.

Copy link
@afollestad

afollestad Mar 24, 2019

Author Owner

Those are used in other modules.

if (this != null && this != 0) {
block(this)
}
}
}
1 change: 1 addition & 0 deletions core/src/main/res-public/values/public.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<attr format="color" name="md_color_content"/>
<attr format="color" name="md_ripple_color"/>
<attr format="color" name="md_color_button_text"/>
<attr format="color" name="md_color_widget"/>
<attr format="dimension" name="md_corner_radius"/>
<attr format="reference" name="md_font_title"/>
<attr format="reference" name="md_font_body"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.view.View
import android.view.View.MeasureSpec.EXACTLY
import android.view.View.MeasureSpec.UNSPECIFIED
import androidx.viewpager.widget.ViewPager
import com.afollestad.materialdialogs.utils.MDUtil.ifNotZero

internal class WrapContentViewPager(
context: Context,
Expand Down Expand Up @@ -50,8 +51,8 @@ internal class WrapContentViewPager(
if (maxChildHeight > maxAllowedHeightFromParent) {
maxChildHeight = maxAllowedHeightFromParent
}
if (maxChildHeight != 0) {
newHeightSpec = MeasureSpec.makeMeasureSpec(maxChildHeight, EXACTLY)
maxChildHeight.ifNotZero {
newHeightSpec = MeasureSpec.makeMeasureSpec(it, EXACTLY)
}

super.onMeasure(widthMeasureSpec, newHeightSpec)
Expand Down
17 changes: 16 additions & 1 deletion documentation/CORE.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
4. [Corner Radius](#corner-radius)
5. [Text Color](#text-color)
6. [Fonts](#fonts)
7. [Widget Color](#widget-color)

## Gradle Dependency

Expand Down Expand Up @@ -584,4 +585,18 @@ using attributes in your app's theme.

See the "Custom Theme" example in the sample project (open the overflow menu for the theme switcher).

<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/master/art/customtheme.png" width="200px" />
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/master/art/customtheme.png" width="200px" />

### Widget Color

By default, widgets like CheckBoxes and RadioButtons use your theme's accent color. You can
override that to use whatever color you want. This applies to checkbox prompts, single and multiple
choice lists, etc. but will not automatically apply to widgets in your own custom views.

```xml
<style name="AppTheme.Custom" parent="Theme.AppCompat">

<item name="md_widget_color">@color/your_color</item>

</style>
```
1 change: 1 addition & 0 deletions sample/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<item name="md_font_button">@font/raleway_semibold</item>
<item name="md_ripple_color">@color/primary_custom_translucent</item>
<item name="md_color_button_text">@color/accent_custom</item>
<item name="md_color_widget">#00CC00</item>
</style>

<style name="SampleHeader" tools:ignore="NewApi">
Expand Down

0 comments on commit 488d915

Please sign in to comment.