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?
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?
use following method on your layout or on any view.
layout.setGravity(Gravity.CENTER_HORIZONTAL);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
( (int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
button.setLayoutParams(params);