Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Align Parent Bottom + Bottom margin programmatically

How can I programatically add a RelativeLayout that is aligned to the bottom of parent, and add a margin or padding in the same RelativeLayout?

Example:

enter image description here

like image 235
Igor Ronner Avatar asked Aug 25 '14 14:08

Igor Ronner


Video Answer


2 Answers

Here's how you can do that:

// Get the parent layout
RelativeLayout parent = (RelativeLayout) findViewById(R.id.parent);

// Create your custom layout
RelativeLayout relativeLayout = new RelativeLayout(this);
// Create LayoutParams for it // Note 200 200 is width, height in pixels
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(200, 200);
// Align bottom-right, and add bottom-margin
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.bottomMargin = 100;

relativeLayout.setLayoutParams(params);
relativeLayout.setBackgroundColor(Color.BLUE);
// Add the custom layout to your parent layout
parent.addView(relativeLayout);
like image 102
Simas Avatar answered Oct 28 '22 12:10

Simas


you can try this it work in my case:

RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) view.getLayoutParams();
                                // position on right bottom
                                rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
                                rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP,0);
                                rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                                rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
like image 24
JCDecary Avatar answered Oct 28 '22 11:10

JCDecary