My question is simple,
How to set my buttons layout_gravity programmatically?
I found this on internet, but it simply throws me a Nullpointer exception:
Button MyButton = new Button(this);
LinearLayout.LayoutParams lllp=(LinearLayout.LayoutParams)MyButton.getLayoutParams();
lllp.gravity=Gravity.RIGHT;
MyButton.setLayoutParams(lllp);
MyLinearLayout.addView(MyButton);
Any solution?
Best Answer
Java
Kotlin
For gravity values and how to set gravity check Gravity.
Basically, you should choose the
LayoutParams
depending on the parent. It can beRelativeLayout
,LinearLayout
etc...