How to Programmatically Set the Layout_Align_Parent_Right Attribute of a Button in Relative Layout

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

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

How to programmatically set the layout_align attribute of a Button in Relative Layout?

Try to add this

    android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"

This is the code

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
TextView textView = new TextView(this);
textView.setLayoutParams(layoutParams);

How to set the align parent right attribute of TextViews in relative layout programmatically

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)textView.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);

textView.setLayoutParams(params); //causes layout updat

Programmatically set attribute AlignParentRight to button in RelativeLayout is not working?

When you call b.getLayoutParams(), b is not in a layout. therefore, it does not have a layoutparams (plus, it wouldn't know that you are expecting a RelativeLayout.LayoutParams at this point).

You need to create a new LayoutParams instance, like you did for the TextView.

How to set layout_alignParentRight=false programmatically?

this code will set it false:

addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0)

How set all attributes of RelativeLayout Programmatically in Kotlin?

You need to cast LayoutParams as ConstraintLayout.LayoutParams to set those properties like below

class ButtonContainer(context: Context) : RelativeLayout(context) {

var button1 = CustomButton(context)

val buttonLayoutParams by lazy { ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT).apply {
bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
startToStart = ConstraintLayout.LayoutParams.PARENT_ID
topToTop = ConstraintLayout.LayoutParams.PARENT_ID
endToEnd = ConstraintLayout.LayoutParams.PARENT_ID
horizontalBias = 0.5F
}
}

override fun onFinishInflate() {
this.layoutParams = buttonLayoutParams
this.addView(button1)
}
}

Programmatically adding Buttons to Relative Layout

You are using the same RelativeLayout.LayoutParams params for all of your buttons. In Java all non primitive types assignments are made by reference, so when you do

RelativeLayout.LayoutParams updateParams = params;
RelativeLayout.LayoutParams installParams = params;
RelativeLayout.LayoutParams dlFamilyParams = params;

and so on, you are assigning the same object in memory to various variables. Variables are only textual references to certain memory addresses, so with different names you are referencing always to the same object in memory.

You set for each button some different layout params properties to position them into the view. But if we told we are referencing always to the same object in memory, then we are updating always the same object and so we are overriding previous settings and we are keeping only last settings.

Thus you are setting the same params object to all your buttons.

The combination of these errors results in having all your buttons with the same RelativeLayout.LayoutParams with his settings, they are placed one over the other and you can see only the last added.

You have to create a different params object for each button like follows:

RelativeLayout.LayoutParams updateParams = new RelativeLayout.LayoutParams(size.x / 5, 200);
...
RelativeLayout.LayoutParams installParams = new RelativeLayout.LayoutParams(size.x / 5, 200);
...

To analyze views problems I suggest you to use Layout Inspector tool, accessible by menu Tools -> Android

To align all buttons equally in relative layout

Alternative approach, maybe could be useful sometimes. Solution with LinearLayout and weights works fine, but I don't like extra layout inflates that could be avoided easily, because of performance issues.

1. Define button width in dimens.xml. Android phones have screen width 320 or 360dp:

values/dimen.xml

<dimen name="button_width">80dp</dimen>

values-sw360dp/dimen.xml (these resources will be used for screens 360dp width and more)

<dimen name="button_width">90dp</dimen>

Of course, you should define extra dimens.xml if tablet support needed.

2. Use this value for buttons:

android:layout_width="@dimen/button_width"


Related Topics



Leave a reply



Submit