How to find LayoutParams of Android objects - android

This is a ridiculously simple question. I am trying to find a list of LayoutParams for an ImageButton. Again the documentation at developer.android.com is driving my insane (it has to be the worst in the known universe).
Can someone please point me at the reference that will explain/list the layoutparams.

There is no "list of LayoutParams for an ImageButton". An ImageButton is not a ViewGroup, so does not have LayoutParams associated with it. The LayoutParams are for the parent of the view, to tell it how it should perform layout of the view. So if you have your ImageButton inside of a LinearLayout, then the LayoutParams on it are LinearLayout.LayoutParams.
Put another way, there are an infinite number of possible LayoutParams for ImageButton, because you can make an infinite number of possible layout managers to put it in. :)

see, reference and let us know, what exactly you face issue in getting layout params, and to get layout params, use
view.getLayoutParams() method.
http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html

Related

Android Studio how do I change layout_alignParentEnd for a view to be false programmatically?

So suppose I have a textview called tvPlaceholder with the following property android:layout_alignParentEnd="true". How would I go about setting this to be false in the code (Kotlin)? I know you can change things like visibility, text size, e.t.c. in the code but could not find a definitive answer for layout_alignParentEnd. I am also calling tvPlaceholder from a viewholder if that matters at all: viewholder.tvPlaceholder.
When a view is added to a parent, it adds a LayoutParams object that defines any rules for laying it out. You can get this object by calling getLayoutParams(). Each layout parent has their own layout params subclass, I'm assuming this is a relative layout. So for a RelativeLayout, the LayourParams has a function removeRule that can do this.
val params = view.getLayoutParams() as RelativeLayout.LayoutParams
params.removeRule(RelativeLayout.ALIGN_PARENT_END)

What LayoutParams do or pass in Android?

I read often something like this:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
What exactly LayoutParams do?
I've read the Documentary but I wasn't smarter after reading!
Hope someone can explain me what LayoutParams do or pass!
Kind Regards!
LayoutParams are the Java Object representation of all the params you give to your View in the .xml layout file, like layout_width, layout_height and so on. Getting this object from a View allows you to look up those params on runtime, but also to change them in your Java code, when you need to move the View, change it's size etc.
LayoutParams are used by views to tell their parents how they want to be laid out.
The base LayoutParams class just describes how big the view wants to be for both width and height. For each dimension, it can specify one of:
FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding)
WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content.
That's all folks.
LayoutParams is use for the dynamically change the layout width and height. and also use the create custom view without the xml by using the directly by use of the LayoutParams for Relative or Linear type layout.
Basically when you set an xml with 'match_parent' or anything like layout_something
the android inflator will set the layout param for the child with the appropriate Layout params with the type matching the parent control, you could also do this in code and if you forget or set the wrong type you will get an exception in runtime.
the parent control needs this information to layout the child control correctly and to his liking.
Please see the following: Android Developer site - Layout Params
I think this picture says it all

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.

setLayoutParams vs addView for adding a new view

What is the difference between using setLayoutParams and supplying the parameters to addView?
I understand that addView only works when adding the view for the first time.
I found two ways of apparently doing the same thing:
tv.setLayoutParams(params);
layout.addView(tv)
vs
layout.addView(tv, params)
Are they equivalent?
If not what are the differences?
Are they equivalent?
Yes, the first method does an extra check to see if the View which is being added has LayoutParams set on it(and generate some LayoutParams if they don't exist). You could choose either one(I would pick the second method call).

Remove rule from RelativeLayout

I have bookmark ImageButton with ALIGN_WITH_PARENT set to true in XML.
If I programatically do (I want to remove that rule)
RelativeLayout.LayoutParams params = (LayoutParams) bookmark.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
bookmark.setLayoutParams(params);
The rule just doesn't apply (in my layout I can see that rule is still active) -> layout isn't good. If I manually do it (some sort of "simulation", set ALIGN WITH PARENT to false) in my Layout Editor, my layout is fine which leads to this code up there. Something is wrong.
What?
(I believe temporary) Solution is to wrap up that ImageButton in one dummy RelativeLayout and then take the params of that dummy layout and add or remove rules. That way it's working just fine.
Insead of calling setLayoutParams(), try to use requestLayout() method:
bookmark.requestLayout();
From API docs:
Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree.
simply call removeRule() function on params.
for example,
params.removeRule(RelativeLayout.CENTER_IN_PARENT);

Categories

Resources