What does getRootView() do? - android

I came across this TextView class method() in some code for taking a screenshot. What does it do, what's its purpose?
textView.getRootView()
Actually, what is a root view?

It is a method of the base class View
It basically lets you find the topmost view in the current view hierarchy.
e.g
<LinearLayout>
<TextView/>
<RelativeLayout>
<...something...>
</RelativeLayout>
</LinearLayout>
In the above example, the root view would be the top most declared view ie the linear layout

First, read the documentation for getRootView().
Then the next question is, what is a View Hierarchy? Read that here.
So to be succinct, a root view is the top-most view in your current View Hierarchy that is connected to your object you are calling getRootView() from.
Looking at the above tree map, or a binary tree, it would be the root, or top-most node.

Related

What do you mean by root view of an activity?

I have a very trivial confusion regarding the what is the root view of an activity. I searched this throughout google and stack overflow. I got some knowledge about it but didn't come to a convincing solution as no one clearly said about that term. The question which seems to be alike my question tell about what a root view of a given layout is but to me they don't seem to specify the root view of an activity is.Though the answer to my question is quite intuitive, I want a definite answer with no confusion.So here is the code structure.
I have the following activity class :
package com.example.android.viewpager;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
And the XML code associated with it when I created the activity is (activity_main.xml) :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.viewpager.MainActivity">
</LinearLayout>
So according to me is this activity_main.xml the root view of the MainActivity?
The Root View of Your MainActivity is LinearLayout
The Root View of Your Activity means the Top most parent layout of Your XML Layout
The Root View may be a RelativeLayout, CoordinatorLayout, ConstraintLayout, LinearLayout,FrameLayout
For Example
Your activity_main.xml Contain LinearLayout as parent layout than LinearLayout is your Root View which hold all the child controlls in it like Buttons, Imageview, EditText, TextView, etc....
The activity_main.xml is the layout of your MainActivity. Inside the layout the root view is the top hierarchical view which in this case is a LinearLayout. It could also be a RelativeLayout, CoordinatorLayout, ConstraintLayout etc.
The first view that is capable of holding child view will be called your root view. In your case, Linear Layout will be your root layout
activity_main is not the root view. This is the XML file where the layout of your activity is defined.
The root view is the view in this layout, containing all the other views.
In your example, the root view is the LinearLayout
When you do setContentView(R.layout.activity_main); the system will parse the XML file and create all the views and subviews defined into it with the attibute and everything
For example if your XML is defined like that:
<RelativeLayout
...
>
<TextView
android:text="HelloWorld"
... />
</RelativeLayout>
When the view is inflated you'll end with a RelativeLayout object containing a TextView object containing "HelloWorld" in one of its fields.
In this example, the root view is the RelativeLayout

Why LayoutInflater.inflate returns the root passed as parameter?

I tried, in code, to inflate a view into a container and to get at the same time a reference to my inflated View (a Button). So the code was like this:
Button mybut = (Button) getLayoutInflater().inflate(resID, lyMain, true);
But this doesn't work: the returned result is not what I expected. Reading the doc at https://developer.android.com/reference/android/view/LayoutInflater.html, I've found that:
(inflate...returns)... The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.
Now the stupid question: why this method should return something I already know (when I pass a root and "true" to attach to it)? It is more probable that I need a reference to the just inflated View, rather than to the root/container I passed to the method.
It is indeed odd. But doing the following will have the exact same result, but will actually capture the inflated layout:
Button mybut = (Button) getLayoutInflater().inflate(resID, lyMain, false);
lyMain.addView(mybut);
So it does require an extra line of code, but that seems like a minor inconvenience to me compared to having to call findViewById or getView(index).
The documentation explains the difference fairly well, but I guess you've already read that:
root: Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)
I think the reason of this design choice is that your layout resource might not have a root itself.
That is the case with <merge> layouts, e.g.
<merge>
<View />
<Button />
</merge>
This can be inflated in a parent ViewGroup, but can’t be represented as a whole with a single View instance. Returning the parent ViewGroup seems the only choice here.

How to use getChildAt(..) on an Android Compound Control

I have a ViewGroup inside a layout inside my XML. It has children defined under it. In the ViewGroup, I can easily use getChildAt(..) to go through the children defined in XML.
activity_main.xml:
<Layout>
<ViewGroup>
<Child1> ...
</ViewGroup>
</Layout>
I want to replace that ViewGroup with a Compound Control that has a ViewGroup and some other controls. But I still need to be able to access its children.
I.e.
activity_main.xml:
<Layout>
<CompoundControl>
<Child1>...
</CompoundControl>
</Layout>
compound_control.xml:
<Layout>
<ViewGroup>
<Buttons or something>
</Layout>
But new if I call getChildAt(..) in the class for the compound control, of course it looks for children in compoun_control.xml, not children in activity_main.xml. Is there a way around this? Thank you.
You have to get the custom compound_control viewgroup and its children accessible in the activity class corresponding to the main_activity.xml. So you can give id to your compound control when replacing it with the viewgroup in main_activity . Later on if you want to access custom viewgroup you can get reference from the activity class. like
<Layout>
<CompoundControl id="xyz">
<Child1>...
</CompoundControl>
</Layout>
This id can be fetched in its activity class like.
CompoundControl a = (CompoundControl)findViewById(R.id.xyz);
a.getChildAt(..);

Using Android's <merge> XML element on rootview for activity's layout

I am using the following layout:
---------------------
FrameLayout
.>>. FrameLayout
.>>. FrameLayout
---------------------
This layout is set on some Activity using setContentView().
I just run lint to analyse my project and got the following message:
MergeRootFrame: FrameLayout can be replaced with <merge> tag
I understand and use the merge element in certain layouts but I fail to understand how can I be advised to merge the root element in my layout when there is no obvious parent to merge with, when I do the correction and run it all goes well but for some reason this feels a bit fishy.
Thanks!
Read the following post by Romain Guy:
http://www.curious-creature.org/2009/03/01/android-layout-tricks-3-optimize-part-1/
"the parent of an activity’s content view is always a FrameLayout"
Answered my question q:)
The window provided for your application by the system has a FrameLayout that is the parent for your Activity's view hierarchy.

Is it possible to get two Viewgroup in single xml?then how?

parent as relative layout i have textview (for title) and linearlayout for viewgroup in that by id am accessing viewgroup. and inside child linear layout i have textview and relative layout for image and textview .after viewgroup in my above linear layout i want to access one viewgroup how to do that?
Did not understand what do you want to achieve, but you must remember, that layout structure in Android is a tree. Every two View Groups can have a parent View Group above them. So just instantiate a root layout and then place any number of View Groups you need there. Good luck!

Categories

Resources