android explaination addview & layoutparams during runtime - android

just want to know what happens when I set new layout params to a new instance of an ImageView and then add it on an empty layout. It seems like doing it reset the parent layout hosting the new ImageView and also reset position and size of every sibling ImageView.
So what happens? Parent layout takes the layoutparams of the child?
thanks

Layout params have direct affect to views which use that params. But that also affects other views under same parent. Think about width, height and weight attrs of linear layout params. For example, if you set a layout params which has a weight attr defined, this will all change the visual of your parent.
From the doc:
LayoutParams are used by views to tell their parents how they want to
be laid out.
Note:
A parent is a view group, a container for child views. There are some types of view groups, ie LinearLayout, RelativeLayout etc(subclasses of ViewGroup can be googled), and they have specific layout behaviours and LayoutParams. While adding views to view groups layout params are used (both on xml and programmatically) for setting position, width, height inside that view group(container).

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.

Different layoutparams for different views

Is it possible to set one LayoutParams to RelativeLayout(it is inside another Layout) and set another ones to a view that is in this Layout.
RelativeLayout inside which another Relative Layout with some LayoutParams and inside 2nd Reltive layout a view with another LayoutParams
Is situation like possible?
try by,
1.giving id to each views you wanna resize
2.initialising those ids to a member variable
3.setting the layoutparams using setLayoutParams(left,top,right,bottom) method.

How to indicate position to the center of the parent in java code?

I am confused on the following:
If I have a LinearLayout as part of a hierarchy and I want to programmatically change its position so that it is in the center of the parent layout how can I do this?
There are too many gravity attributes.
Should I update the gravity of the layout params of the item or of the parent?
If the parent is a LinearLayout you can use android:layout_gravity="center" on that view to center it in its parents view.
If the parent is a RelativeLayout you can use
android:layout_centerInParent="true"
I would generally use a RelativeLayout as a parent view if you want to center views inside it

How to force onClick to take parent view width as clickable area?

I have an activity contains a parent view along with some dynamically created children views, according to my app needs, these views width are set dynamically through the run-time so that I can't set it to match_parent, the problem here is that I want to assign an onClickListener to these views but want to make it functionable through the whole parent view width, to be more specific look at the following picture:
in the past picture the each View width is represented in red, and all I want is to assign each View onClick function to the whole width represented in green.
is that possible?
You can set LayoutParams to your View
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
yourparentview.setContentView(yourchildview, layoutParams);

LinearLayoutParams are causing a casting issue

I must be missing something really stupid.
I have a LinearLayout in my view:
_editTextViews = (LinearLayout)findViewById(R.id.editDesAndLocViews);
I am trying to dynamically change the height of that view by setting the LayoutParams
I have two LinearLayout.LayoutParams
LinearLayout.LayoutParams collapsedParams;
LinearLayout.LayoutParams openParams;
that are set in the constructor like so:
collapsedParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,0);
openParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
But as soon as i try to apply one of those params to my LinearLayout, like so:
_editTextViews.setLayoutParams(collapsedParams);
I get the following FATAL EXCEPTION:
At first i thought my project needed to be cleaned as it seems to think i am trying to force a relative layout to use linear layout params, but everything is LinearLayout. The LinearLayout in question does contain a couple RelativeLayouts. I don't know if / why that would be the problem.
please help
Change your LayoutParamss' type to either ViewGroup.LayoutParams or RelativeLayout.LayoutParams.
The LayoutParams you apply have to match the parent layout of the view. LayoutParams define how a View is layed out in its parent view (layout). Because each layout type takes different parametres for laying out a view (e.g. only LinearLayout has weight, only RelativeLayout can use anchors), they all have their own LayoutParams.

Categories

Resources