Created
February 20, 2019 23:41
-
-
Save basshelal/36d9335de0ad398dc16142ed45fdb477 to your computer and use it in GitHub Desktop.
Editable Text View for Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Add this to your attrs.xml --> | |
<declare-styleable name="EditTextView"> | |
<attr name="isEditable" format="boolean" /> | |
<attr name="isMultiline" format="boolean" /> | |
</declare-styleable> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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