Search code examples
javaandroidkotlinandroid-edittext

Cannot draw shapes Rectangles, Paths... below EditText text


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

enter image description here

Last time I have use similar code I remember it working especially for EditText!

  1. 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.
  2. 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.
  3. 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."

Solution

  • The problem that you are having is related to this issue: Cannot draw on Chip when text alignment is center. You can read through the accepted answer, but the bottom line is that TextView internal scrolling is off so, even though your drawing is taking place, it is scrolled out of view. The quick solution is to set horizontal scrolling to false.

    class MyEditText : androidx.appcompat.widget.AppCompatEditText{
        ...
        init {
            setHorizontallyScrolling(false)
        }
        ...
    }
    

    There may be unintended consequences to doing this but, if the TextView doesn't do anything fancy, I think you will be ok. There are other fixes mentioned in the answer such as capturing the scroll and scrolling back that may be safer.