I have a relative layout which I am creating programmatically:
RelativeLayout layout = new RelativeLayout( this );
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
Now I have two buttons which I want to add in this relative layout. But the problem is both buttons are being shown on the left of the RelatiiveLayout overlapping on each other.
buttonContainer.addView(btn1);
buttonContainer.addView(btn2);
Now I want to know how can I programmatically set the the android:layout_alignParentRight="true
"
or android:layout_toLeftOf="@id/btn"
attribute of buttons as we do in the xml?
For adding a
RelativeLayout
attribute whose value is true or false use0
for false andRelativeLayout.TRUE
for true:It doesn't matter whether or not the attribute was already added, you still use
addRule(verb, subject)
to enable/disable it. However, post-API 17 you can useremoveRule(verb)
which is just a shortcut foraddRule(verb, 0)
.btn1.setId(1);
addRule()
, check out the android java docs for thisLayoutParams
object.You can access any
LayoutParams
from code usingView.getLayoutParams
. You just have to be very aware of whatLayoutParams
your accessing. This is normally achieved by checking the containingViewGroup
if it has aLayoutParams
inner child then that's the one you should use. In your case it'sRelativeLayout.LayoutParams
. You'll be usingRelativeLayout.LayoutParams#addRule(int verb)
andRelativeLayout.LayoutParams#addRule(int verb, int anchor)
You can get to it via code: