Remove rule from RelativeLayout - android

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

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

Fragment inflating onCreateView parameters

inflater.inflate(R.layout.fragmenta , container , false);
We use this line of code for inflating. Despite read documentary , i didn't understand what is the second and third parameters function is ? What is the root in this situation and what happen if i change third to true ?
In your layout XML file some of the attributes relate to the parent container. Anything that starts with layout_*. The container, or parent in some cases, is needed to resolve those values. You can see this with things like layout_margin. If you don't give the inflater a parent you won't have any margins. But if you wrap your layout in another container like a frame layout it works. That's because it's able to resolve layout_margin using the frame layout. I'm not completely sure about the attach to parent boolean. I always get Exceptions when I set it to true.
Layout Inflater
The video should give you a good explanation of what is done and why it is done.

Why can't I set the gravity for my view

I want to set the gravity of a view (in my .java file) but for some reason it can't be done this time (it states that there is no such method). But normally setGravity(Gravity.CENTER); will just be considered valid code.
So why can't it be done all the time? What should I do when I can't set the gravity of a view but I still want to align it to mid( like setGravity(Gravity.CENTER);)?
AScrollView middle = new AScrollView(context);
RelativeLayout.LayoutParams formid =
new RelativeLayout.LayoutParams(width,LayoutParams.WRAP_CONTENT);
middle.setLayoutParams(formid);
middle.setGravity(Gravity.CENTER); //This can't be done..
I can't say exactly why you can't set Gravity on ScrollView but I'm pretty sure you can't. However, if you have a ScrollView then you should have some Layout or content inside of it to scroll. Simply set the Gravity on that Layout and it should give you the effect you want
use this way..First add the view middle then after try to set gravity
formid.addView(middle) ;
middle.setGravity(Gravity.CENTER);
Layout Gravity is not supported by RelativeLayout. It is supported e.g. by LinearLayout where you can set it via the public property gravity of the LinearLayout.LayoutParams class.
try this code.
ScrollView middle = new ScrollView(this);
middle.setLayoutParams(newLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.CENTER));
let me know the status.

Set attributes (margin, gravity, etc...) to an Android view programmatically (without XML)

I need to create a GUI (layout+views) in my .java activity class (I know it's far more flexible and easier to use .xml layout file, but I don't want to use it for now).
I can't find any setGravity() (but a "Gravity" object I can't figure how to use) or any set setMargin() method for the "View" object.
What is the easiest way to do it ?
Thanx.
For setting the margin on component. The following leaves the existing margins as previously set and sets the left margin as zero.
TextView title = ((TextView)findViewById(R.id.default_panel_title))
final ViewGroup.MarginLayoutParams lpt =(MarginLayoutParams)title.getLayoutParams();
lpt.setMargins(0,lpt.topMargin,lpt.rightMargin,lpt.bottomMargin);
title.setLayoutParams(lpt);
You can add gravity to the "layouts" not to the "controls". Try to set gravity to any of your Linear/Relative or Frame layouts using setGravity(); .
Eg:
LinearLayout lll = (LinearLayout) findViewById(R.id.layoutname);
lll.setGravity(Gravity.CENTER);

Categories

Resources