Skip to content

Commit

Permalink
Add support for setting custom drawables programatically.
Browse files Browse the repository at this point in the history
  • Loading branch information
tiper committed Jun 27, 2019
1 parent 2e40c1d commit 1fc9847
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
45 changes: 26 additions & 19 deletions materialspinner/src/main/kotlin/com/tiper/MaterialSpinner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ open class MaterialSpinner @JvmOverloads constructor(
const val MODE_BOTTOMSHEET = 2
}

private val colorStateList: ColorStateList

/**
* The view that will display the available list of choices.
*/
Expand Down Expand Up @@ -191,7 +193,7 @@ open class MaterialSpinner @JvmOverloads constructor(

// Create the color state list.
//noinspection Recycle
context.obtainStyledAttributes(
colorStateList = context.obtainStyledAttributes(
attrs,
intArrayOf(R.attr.colorControlActivated, R.attr.colorControlNormal)
).run {
Expand All @@ -206,23 +208,17 @@ open class MaterialSpinner @JvmOverloads constructor(
intArrayOf()
), intArrayOf(activated, activated, normal)
)
}.let {
// Set the arrow and properly tint it.
getContext().getDrawableCompat(
}
// Set the arrow and properly tint it.
getContext().getDrawableCompat(
getResourceId(
R.styleable.MaterialSpinner_android_src,
getResourceId(
R.styleable.MaterialSpinner_android_src,
getResourceId(
R.styleable.MaterialSpinner_srcCompat,
R.drawable.ic_spinner_drawable
)
), getContext().theme
)?.apply {
DrawableCompat.setTintList(this, it)
DrawableCompat.setTintMode(this, PorterDuff.Mode.SRC_IN)
}
}?.apply {
setBounds(0, 0, intrinsicWidth, intrinsicHeight)
}.also {
R.styleable.MaterialSpinner_srcCompat,
R.drawable.ic_spinner_drawable
)
), getContext().theme
).let {
setDrawable(it)
}

Expand Down Expand Up @@ -257,8 +253,19 @@ open class MaterialSpinner @JvmOverloads constructor(
}
}

private fun setDrawable(d: Drawable?) {
editText.setCompoundDrawablesWithIntrinsicBounds(null, null, d, null)
fun setDrawable(drawable: Drawable?, applyTint: Boolean = true) {
editText.setCompoundDrawablesWithIntrinsicBounds(
null,
null,
drawable?.let { DrawableCompat.wrap(drawable) }?.apply {
setBounds(0, 0, intrinsicWidth, intrinsicHeight)
if (applyTint) {
DrawableCompat.setTintList(this, colorStateList)
DrawableCompat.setTintMode(this, PorterDuff.Mode.SRC_IN)
}
},
null
)
}

override fun setOnClickListener(l: OnClickListener?) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tiper.materialspinner.sample

import android.os.Bundle
import android.support.v4.content.res.ResourcesCompat
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
Expand Down Expand Up @@ -42,6 +43,7 @@ class MainActivity : AppCompatActivity() {
adapter = it
onItemSelectedListener = listener
selection = 3
setDrawable(ResourcesCompat.getDrawable(resources, R.drawable.ic_arrow_downward, theme))
}
spinner.adapter = it
appCompatSpinner.adapter = it
Expand Down
9 changes: 9 additions & 0 deletions sample/src/main/res/drawable/ic_arrow_downward.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M20,12l-1.41,-1.41L13,16.17V4h-2v12.17l-5.58,-5.59L4,12l8,8 8,-8z"
android:fillColor="#010101"/>
</vector>

0 comments on commit 1fc9847

Please sign in to comment.