How can I set the alignParentEnd property programmatically?
I searched all over the Android Developer website, but I couldn't find any reference to setting properties like alignParentEnd in code. It is only explained how to set them in XML. Where could I find documentation for something like this in the future?
LayoutParams are used to set layout properties in code. alignParentEnd is a property of RelativeLayout so you have to use RelativeLayout.LayoutParams. You can find the documentation here.
So to set the property in code you can use the addRule() method of the 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 rules which you can set with addRule() here.
Parameter are added in View and ViewGroup using LayoutParam.
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) my_child_view.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_END);
my_child_view.setLayoutParams(params);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With