I'm creating a LinearLayout on Runtime in which I'm adding a two elements: a textview and a spinner. I would like to center them horizontally in the LinearLayout, but cannot figure out how to do it. Below, the code that I use to create my views:
LinearLayout leftSideAttributLayout = new LinearLayout(this);
leftSideAttributLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams attributLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
leftSideAttributLayout.setLayoutParams(attributLayoutParams);
LinearLayout.LayoutParams leftLabelParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
leftLabelParams.gravity = Gravity.CENTER;
leftAttributLabel.setLayoutParams(leftLabelParams);
TextView leftAttributLabel = new TextView(this);
leftAttributLabel.setText(attribut.getNom());
leftAttributLabel.setTextColor(Color.WHITE);
Tools.applyFont(getApplicationContext(), leftAttributLabel, "gothic_0.TTF");
LinearLayout.LayoutParams spinnerParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
spinnerParams.gravity = Gravity.CENTER_HORIZONTAL;
spinnerParams.setMargins(10, 0, 0, 0);
Spinner leftAttributValues = new Spinner(this);
leftAttributValues.setLayoutParams(spinnerParams);
leftAttributValues.setAdapter(adapter);
leftAttributValues.setBackgroundResource(R.drawable.lenti_attributspinner);
leftAttributValues.setTag(attribut);
Would be great if someone could help me with that ! Thanks :)
To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”. Let us create an Android application with LinearLayout containing two Button widgets as children. We shall center align these children using gravity attribute.
If in linearlayout your orientation vertical, you can put the textview in the "horizontal center" by android:layout_gravity="center" . For centering textview vertically you need to set layout_height of textview to match_parent and set android:gravity to "center".
You can specify the gravity as center_horizontal, center_vertical and center (which centers both horizontally and vertically). On the child views of the LinearLayout you can specify the layout_gravity property, which also supports center_horizontal, center_vertical and center.
because you have to set gravity to linearlayout itself so just put this line after setting your linear layout parameter
leftSideAttributLayout.setGravity(Gravity.CENTER);
here is sample from your code
LinearLayout leftSideAttributLayout = new LinearLayout(this);
leftSideAttributLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams attributLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
attributLayoutParams.gravity = Gravity.CENTER;
leftSideAttributLayout.setLayoutParams(attributLayoutParams);
leftSideAttributLayout.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams leftLabelParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//leftLabelParams.gravity = Gravity.CENTER;
TextView leftAttributLabel = new TextView(this);
leftAttributLabel.setText("sample");
leftAttributLabel.setTextSize(25);
leftAttributLabel.setLayoutParams(leftLabelParams);
LinearLayout.LayoutParams spinnerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
// spinnerParams.gravity = Gravity.CENTER;
spinnerParams.setMargins(10, 0, 0, 0);
Spinner leftAttributValues = new Spinner(this);
leftAttributValues.setLayoutParams(spinnerParams);
leftSideAttributLayout.addView(leftAttributLabel);
leftSideAttributLayout.addView(leftAttributValues);
mainview.addView(leftSideAttributLayout);
hope it helps
Problem is you get the params from the Layout
, not from the Spinner
, so your attribute size is fullscreen, which makes gravity modifications without effect.
Use this:
Spinner leftAttributValues = new Spinner(this);
leftAttributValues.setAdapter(adapter);
leftAttributValues.setBackgroundResource(R.drawable.lenti_attributspinner);
leftAttributValues.setTag(attribut);
// get actual spinner params
LayoutParams params = (LayoutParams) leftAttributValues.getLayoutParams();
// modify desired params
params.gravity = Gravity.CENTER_HORIZONTAL;
params.setMargins(10, 0, 0, 0);
// set modified params to spinner again
leftAttributValues.setLayoutParams(params);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With