Are layouts same as ViewGroups? - android

I'm a bit confused, when I declare a layout in XML, and I call the:
R.layout.idname
is this considered the ViewGroup?

It depends on the widget you declared inside your layout. For instance you can declare a single TextView inside your layout. TextViews are views, not ViewGroup. If you declare a LinearLayout for instance, it will be a ViewGroup. If you take a look to the documentation you can see the direct and indirect subclass of ViewGroup

is this considered the ViewGroup?
No, this is the complete layout file.
Are layouts same as ViewGroups?
No, one is the file. A ViewGroup would be any View such as a RelativeLayout, LinearLayout, etc... that holds other Views.
From the docs
A ViewGroup is a special view that can contain other views (called children.)

Not really. It depends on which xml layout you have given R.layout.idname to.
TextView, ImageView, EditText for examples are NOT viewgroups.
FrameLayout, RelativeLayout, LinearLayout etc are considered viewgroups.
A clue is in the name really... viewgroup. a view that can be a grouping of views.

No, Layouts are not same as ViewGroups. While every Layout is a ViewGroup, there are ViewGroups that aren't layouts (e.g. ViewPager, ScrollView). Regarding an XML file in R.layout, it depends on the root element of the XML: if for example it's a LinearLayout - you'll be able to cast it to ViewGroup, if it's an ImageView - it is considered a View.

View group: is a combination of views
Layouts: how views should sortup
View group has views inside it, but how the views should be arranged, the arrangement of views is known as layouts.
For examples, linear layout and relative layout are both layout and view group because they have views inside and the arrangement of views in them are known as layouts.

Related

What is the parent of the layout element?

I'm new to Android development so this might be an obvious question, but I've looked at multiple sources and can't find the answer. If you put widget elements inside a layout element then the parent of those widget elements is the layout element, right? So what is the parent element of the layout? Because in the default generated file, activity_main.xml (for the layout), there is the attribute android:layout_width="match_parent" for the topmost constraint layout element. So, what is the parent of this constraint layout element?
Your ConstraintLayout is the root layout of your Activity but it is just part of a layout created by the system.
You can for example get the reference of the parent layout of your ConstraitLayout:
ViewGroup layoutRoot = findViewById(android.R.id.content);
When you set match_parent, your View (a Layout is a ViewGroup which is just a View) will try to fill the space available in its parent layout. So, in a certain way, it just tell to how much your view can grow in the X/Y-axis. In your case, your ConstraintLayout will grow in order to match that android.R.id.content
Of course, there's one View on the top of the stack (probably the decorView of your Window) which is created by the System and is assuming whole area available on the Window created by the system.
Basically we use parent layout as Linear Layout/Relative Layou/ConstraintLayout/Frame Layout etc. So all are classes which extends ViewGroup. Please refer to Google developer documentation for better understanding.
Read this -
https://developer.android.com/reference/android/view/ViewGroup.html
match_parent is a fixed value for views to stretch according to their parents.
For example let's say you have a Textview inside a RelativeLayout. If the Textview is set to match parent then it would stretch according to its parent/rootview which is the Relative layout.
So basically it would have as the same height and width as the parent layout.

Define custom ViewGroup attributes for children

I am looking for a way to define custom attributes for views that are used by their children. For example layout_centerInParent for children of RelativeLayouts or layout_span for children of TableLayouts.
Just in case someone else gets here hoping for a direction like I did:
The layout parameters that are defined in the children are used in the parents. For example, layout_centerInParent is used by the RelativeLayout parent to position the child according to its value of this attribute.
In a custom ViewGroup you can create an inner class that extends ViewGroup.LayoutParams. Then you can use XML attributes within the children and retrieve them in the custom layout using the View#getLayoutParams() method.
You can look at this question and its answer.

Android SDK layout_width and layout_height xml attributes inheritance?

I've been trying to start programming with the Android OS. Using the tutorials on the website I see that such views as LinearLayout, GridLayout use layout_width and layout_height xml attributes but the documentation never shows the inheritance of these attributes for these layouts. I have read where they're required but don't see how they can use something they never inherit.
Hopefully a simple question that has been bugging me.
Check http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html and http://developer.android.com/guide/topics/ui/declaring-layout.html
XML layout attributes named layout_something define layout parameters for the View that are appropriate for the ViewGroup in which it resides.
Every ViewGroup class implements a nested class that extends ViewGroup.LayoutParams. This subclass contains property types that define the size and position for each child view, as appropriate for the view group. As you can see in figure 1, the parent view group defines layout parameters for each child view (including the child view group).
Note that every LayoutParams subclass has its own syntax for setting values. Each child element must define LayoutParams that are appropriate for its parent, though it may also define different LayoutParams for its own children.
All view groups include a width and height (layout_width and layout_height), and each view is required to define them.
These attributes are inherited by LayoutParams from ViewGroup.LayoutParams: LinearLayout.LayoutParams, GridLayout.LayoutParams, etc.

Which Layout supports android:layout_gravity?

I came through many examples in internet.I found that neither Relative Layout nor Linear Layout supports android:layout_gravity.By it I mean the views inside these layouts does not support android:layout_gravity attribute. So any one having idea which layout supports android:layout_gravity and how to use it in better way?
Children (that is, direct descendants in the View hierarchy) of LinearLayout do use layout_gravity (see LinearLayout.LayoutParams), but only on the "secondary" axis. So, in a vertical LinearLayout, center_horiztonal will work, but center_vertical will do nothing.
Children of FrameLayout also support layout_gravity (see FrameLayout.LayoutParams).
Keep in mind that the layout_* parameters set values in a LayoutParams object provided by the view parent. So a layout_* parameter will only have an effect if the parent view supports the parameter.
Actually if you use RelativeLayout you don't need to use layout_gravity.Better way to position your layout's elements are android. Here you can get a good explanation how to use RelativeLayout.

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