setLayoutParams vs addView for adding a new view - android

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).

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)

Whats the purpose of "this" when create object in memory? - android

someone can help me with this?
I didn't understand two things.
one, is that thing:
RelativeLayout.LayoutParams center_ob_l = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
what is the meaning of WARP.CONTENT and why should I do this?
second thing, is the "this":
Button log_b = new Button(this);
why should i send "this" in those brackets?
and why at all I would want to create by myself the buttons and things instead of just go to the visual device and throw the things I want to the screen?
This is answer for "this":
Button btn = new Button(this); what is the use of "this" in this context....?
For understanding WRAP_CONTENT read this: https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
WRAP_CONTENT means that the relativelayout establish his size fitting his content.
this (java key) refers to current object, whatever it is, the current istance of the class you are coding.
if you don't need to build programmatically your layout, build it with visual editor, when you will need to create the layout programmatically you will understand it by yourself ;)
LayoutParams is used to tell how the view is going to be drawn, you are using this ViewGroup.LayoutParams(int width, int height) where WRAP_CONTENT is the width and height.
check this
https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
For Button log_b = new Button(this);
this keyword in java refers to the current class object.
Button(Context context) 'this' is the context you are passing in the constructor
check this
https://developer.android.com/reference/android/widget/Button.html
wrap_content
which means that the view wants to be just big enough to enclose its content (plus padding)
It's roughly the equivalent of setting a Windows Form Control's Autosize property to true
this
is required by the way android works with Context. Specifically when you are passing this you are basically passing the class that encapsulates this statement.

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

How to find LayoutParams of Android objects

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

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