Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Editable Text View for Android
<!-- Add this to your attrs.xml -->
<declare-styleable name="EditTextView">
<attr name="isEditable" format="boolean" />
<attr name="isMultiline" format="boolean" />
</declare-styleable>
package uk.whitecrescent.waqti.frontend.customview
import android.content.Context
import android.os.Build
import android.text.InputType
import android.util.AttributeSet
import android.view.View
import androidx.appcompat.widget.AppCompatEditText
/**
* The most basic version of an Editable TextView.
* If you are reading this and you are not me (Hello there :) ) you are free to
* use this however you like! Enjoy :)
*
* @author Bassam Helal
*/
class EditTextView
@JvmOverloads constructor(context: Context,
attributeSet: AttributeSet? = null,
defStyle: Int = 0) : AppCompatEditText(context, attributeSet, defStyle) {
var isEditable: Boolean = true
set(value) {
field = value
isCursorVisible = value
showSoftInputOnFocus = value
isFocusableInTouchMode = value
isFocusable = value
isClickable = value
}
var isMultiLine: Boolean = false
set(value) {
field = value
if (value) {
inputType = inputType or InputType.TYPE_TEXT_FLAG_MULTI_LINE
}
}
init {
val attributes = context.obtainStyledAttributes(attributeSet, R.styleable.EditTextView)
attributes.getBoolean(R.styleable.EditTextView_isEditable, true).apply {
isEditable = this
}
attributes.getBoolean(R.styleable.EditTextView_isMultiline, false).apply {
isMultiLine = this
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
setTextAppearance(R.style.TextAppearance_MaterialComponents_Headline4)
} else {
@Suppress("DEPRECATION")
setTextAppearance(this.context, R.style.TextAppearance_MaterialComponents_Headline4)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
setTextColor(resources.getColor(R.color.black, null))
} else {
@Suppress("DEPRECATION")
setTextColor(resources.getColor(R.color.black))
}
textAlignment = View.TEXT_ALIGNMENT_CENTER
attributes.recycle()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.