is there a way to get the hierarchy of the current layout of the device along with the resource id of the elements in the hierarchy?
I found a way to get the hierarchy but the IDs of the elements weren't there.
Also, note that i'm not doing that from inside the application with the current layout.
View has method: getParent() and getId(). ViewGroup hasMethod: getChildren(). It is enough to get full hierarchy.
If you want to find out View hierarchy of elements in layout you should inflate this layout using LayoutInflater.
Another solution is to create your own xml parser.
Related
Difference between Layout & Container Views?
First of all It is not meant if duplicate answers are their it must be correct. They answers the question according to definition but According to Visualization & According to Android Studio they are different & yet not explained in details.
Layout : define the arrangement of Views inside a root Container View
View : the child Views inside a Container View.
Container View : A View that can contain child Views and nested Container Views, they are classes extended from Android ViewGroup class. Some Container View classes are RelativeLayout, LinearLayout, FrameLayout, RecyclerView etcetera
As far as I understand we can put any view, such as child view of our root layout when initializing Snackbar.
Quoted from the document:
Snackbar will try and find a parent view to hold Snackbar's view from the value given to view.
However, this statement makes me think of putting the root view might be the best approach. Since traversing the nodes can be taxing especially a layout with a complex hierarchy.
Do I understand this correctly?
I'm studying android, of course I'm novice, I always read something like view hierarchy, so what exactly does this mean? what is view hierarchy? e.g.
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
http://developer.android.com/reference/android/app/Fragment.html
A View inside another View creates an hierarchy, the outer view becomes the parent of the inner view and the inner view is its child. It's just nested views.
Here's an example:
You'll want to read the docs on the View class, but essentially views can be children of certain other views. You can nest views in complicated ways. This whole structure of views is referred to as the view hierarchy.
http://i.stack.imgur.com/gN6AO.png
Each view in a user interface represents a rectangular area of the display. A view is responsible for what is drawn in that rectangle and for responding to events that occur within that part of the screen (such as a touch event).
A user interface screen is comprised of a view hierarchy with a root view positioned at the top of the tree and child views positioned on branches below. The child of a container view appears on top of its parent view and is constrained to appear within the bounds of the parent view’s display area.
you can refer to this link : [
http://www.techotopia.com/index.php/Understanding_Android_Views,_View_Groups_and_Layouts_in_Android_Studio][1]
For example When we write the code
View view = inflater.inflate(R.layout.main_activity, null);
What does the Android system do?
Check out the source for the LayoutInflater. It's an abstract class, a concrete instance of which is obtained through getLayoutInflater().
In essence, the inflater creates a root view object (the root view group of the inflated XML), then does two passes through the XML tree to attach each child view. This is done recursively to handle 'include' and to fix up references between child views, for example in RelativeLayout, and is done top to bottom.
The first pass constructs the tree by instantiating each of the child views, top down recursively, and passes the XML attributes to the view constructor telling the view how big it should be. It then calls measure() for each child passing in restrictions determined by the parent (e.g. RelativeLayout with 2 child views each requesting match_parent) using a measure specifications object and asks the view how big it wants to be. If the view is itself a view group, it will use the same algorithm to measure it's children.
The second pass is the layout pass when layout() is called on each child to position itself within the view. The parent positions the view using the measurements calculated in the measure pass. onDraw() is called and is passed a Canvas created from the DecorView backing bitmap.
The finalised tree is then ready to pass to the window manager which is done by setContentView() or addContentView().
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.1_r1/android/view/LayoutInflater.java#LayoutInflater
Inflating an XML layout in simple language means you are converting the XML in View. Then you can fetch each view declared in the XML using the parent/inflated View.
For eg -
View view = inflater.inflate(R.layout.main_activity, null);
Now, here view is the reference of the XML from which you can fetch all the views as,
TextView tv = (TextView)view.findViewById(R.id.tv);
Just something I have been thinking about. It is possible to create an android layout with just a TextView widget and no Layout code (e.g: inearLayout, ScrollLayout), but if I try to add anything else to the XML file all sorts of errors start popping up. Is it possible to create a Layout with just widgets?
Also, if it is, how?
I don't think it's possible inside of an XML layout to have a layout of purely widgets and no ViewGroups. You would for one be creating an invalid XML doc (multiple roots since a widget cannot contain other widgets). Also you need a ViewGroup to hold multiple Views. You can create a merge and then decide on the view group later, however that is just a substitute for a ViewGroup. However it is the closest thing to an XML layout with only widgets in it, surrounded by a merge element as the root xml tag.
Yes it is possible to create layout with a single View (e.g. TextView). But if multiple Views are required you neeed to wrap them with a ViewGroup (e.g. LinearLayout)