Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set layout_gravity programmatically?

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?

like image 234
Adam Varhegyi Avatar asked Nov 08 '11 11:11

Adam Varhegyi


People also ask

How to set Layout Gravity in java?

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

What is layout params?

LayoutParams are used by views to tell their parents how they want to be laid out. See ViewGroup Layout Attributes for a list of all child view attributes that this class supports. The base LayoutParams class just describes how big the view wants to be for both width and height.

What is true about Layout_gravity attribute in Linearlayout?

The difference between the gravity and layout_gravity XML attributes in Android is that the android:gravity attribute is used to arrange the position of the content inside a View (for example text inside a Button widget) while the android:layout_gravity is used to arrange the position of the entire View relative to ...


2 Answers

Java

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

Kotlin

val params = LinearLayout.LayoutParams(     LinearLayout.LayoutParams.WRAP_CONTENT,     LinearLayout.LayoutParams.WRAP_CONTENT ).apply {     weight = 1.0f     gravity = Gravity.TOP } 

For gravity values and how to set gravity check Gravity.

Basically, you should choose the LayoutParams depending on the parent. It can be RelativeLayout, LinearLayout etc...

like image 156
Karthi Avatar answered Sep 19 '22 08:09

Karthi


I'd hate to be resurrecting old threads but this is a problem that is not answered correctly and moreover I've ran into this problem myself.

Here's the long bit, if you're only interested in the answer please scroll all the way down to the code:

android:gravity and android:layout_gravity works differently. Here's an article I've read that helped me.

GIST of article: gravity affects view after height/width is assigned. So gravity centre will not affect a view that is done FILL_PARENT (think of it as auto margin). layout_gravity centre WILL affect view that is FILL_PARENT (think of it as auto pad).

Basically, android:layout_gravity CANNOT be access programmatically, only android:gravity. In the OP's case and my case, the accepted answer does not place the button vertically centre.

To improve on Karthi's answer:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.gravity = Gravity.CENTER; button.setLayoutParams(params); 

Link to LinearLayout.LayoutParams.

android:layout_gravity shows "No related methods" meaning cannot be access programatically. Whereas gravity is a field in the class.

like image 27
Uknight Avatar answered Sep 19 '22 08:09

Uknight