Skip to content

Instantly share code, notes, and snippets.

@fulcrum6378
Last active July 28, 2022 16:21
Show Gist options
  • Save fulcrum6378/344c9c20598bdbeccb631902c74733ba to your computer and use it in GitHub Desktop.
Save fulcrum6378/344c9c20598bdbeccb631902c74733ba to your computer and use it in GitHub Desktop.
Customised NumberPicker capable of applying a font resource on its numbers. (it was quite difficult to discover this hack tho!!)
package <COM.MY.APPLICATION>
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.NumberPicker
class PrettyNumberPicker(
context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int
) : NumberPicker(context, attrs, defStyleAttr, defStyleRes) {
constructor(context: Context) : this(context, null)
constructor(context: Context, attrs: AttributeSet?) :
this(context, attrs, android.R.attr.numberPickerStyle)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
this(context, attrs, defStyleAttr, 0)
override fun addView(child: View?, params: ViewGroup.LayoutParams?) {
super.addView(child, params)
if (child is EditText) {
child.typeface = context.resources.getFont(<YOUR FONT RESOURCE ID HERE>)
child.isEnabled = false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment