Android Textview Outline (Kotlin)
July 25, 2018NOTE: You can use shadow to simulate outline.
OutlineTextView class in Kotlin
class OutlineTextView : AppCompatTextView {
private val defaultOutlineWidth = 0F
private var isDrawing: Boolean = false
private var outlineColor: Int = 0
private var outlineWidth: Float = 0.toFloat()
constructor(context: Context) : this(context, null)
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
if (attrs != null) {
val a = context.obtainStyledAttributes(attrs, R.styleable.OutlineTextView)
outlineColor = a.getColor(R.styleable.OutlineTextView_outlineColor, currentTextColor)
outlineWidth = a.getDimension(R.styleable.OutlineTextView_outlineWidth, defaultOutlineWidth)
a.recycle()
} else {
outlineColor = currentTextColor
outlineWidth = defaultOutlineWidth
}
setOutlineWidth(TypedValue.COMPLEX_UNIT_PX, outlineWidth)
}
fun setOutlineColor(color: Int) {
outlineColor = color
}
fun setOutlineWidth(unit: Int, width: Float) {
outlineWidth = TypedValue.applyDimension(
unit, width, context.resources.displayMetrics)
}
override fun invalidate() {
// prevent onDraw.setTextColor force redraw
if (isDrawing) return
super.invalidate()
}
override fun onDraw(canvas: Canvas) {
// if (outlineWidth > 0) {
isDrawing = true
paint.style = Paint.Style.FILL
super.onDraw(canvas)
val currentTextColor = currentTextColor
paint.style = Paint.Style.STROKE
paint.strokeWidth = outlineWidth
setTextColor(outlineColor)
super.onDraw(canvas)
setTextColor(currentTextColor)
isDrawing = false
/*
} else {
super.onDraw(canvas)
}
*/
}
}
res/values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="OutlineTextView">
<attr name="outlineColor" format="color" />
<attr name="outlineWidth" format="dimension" />
</declare-styleable>
</resources>
Sample usage
<com.myapp.view.OutlineTextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="30sp"
app:outlineColor="#000"
app:outlineWidth="3"
/>
textView.setOutlineWidth(TypedValue.COMPLEX_UNIT_PX, 3f)
textView.setOutlineColor(color)
NOTE: Refer Android Get Color.
- algo-trading
- algolia
- analytics
- android
- android-ktx
- android-permission
- android-studio
- apps-script
- bash
- binance
- bootstrap
- bootstrapvue
- chartjs
- chrome
- cloud-functions
- coding-interview
- contentresolver
- coroutines
- crashlytics
- crypto
- css
- dagger2
- datastore
- datetime
- docker
- eslint
- firebase
- firebase-auth
- firebase-hosting
- firestore
- firestore-security-rules
- flask
- fontawesome
- fresco
- git
- github
- glide
- godot
- google-app-engine
- google-cloud-storage
- google-colab
- google-drive
- google-maps
- google-places
- google-play
- google-sheets
- gradle
- html
- hugo
- inkscape
- java
- java-time
- javascript
- jetpack-compose
- jetson-nano
- kotlin
- kotlin-serialization
- layout
- lets-encrypt
- lifecycle
- linux
- logging
- lubuntu
- markdown
- mate
- material-design
- matplotlib
- md5
- mongodb
- moshi
- mplfinance
- mysql
- navigation
- nginx
- nodejs
- npm
- nuxtjs
- nvm
- pandas
- payment
- pip
- pwa
- pyenv
- python
- recylerview
- regex
- room
- rxjava
- scoped-storage
- selenium
- social-media
- ssh
- ssl
- static-site-generator
- static-website-hosting
- sublime-text
- ubuntu
- unit-test
- uwsgi
- viewmodel
- viewpager2
- virtualbox
- vue-chartjs
- vue-cli
- vue-router
- vuejs
- vuelidate
- vuepress
- web-development
- web-hosting
- webpack
- windows
- workmanager
- wsl
- yarn