Should I pass the layout's rootview to Snackbar? - android

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?

Related

Inflater in Android Fragments

View rootView = inflater.inflate(R.layout.check_fragment,container,false);
What is the use of a bool as in this case false ?
I am a beginner to Android Programming , can someone explain this to me in detail ?
Thanks in advance.
https://developer.android.com/reference/android/view/LayoutInflater.html
If you set it as true, then the view will be automatically added to its parent (second param). It most of times it should be false, but sometimes it is needed especially when you're using <merge> as root in inflated xml.
true means "please add the inflated View to container as a child for me". false means "please do not add the inflated View to container as a child for me, as other code will handle that later on".
In the case of fragments, you allow the FragmentManager to control adding and removing the fragment's View from its container.
The reason why you need container at all in inflate() is because certain layout manager classes (notably RelativeLayout) need to know their container in order to set up their layout rules properly.
Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.
From docs:
attachToRoot - Whether the inflated hierarchy should be attached to the
root parameter? If false, root is only used to create the correct
subclass of LayoutParams for the root view in the XML.
In fragments you should pass false as an attach argument, this way the view hierarchy will not be attached to the ViewGroup parent passed in the onCreateView. This attachment will happen later on, Android will take care of it. The container is only passed to onCreateView so you can know about the container where your fragments view hierarchy is going to go.
Actually setting this parameter to true will likely cause exceptions or at least some strange behaviour.

android view hierarchy with resource id

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.

android - what is view hierarchy?

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]

Android: How to get the ID of a parent View?

View.getRoot() returns View, so we can easily figure out which is the root view by using getResourceName(View.getId()).
View.getParent()..., while I expect it also returns View that is the parent, actually only returns an instance of ViewParent that seems to have very very few useful method/fields. It sucks.
So, is there any way to know the ID of the parent? I believe a View's parent is also View, thus it should has mID field.
I really wonder why Google didn't let View.getParent() just returns View. It makes sense, only when something else other than View could be the parent, and as far as I know, it's limited to View and its subclasses.
ViewParent is just an interface that any View that can have children implements. Most of the times the class you get back will be an instance of a ViewGroup, like LinearLayout or RelativeLayout. I'm not exactly sure what you mean by "name" but if you want to get the class name you can do so like always: view.getParent().getClass().getName().
The docs state that the parent is not necessarily a View:
public final ViewParent getParent ()
Added in API level 1 Gets the parent of this view. Note that the
parent is a ViewParent and not necessarily a View.
Returns Parent of this view.
However all implementations of ViewParent inherit from View. This should be a design decision to decouple the parent from a View using the ViewParent interface, although all implementations in the SDK are views.
Try casting it first. For example, if the parent of the View that you're trying to get id of is a TableRow, then do
((TableRow)View.getParent()).getID()

What happens behind the scenes when an xml is "inflated"?

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

Categories

Resources