How do I apply layout_gravity as a code?

Asked 2 years ago, Updated 2 years ago, 79 views

How do I apply layout_gravity from button to code? I ran the example I found Only NullPointer errors appear.

Button MyButton = new Button(this);

    LinearLayout.LayoutParams  lllp=(LinearLayout.LayoutParams)MyButton.getLayoutParams();
    lllp.gravity=Gravity.RIGHT;
    MyButton.setLayoutParams(lllp); 


    MyLinearLayout.addView(MyButton);

android-layout android

2022-09-22 22:16

1 Answers

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
params.weight = 1.0f;
params.gravity = Gravity.TOP;

button.setLayoutParams(params);

You can do it like this.


2022-09-22 22:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.