jax
jax

Reputation: 38643

android:layout_gravity="center_horizontal" in Java code

I have added a Button to a view in java code. Basically I want to add the xml

android:layout_gravity="center_horizontal"

to the Button but do this in Java code, how is this done?

Upvotes: 0

Views: 7403

Answers (2)

kaushal trivedi
kaushal trivedi

Reputation: 3443

use following method on your layout or on any view.

layout.setGravity(Gravity.CENTER_HORIZONTAL);

Upvotes: 3

Balaji
Balaji

Reputation: 2026

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
        ( (int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);

params.addRule(RelativeLayout.CENTER_HORIZONTAL);

button.setLayoutParams(params);

Upvotes: 1

Related Questions