Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

android

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?

like image 545
Malloc Avatar asked Mar 27 '15 07:03

Malloc


2 Answers

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.

like image 112
Xaver Kapeller Avatar answered Oct 03 '22 15:10

Xaver Kapeller


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);
like image 21
Ajeet Avatar answered Oct 03 '22 14:10

Ajeet