Android - fragments/custom views and layout width/height - android

When I create a layout for my fragment or my custom view I must define layout_width and layout_height. When I use this fragment or view in another layout, I must also define layout_width and layout_height.
Let's say that the root element of my fragment/view layout has layout_height="match_parent" and when I use this in another layout I have for example <fragment layout_height="wrap_content".
In this case the layout_height element is basically defined twice, once in the layout of the fragment/view itself and once in the layout where I'm using it, but with different values.
What happens to the value of layout_height of the root element of the fragment/view in this case? I don't understand how this works... does one override the other?

when you are using match_parent for parent layout it matches the parent width/height.Now when you are using another layout inside this parent layout
1.if you are setting wrap_content it will not totally use the height/width of parent as it's set to wrap_content
2.setting child layout width/height to match_parent then the child layout will stretch to entire parent limits

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.

Couldn't change the height and width of a layout

In background it is listed as a constraintLayout , I am trying to insert a Linear Horizontal Layout over it. Whenever I assign match_parent to both android:layout_width and android:layout_height , it automatically reverts back to some Fixed Dp (like 395dp for width and 587dp for height) . How to change this setting?
Screenshot Here
Match parent won't work inside a constraint layout, you have to define the relative position inside the constraint layout. You have to assign the layout_constraintStart_toStartOf and layout_constraintEnd_toEndOf

Difference in behavior of android:gravity="center_vertical" for RelativeLayout and LinearLayout

Let's say I have two views that I want to center vertically. The first view is bigger than the second view.
I noticed that if I place theses two views inside a RelativeLayout with properties layout_height="wrap_content" and android:gravity="center_vertical" nothing happens. This is what I get :
In the opposite, if I place these two views inside a LinearLayour with properties layout_height="wrap_content" and android:gravity="center_vertical" the views are centered vertically :
Lastly, if I place these two views inside a RelativeLayout with properties layout_height with a fixed height and android:gravity="center_vertical" I get the same result as the LinearLayout. The views are centered vertically.
I would expect the views to be centered vertically in each case. Do you know the reason for this difference?
LinearLayout handles all its child object based on its orientation (Horizontal or vertical). So when you are saying gravity: "center_vertical". You are actually referencing based on your parent layout.
In case of RelativeLayout,it enables you to specify the location of child objects relative to each other (child A to the left of child B) or to the parent (aligned to the top of the parent).
Personally I would use gravity only in LinearLayouts and the centerInParent for RelativeLayouts.
In your first case it'll work with RelativeLayout as you expected if you use android:layout_centerVertical="true" to the child view which you want to be centred.
So in case of LinearLayout you need to specify the orientation first (i.e. horizontal/vertical) so that the child views are inflated based on the reference of your parent layout.
While in RelativeLayout, as the name says it all, you can specify the position with respect to the views which are the child of a parent RelativeLayout.
Now the views you want to achieve can be generated in many other ways too.
For example, set your parent layout as LinearLayout. Don't specify any gravity attribute in the parent layout. Hence, you set a layout_gravity attribute to the child to certer_vertical and this should work too.
Well, after some others tests, it seems that the behavior of android:gravity for a RelativeLayout is a bit random. I will just avoid to use is.

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.

Hierachy of android:layout_width/ height between containers and views

What is the relationship between android:layout_width & android:layout_height in the container and the views contained within?
For instance if I have a LinearLayout with the layout width and height set to fill_parent, and I have a Button with those values set to wrap_content it uses the value from the Button, like the Button values overide the LinearLayout.
However if i swap them around so that the LinearLayout values are both wrap_content and the Button values are now fill_parent it still uses wrap_content, this time not overriding the values, but using the values supplied with the LinearLayout.
Could someone explain how they relate as this is very confusing to me?
Thanks
Kyros
http://developer.android.com/guide/topics/ui/how-android-draws.html
This article explains it very well

Categories

Resources