Reputation: 38643
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
Reputation: 3443
use following method on your layout or on any view.
layout.setGravity(Gravity.CENTER_HORIZONTAL);
Upvotes: 3
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