10

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?

3
  • where are you calling it? ARe you calling it in the onCreate of the activity? Commented May 10, 2014 at 21:35
  • I am calling these setter functions in the constructor of my custom class that extends EditText (although I have tried setting one of them in constructor and setting the other from a button's onClickListener but the result was the same: my "drawings" disappeared as I clicked the button) Commented May 10, 2014 at 23:16
  • Thank you so much for EDIT. It saved my life.. :D
    – vikoo
    Commented May 17, 2016 at 19:58

4 Answers 4

5

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.

2
  • thank you! I was struggling for several hours while trying to find why adding 'numberDecimal' to input type ruined all my painting in onDraw(). Got really close by observing that clipRect() is set to some weird x translation, but it didn't click. Sometimes development for Android reminds me of living in some detective story :)
    – dimsuz
    Commented Jun 10, 2015 at 12:55
  • for tartar sauce god, how could you figure out that the getScrollX() is the cause?
    – DeckyFx
    Commented Apr 17, 2018 at 7:34
2

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
}
0

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

0

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);       
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.