Skip to content

Commit

Permalink
Add some support for simple text costumization.
Browse files Browse the repository at this point in the history
  • Loading branch information
tiper committed Jun 27, 2019
1 parent e8c1e44 commit 2e40c1d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 17 deletions.
53 changes: 40 additions & 13 deletions materialspinner/src/main/kotlin/com/tiper/MaterialSpinner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -171,7 +189,7 @@ open class MaterialSpinner @JvmOverloads constructor(
}
}

// Create the color state list
// Create the color state list.
//noinspection Recycle
context.obtainStyledAttributes(
attrs,
Expand All @@ -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)
}

Expand Down Expand Up @@ -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."
)
}

Expand Down Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions materialspinner/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<attr name="android:focusableInTouchMode" />
<attr name="android:prompt" />
<attr name="android:src" />
<attr name="android:textColor" />
<attr name="android:textSize" />
<attr name="android:text" />
<attr name="srcCompat" />
<attr name="spinnerMode" format="enum">
<enum name="dialog" value="0" />
<enum name="dropdown" value="1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 13 additions & 4 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" />

<Button
Expand All @@ -42,7 +45,6 @@
app:spinnerMode="bottomsheet" />

<FrameLayout

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp">
Expand All @@ -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" />

<Button
Expand All @@ -70,7 +74,10 @@
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp"
android:hint="MaterialSpinner 2"
android:prompt="@string/app_name" />
android:prompt="@string/app_name"
android:src="@drawable/ic_selector_drawable"
android:textColor="#0ff"
tools:text="lalala" />

<FrameLayout
android:layout_width="match_parent"
Expand All @@ -82,6 +89,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="clear" />

<Button
Expand All @@ -99,8 +108,8 @@
android:layout_height="wrap_content"
android:hint="MaterialSpinner 3"
android:prompt="@string/app_name"
android:src="@drawable/ic_selector_drawable"
app:spinnerMode="dialog" />
app:spinnerMode="dialog"
app:srcCompat="@drawable/ic_selector_drawable" />

<Spinner
android:id="@+id/spinner"
Expand Down

0 comments on commit 2e40c1d

Please sign in to comment.