Java – How do I set layout_alignParentEnd properties of an android view through code?

How do I set layout_alignParentEnd properties of an android view through code?… here is a solution to the problem.

How do I set layout_alignParentEnd properties of an android view through code?

How do I set the alignParentEnd property programmatically?

I

searched the entire Android developer site, but I couldn’t find any references to setting properties like alignParentEnd in the code. Only describes how to set them in XML. Where can I find such documentation in the future?

Solution

LayoutParams is used to set layout properties in code. alignParentEnd is a property of RelativeLayout, so you must use RelativeLayout.LayoutParams. You can find the documentation here


Therefore, to set properties in code, you can use the addRule() method of RelativeLayout.LayoutParams. For example you can do this:

// Create the LayoutParams
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

 Add all the rules you need
params.addRule(RelativeLayout.ALIGN_PARENT_END);
...

 Once you are done set the LayoutParams to the layout
relativeLayout.setLayoutParams(params);

You can find a list of all the rules that can be set using addRule(). here .

Related Problems and Solutions