meztelentsiga
meztelentsiga

Reputation: 171

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?

Upvotes: 10

Views: 1543

Answers (4)

Ramachandra Reddy Avula
Ramachandra Reddy Avula

Reputation: 1114

getScrollX() solved my problem

   @Override
    protected void onDraw(Canvas canvas) {
        Paint p = new Paint();
        p.setColor(Color.RED);
        canvas.drawLine(0f+getScrollX(), 20f, 200f+getScrollX(), 30f, p);       
    }

Upvotes: 0

Ahmad Melegy
Ahmad Melegy

Reputation: 1580

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
}

Upvotes: 2

simekadam
simekadam

Reputation: 7384

Check your getScrollX() values. I bet you'll see values like 1000000 pixels. EditText translates its canvas in this weird way, so eventhough you're actually drawing on it, it's way off the visible clip.

To solve it just add the scrollX value to your drawing coordinates.

Upvotes: 5

H3c
H3c

Reputation: 951

You must set android:digits="0123456789" in xml.But I don't know why...

Upvotes: 0

Related Questions