Malloc
Malloc

Reputation: 669

How to set android view's layout_alignParentEnd property by code?

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?

Upvotes: 8

Views: 3731

Answers (2)

Xaver Kapeller
Xaver Kapeller

Reputation: 49817

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.

Upvotes: 8

Ajeet
Ajeet

Reputation: 1560

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

Upvotes: 5

Related Questions