Change LayoutParams of LinearLayout that is inside of RelativeLayout - android

I have LinearLayout inside of RelativeLayout. I would like to change height of LinearLayout programmatically. I have tried do this with LinearLayout.LayoutParams but I've got an exception. How to solve this problem? Thanks.

Try with RelativeLayout.LayoutParams as the parent is a RelativeLayout.
The type of the LayoutParams depends on the parent type.

Related

set layout_below of RelativeLayout within a Relative Layout programmatically

I have a RelativeLayout #+id/main_layout.
Inside the #+id/main_layout, I have a TextView #+id/txt1 and a RelativeLayout #+id/store.
For some reason I have to set android:layout_below="#+id/txt1" to the inner RelativeLayout #+id/store programmatically. How to do that?
Can you try adding it like this:
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.BELOW, idOfTheViewBelow);
Of course, use parameters (fill parent etc.) which you need!
Edit: Of course, you don't need to create the RelativeLayout, just instantiate your own, and apply the rules you need!

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.

Setting RelativeLayout.ALIGN_PARENT_LEFT to LinearLayout

How can I set the RelativeLayout.ALIGN_PARENT_LEFT to LinearLayout in code? I have a RelativeLayout which child is LinearLayout and I need to set the RelativeLayout.ALIGN_PARENT_LEFT in the child LinearLayout instance. The problem is that when I set it, I will lose the child LinearLayout parameters (I will lose the weight). My understanding is that the only way to set the RelativeLayout.ALIGN_PARENT_LEFT to child element is to use RelativeLayout.Layoutparams when setting the child view layout parameters.
Thanks in advance!!
You have to set the width height parms and weight along with your aligh left rule in programtically
layoutparms = new LayoutParams(WindowManager.LayoutParams.FILL_PARENT,
WindowManager.LayoutParams.FILL_PARENT, weightthere);
layoutparms.addRule(RelativeLayout.LEFT_OF, leftisdeview);
yourview.setLayoutParams(layoutparms);
You just have to give android:gravity="left" to the parent linear layout.Remember that it is gravity not layout_gravity.

How to copy params from one LinearLayout and set to another LinearLayout in code?

How to copy params from one LinearLayout and set to another LinearLayout in code ?
I have tried like
LinearLayout.LayoutParams paramsR=(LinearLayout.LayoutParams) llRight.getLayoutParams();
LinearLayout.LayoutParams paramsL=(LinearLayout.LayoutParams) llLeft.getLayoutParams();
paramsR.height=paramsL.height;
paramsR.width=paramsL.width;
llRight.setLayoutParams(paramsR);
but it ignores at all. How to solve this problem ?
If the parent of llRight & llLeft is RelativeLayout then you should use:
RelativeLayout.LayoutParams paramsR=(RelativeLayout.LayoutParams) llRight.getLayoutParams();
RelativeLayout.LayoutParams paramsL=(RelativeLayout.LayoutParams) llLeft.getLayoutParams();
paramsR.height=paramsL.height;
paramsR.width=paramsL.width;
llRight.setLayoutParams(paramsR);

ClassCastException when adding LayoutParams

Which class do I have to use, when setting the LayoutParams of my LinearLayout? The following two are not working:
linLayout.setLayoutParams(new ViewGroup.LayoutParams(50,50));
linLayout.setLayoutParams(new android.widget.LinearLayout.LayoutParams(50,50));
if I get the params first, change them and set them back it's working. So I suppose the Params I get returned inherit from ViewGroup.LayoutParams...
Any idea? THX
Use the LayoutParams of the parent. So in case your LinearLayout is a child of RelativeLayout then you use
linLayout.setLayoutParams(new android.widget.RelativeLayout.LayoutParams(50,50));

Categories

Resources