Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom EditText not working after setting its input type through setInputType()

I have created a custom EditText, and have its onDraw() function overridden. Everything was working fine (I could draw anything inside the EditText) until I wanted to change the input method for this EditText and have called this function:

setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

No matter when and where I call it: after it has been called, my custom EditText disappears, and only the original EditText is shown, although my overridden onDraw() function, and inside it, either its super function is invoked (no matter where exactly I invoke it).

I have also tried playing with Transparent colors as suggested in some posts here: setBackgroundColor(Color.TRANSPARENT) on the base class, and also paint.setColor(0x80FF0000) for my Paint object in the onDraw() function but they had no effect.

EDIT: I have discovered that the problem is not lying in setting input type, but setting input type AND gravity of EditText simultaneously:

setGravity(Gravity.CENTER_HORIZONTAL);

If any of them is not set, my custom view is drawn as expected. Also (and what is even more strange): with Gravity.CENTER_VERTICAL there is no problem, only with Gravity.CENTER_HORIZONTAL and Gravity.CENTER.

It seems like these two settings are mutually exclusive. Could this be possible?

like image 240
meztelentsiga Avatar asked May 09 '14 12:05

meztelentsiga


1 Answers

Thanks to @simekadam's answer I managed finally to fix it. This is how I fixed it in case you didn't get it

override fun onDraw(canvas: Canvas) {        
    super.onDraw(canvas)
    canvas.translate(scrollX.toFloat(), 0f)
    // do your drawing
}
like image 93
Ahmad Melegy Avatar answered Sep 30 '22 14:09

Ahmad Melegy