diff --git a/materialspinner/src/main/kotlin/com/tiper/MaterialSpinner.kt b/materialspinner/src/main/kotlin/com/tiper/MaterialSpinner.kt
index 96ab7fb..87c11f8 100644
--- a/materialspinner/src/main/kotlin/com/tiper/MaterialSpinner.kt
+++ b/materialspinner/src/main/kotlin/com/tiper/MaterialSpinner.kt
@@ -159,6 +159,24 @@ open class MaterialSpinner @JvmOverloads constructor(
R.styleable.MaterialSpinner_android_focusableInTouchMode,
editText.isFocusableInTouchMode
)
+ getColorStateList(R.styleable.MaterialSpinner_android_textColor)?.let {
+ editText.setTextColor(
+ it
+ )
+ }
+ getDimensionPixelSize(
+ R.styleable.MaterialSpinner_android_textSize,
+ -1
+ ).let { if (it > 0) editText.textSize = it.toFloat() }
+ getText(R.styleable.MaterialSpinner_android_text)?.let {
+ // Allow text in debug mode for preview purposes.
+ if (isInEditMode) {
+ editText.setText(it)
+ } else {
+ throw RuntimeException("Don't set text directly." +
+ "You probably want setSelection instead.")
+ }
+ }
popup = when (getInt(R.styleable.MaterialSpinner_spinnerMode, mode)) {
MODE_DIALOG -> {
DialogPopup(context, getString(R.styleable.MaterialSpinner_android_prompt))
@@ -171,7 +189,7 @@ open class MaterialSpinner @JvmOverloads constructor(
}
}
- // Create the color state list
+ // Create the color state list.
//noinspection Recycle
context.obtainStyledAttributes(
attrs,
@@ -189,21 +207,22 @@ open class MaterialSpinner @JvmOverloads constructor(
), intArrayOf(activated, activated, normal)
)
}.let {
- val resources = getContext().resources
- val theme = getContext().theme
// Set the arrow and properly tint it.
- resources.getDrawableCompat(
+ getContext().getDrawableCompat(
getResourceId(
R.styleable.MaterialSpinner_android_src,
- R.drawable.ic_spinner_drawable
- ), theme
+ 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)
- }.let {
+ }.also {
setDrawable(it)
}
@@ -243,9 +262,8 @@ open class MaterialSpinner @JvmOverloads constructor(
}
override fun setOnClickListener(l: OnClickListener?) {
- throw RuntimeException(
- "Don't call setOnClickListener. You probably want" +
- "setOnItemClickListener instead"
+ throw RuntimeException("Don't call setOnClickListener." +
+ "You probably want setOnItemClickListener instead."
)
}
@@ -310,9 +328,18 @@ open class MaterialSpinner @JvmOverloads constructor(
prompt = context.getText(promptId)
}
- private fun Resources.getDrawableCompat(@DrawableRes id: Int, theme: Resources.Theme?): Drawable? {
- return ResourcesCompat.getDrawable(this, id, theme)
- ?.let { DrawableCompat.wrap(it).mutate() }
+ private fun Context.getDrawableCompat(
+ @DrawableRes id: Int,
+ theme: Resources.Theme?
+ ): Drawable? {
+ return resources.getDrawableCompat(id, theme)
+ }
+
+ private fun Resources.getDrawableCompat(
+ @DrawableRes id: Int,
+ theme: Resources.Theme?
+ ): Drawable? {
+ return ResourcesCompat.getDrawable(this, id, theme)?.let { DrawableCompat.wrap(it) }
}
private inner class DialogPopup(
diff --git a/materialspinner/src/main/res/values/attrs.xml b/materialspinner/src/main/res/values/attrs.xml
index 795e899..57fb8cf 100644
--- a/materialspinner/src/main/res/values/attrs.xml
+++ b/materialspinner/src/main/res/values/attrs.xml
@@ -6,6 +6,10 @@
+
+
+
+
diff --git a/sample/src/main/kotlin/com/tiper/materialspinner/sample/MainActivity.kt b/sample/src/main/kotlin/com/tiper/materialspinner/sample/MainActivity.kt
index 281551d..e459796 100644
--- a/sample/src/main/kotlin/com/tiper/materialspinner/sample/MainActivity.kt
+++ b/sample/src/main/kotlin/com/tiper/materialspinner/sample/MainActivity.kt
@@ -15,6 +15,7 @@ class MainActivity : AppCompatActivity() {
object : MaterialSpinner.OnItemSelectedListener {
override fun onItemSelected(parent: MaterialSpinner, view: View?, position: Int, id: Long) {
Log.v("MaterialSpinner", "onItemSelected parent=${parent.id}, position=$position")
+ parent.focusSearch(View.FOCUS_UP)?.requestFocus()
}
override fun onNothingSelected(parent: MaterialSpinner) {
diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml
index 58208f7..7e119fa 100644
--- a/sample/src/main/res/layout/activity_main.xml
+++ b/sample/src/main/res/layout/activity_main.xml
@@ -9,6 +9,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
+ android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:padding="8dp"
@@ -23,6 +24,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
+ android:focusable="true"
+ android:focusableInTouchMode="true"
android:text="clear" />
@@ -52,6 +54,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
+ android:focusable="true"
+ android:focusableInTouchMode="true"
android:text="clear" />
+ android:prompt="@string/app_name"
+ android:src="@drawable/ic_selector_drawable"
+ android:textColor="#0ff"
+ tools:text="lalala" />
+ app:spinnerMode="dialog"
+ app:srcCompat="@drawable/ic_selector_drawable" />