How to programmatically set the layout_align_parent_right attribute of a Button in Relative Layout?
Asked Answered
C

5

302

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?

Creationism answered 9/1, 2011 at 11:12 Comment(0)
H
667

You can access any LayoutParams from code using View.getLayoutParams. You just have to be very aware of what LayoutParams your accessing. This is normally achieved by checking the containing ViewGroup if it has a LayoutParams inner child then that's the one you should use. In your case it's RelativeLayout.LayoutParams. You'll be using RelativeLayout.LayoutParams#addRule(int verb) and RelativeLayout.LayoutParams#addRule(int verb, int anchor)

You can get to it via code:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);

button.setLayoutParams(params); //causes layout update
Haystack answered 9/1, 2011 at 12:4 Comment(6)
what if the anchor has no id? params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);TextView tvUserName = new TextView(act);Diazine
how about adding the ID?Spoliate
What if button is on a RecycleView? Isn't it a problem adding same or opposite rules to the same widget?Producer
Its working only for once, if I change alignment its not getting updated. May I know what can be the issue ??Polaris
I tried using that but it goes red in my code for addRule ?Seow
Sorry i down voted, that was an accident and I discovered it 2 weeks later...Signalment
M
15

For adding a RelativeLayout attribute whose value is true or false use 0 for false and RelativeLayout.TRUE for true:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) button.getLayoutParams()
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.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 use removeRule(verb) which is just a shortcut for addRule(verb, 0).

Matchlock answered 10/6, 2018 at 5:43 Comment(0)
F
13
  1. you need to create and id for the buttons you need to refference: btn1.setId(1);
  2. you can use the params variable to add parameters to your layout, i think the method is addRule(), check out the android java docs for this LayoutParams object.
Fluidize answered 9/1, 2011 at 11:27 Comment(3)
I would highly appreciate any piece of code. I dont find any method in button.Creationism
Setting id doesn't help since it's expecting a real resource id instead of just a number since setId is being annotated with @IdRes.Steamship
that's new then, in the past you could take a resource number from layouts for example and use it as a base for resource id that will not collide.i guess google must have blocked that/Fluidize
L
2

Kotlin version:

Use these extensions with infix functions that simplify later calls

infix fun View.below(view: View) {
    (this.layoutParams as? RelativeLayout.LayoutParams)?.addRule(RelativeLayout.BELOW, view.id)
}

infix fun View.leftOf(view: View) {
    (this.layoutParams as? RelativeLayout.LayoutParams)?.addRule(RelativeLayout.LEFT_OF, view.id)
}

infix fun View.alightParentRightIs(aligned: Boolean) {
    val layoutParams = this.layoutParams as? RelativeLayout.LayoutParams
    if (aligned) {
        (this.layoutParams as? RelativeLayout.LayoutParams)?.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
    } else {
        (this.layoutParams as? RelativeLayout.LayoutParams)?.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0)
    }
    this.layoutParams = layoutParams
}

Then use them as infix functions calls:

view1 below view2
view1 leftOf view2
view1 alightParentRightIs true

Or you can use them as normal functions:

view1.below(view2)
view1.leftOf(view2)
view1.alightParentRightIs(true)
Lindsay answered 28/4, 2020 at 21:36 Comment(0)
C
1

In Kotlin:

val params = mBinding.tvTotalAmount.layoutParams as RelativeLayout.LayoutParams

params.addRule(RelativeLayout.ALIGN_PARENT_END)

mBinding.tvTotalAmount.layoutParams = params

Creolacreole answered 4/8, 2022 at 9:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.