I'm not too clear about this and neither are the docs.
When I'm creating a custom view, I override like so:
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
//more code here...
}
My question is whether it's necessary to call super.onDraw(canvas);. The code seems to work fine without it, but I want to be sure that it's okay to leave it out.
So is it necessary?
If you want it to call the superclass onDraw method (think TextView or KeyboardView rather than a generic View), then call super.onDraw. If you don't want that, i.e. you are planning on drawing the entire View yourself (which it seems you are), there's no reason to call it.
Also, if you're extending View (and not some class that extends view), super.onDraw doesn't really do anything.
For me, I call super.onDraw when I want to draw lines over a KeyboardView. So, super.onDraw draws the keyboard, and my custom LatinKeyboardView (which extends KeyboardView) draws the swiping path on top of the keyboard.
A peek at the source code shows that View.onDraw() is an empty method. So, calling super.onDraw(), if the parent class is View itself, does nothing. It's unnecessary yet harmless.
Whether you should go ahead and do it anyway is a separate question of efficiency, safety, and style.
Yes, it is. If you custom a TextView, the super.onDraw will ensure to draw whatever belongs to TextView attributes (like the text, text color, text shadow, etc...) The same with other Views like Button, CheckBox...
In case your custom View extend View (not a specific subclass of View, just View), it is still better to leave super.onDraw(canvas) there for some View's draw methods (like setBackgroundDrawable, etc...)
It's not required unless you are actually overriding the onDraw() method. If the new class doesn't override it, the super's method will automatically be called.
Related
I'm working on a project that using custom views created with Android Canvas. And I'm stuck in the next part: How to create custom animations for elements on Android Canvas (the idea), where I cannot use the already-supported-animations. Any idea or example is appreciated! Thanks!
The simplest approach is to hold some state variables in your View. Then you can use ValueAnimator or ObjectAnimator. Former will trigger ValueAnimator.AnimatorUpdateListener where you can modify view state in proper way and then invalidate(), which trigger redrawing. Latter will update view state via setSmth() methods, that you should implement on your view. At the end of these setters you should call invalidate() too. And in your onDraw() you need to draw proper things depending on actual values of state variables
Hi I'm having trouble understanding how to draw a custom view at runtime. For simplicity sake, say I have a custom view (Box) that extends the View class, and basically all it does is create a square which is configured to do in my onDraw() method. Now I want to be able to draw this custom view in an activity depending on if the user clicks a button on the activity.
So if the user clicks the button once, one box is drawn, clicks again another box is drawn maybe 10 pixels down etc. I searched for a solution but all I can find is people using a pre-made xml with the view and then using an inflator but I dont want to have a pre drawn box if the user hasn't pressed the button.
Also another quick question: When is onDraw() called or do I have to explicitly call
Box box = new Box();
box.onDraw(canvas);
Eclipse says that's a suspicious call, I agree.
To make a custom View you have to extend View class and override onDraw method. Then when you want to draw this view you have to add it to your view hierarchy. This is as simple view.add(new Box()), where view is your root view, this way Android system would invoke onDraw method by itself on every rendering cycle. For more information refer to official documentation.
i'm building my interactive map and i find myself really confused:
I have an extend of ItemizedOverlay class which i'm managing the overlays.
when shell i use its populate() function in order to update the overlays in the mapView and when should i use invalidate() or postInvalidate() in order to get the map to update.
i found that sometimes this works, and sometimes the other.
if someone will be kind to explain exactly what each of the function does - i will be thanks full because i couldn't find a decent explanation.
populate() should be used when you want to populate the overlay. If all you want to do is have it redrawn, then you should use invalidate() if you're on the UI thread, and postInvalidate() if you're on another thread. The invalidate methods are applicable to any View, be it a button or a text view or anything. They basically tell Android that something has changed related to the View, like the data being shown or the button's state or colour or whatever. Android will then attempt to redraw the View as soon as possible by calling the View's onDraw() method.
I always use invalidate() and never had a problem with it... (i.e. always refreshes)
The problem:
I have a class, inherited from LinearLayout.
This class creates some buttons in the constructor and puts them into itself using addChild().
I've overloaded method onSizeCHanged and I want to add some childs in this method.
But changes have no effect until one of the other buttons clicked.
So, I need to press some of existing buttons and after that views which I've added appear.
(by the way, only buttons with ontouchlistener do it. buttons without listener cannot make new views appeared).
How to add views in onSizeChange method to do them appeared immediately?
ADDITION:
Methods forceLayout() and requestLayout() cannot work.
I have not extended LinearLayout myself but I read up on the class a bit and I think you need to override onLayout() and add your child views in it. Check the docs and see if it helps
http://developer.android.com/reference/android/widget/LinearLayout.html#onLayout(boolean,%20int,%20int,%20int,%20int)
The solution was: making a simple boolean flag, setting it "true" im onSizeChanged and calling "invalidate".
In method "onDraw" checking if flag is true and if it is so, calling requestLayout. Then setting flag to false.
While I'm developing a custom widget by using TextView widget, this question comes up to my mind.
When mText(member variable of TextView) is displayed, actually?
I have thought that, just like other widgets, if I override the onDraw method in the custom widget, which is derived from TextView, I can draw mText as I want.
But, it's not true.
I'm reviewing Android Widget source, and then I realized that mText is not displayed while onDraw is called, definitely.
Is there someone who knows about it?
take a look at protected makeNewLayout()