I want to draw shapes Rectangles, Circle, Paths, Triangles below the text value of TextView and EditText. So I override the onDraw() method and tested it by drawing simple BLUE rectangle after setting the background color to GREEN. I know that I should not initialize object (Paint object) inside the onDraw() method but it is for testing only. Below is the full code:
class Test : AppCompatEditText {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
override fun onDraw(canvas: Canvas) {
setBackgroundColor(Color.GREEN)
canvas.drawRect(0f, 0f, 150f, 50f, Paint().apply {
isAntiAlias = true
color = Color.BLUE
style = Paint.Style.FILL
})
super.onDraw(canvas)
}
}
And then initialize it using xml
<com.slaviboy.galaxy.Test
android:id="@+id/ssss"
android:layout_width="150dp"
android:layout_height="50dp"
android:gravity="center"
android:text="TEST"
android:imeOptions="actionDone"
android:inputType="number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Here is the final result
Last time I have use similar code I remember it working especially for EditText!
- I am drawing a OpenGL scene using GLSurfaceView beneath all the view so I thought that was causing it, so I removed it but the problem remained.
- Then I changed the type from EditText to simple View and it started worked as expected. So it indicated that it is problem caused only when I use EditText and the TextView.
- So I started removing attributes from the xml initialization and in particular the attribute inputType
So as soon as I removed the inputType attribute the code started working as expected,
android:inputType="number"
Any idea why it cases the view to lose all its drawing? And is there a workaround around it?
I know that I can limit the letters allowed using the attribute digits, but is there a way to keep the keyboard style to numbers?
android:digits="0123456789."