I was trying to understand what's the meaning of inflating a view and found this topic:
What does it mean to inflate a view from and xml file?
But I was not satisfied by the accepted answer, as it mention that inflating a view causes its rendering, while I think it is only responsible of the conversion of the XML layouts into the corresponding code by building the View objects, and I'm wondering if it's the case or not, and if not, what can initiate the drawing of the view on screen?
Can anyone clarify my questioning by providing trusted or official documentation?
Every subclass of View overrides the onDraw() method from the View class. This is where the View actually specifies how it will be drawn on the screen. All drawing takes place on the View's Canvas, and the dimensions of the Canvas are specified by the width and height attributes you set in your layout file for that View.
For instance, here's the onDraw() method for TextView:
https://android.googlesource.com/platform/frameworks/base/+/jb-mr0-release/core/java/android/widget/TextView.java#4738
Related
I am trying to draw a View in a ViewGroup without adding it to the child list.
I am doing this because I want to display something like a ProgressBar in the exact center of layouts like a LinearLayout so I don't want the layout to handle the measuring and layouting.
I also don't want to complicate the view hierarchy by adding extra layouts just to achieve this effect so my solution was to extend the LinearLayout, create a ProgressBar and handle measuring, layouting and drawing for that view myself.
My implementation seems to work ok from what I tested but I am wondering if there is anything I am not noticing or if there are any problems that can appear in the future.
From what I understand calling addView also sets the child view's parent and calls dispatchAttachedToWindow, these methods are package-private so I can't call them myself.
Is there any side effect that can arise from calling measure, layout and draw on a view that has no parent and that was not "attached" to a window? Is there a safer way to achieve the same effect?
Thanks.
I'm trying to programatically position a TextView above a custom View in a Relative Layout that has also been placed programatically (lets call this CustomView).
I've scoured these forums for similar problems, and I seem to have done all the steps, including setting ids, making sure there are no conflicting rules, etc. But yet it doesn't seem to work.
I am wondering if this has to do with the fact that my CustomView class overrides the onDraw() method (it needs to eventually be animated). In fact, testing the CustomView with it's own set of LayoutParams shows that it does not obey the rules laid out in it - in fact the way that I place this CustomView is by calling canvas.drawBitmap() with the relevant positions in the onDraw() method of CustomView.
Any idea how I can get around this problem? Is it possible to somehow place the the TextView above the CustomView?
I am new to android and trying to make something like the fingerlock security system of the android. The problem is that I have found a lot of reference on drawing onto a plain background (that sets different content view and allows drawing onto that), but not regarding drawing onto the same activity layout.
To draw manually on a View, you need to override its onDraw method. For this you need a custom View (often extending from an existing View type).
If you are using an xml to set the Activity's layout in setContentView() then you need to add the above custom view to this xml. Then, you can draw onto the same activity layout.
setContentView is noting but the activity view. once the view is set in main activity (max in oncreate method) it can update from multiple classes according to your logics.. i think before trying this you have to understand the view and its hierarchic properties properly
I have a ViewGroup with several children, and I want to be able to get the bitmap of the view group itself, before any of its children are added. I need it because I want to rotate the bitmap without affecting the children. Is there any way to do that? Thanks.
Don't add the Bitmap until after View inflation is done. In other words, if it's in the xml layout, take it out and do what you need in Java.
EDIT: in light of your comment.
Get the ViewTreeObserver, like so:
ViewTreeObserver theObserver = viewGroup.getViewTreeObserver();
Then add whatever listener you need to the ViewTreeObserver.
I found out that the onDraw function actually handles only the drawing of the view itself. The children, if exist, are handled in a later stage of the drawing process, so I can do all the bitmap manipulations there.
i have the following problem.
I am working with a library of maps which paints the icons on the map using drawables and canvas.
Now, i'm trying to modify it in order to the user can click on icons. So i want to attach drawables into different ImageView with a onClickListener.
However, i don't know how i can paint the ImageView using canvas from method onDraw.
I've tried with:
ImageView iv = new ImageView(context);
iv.setDrawableResource(drawable);
iv.draw(c)
But it doesn't appears in screen.
Any idea? Thanks
You might have better luck if you provided more context -- what's "c" represent here? But in any case, you can't just create new imageviews, you need to attach them to your layout, either by inflating them with a parent view argument from XML, or calling your parent layout's addView() programatically.
Generally, you'll rarely call any draw() methods by hand (unless you're implementing a custom view of some sort); you'll inflate your views from XML into your layout, or else instantiate your views, set whatever LayoutParams you need, and add them to a layout. The Android UI libraries handle figuring out when standard views are invalidated and need to be redrawn, for performance reasons and your own sanity.