Skip to content Skip to sidebar Skip to footer

Dynamically Changing The Position Of A Button With Respect To Another Button

I had seen a couple of examples for setting the relative position through program (from java) for views in android. But in my particular case i have 2 buttons (which are not views)

Solution 1:

You can use LayoutParams object and add rules to that object as per your requirement. and then setlayoutparam to your button.

For example:

            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(80,80); // size of button in dpparams.addRule(RelativeLayout.LEFT_OF, R.id.btnAdd);
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            params.setMargins(0, 0, 20, 60);
            btnMyLocation.setLayoutParams(params);

Solution 2:

You can add rules to set position dynamically.

RelativeLayout.LayoutParamsp=newRelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);

p.addRule(RelativeLayout.BELOW, R.id.button_rate);
buttonTag.setLayoutParams(p);

Post a Comment for "Dynamically Changing The Position Of A Button With Respect To Another Button"