Center elements in LinearLayout after creating it programmatically
11,470
Solution 1
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
Solution 2
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);
Author by
Hubert Solecki
Updated on June 19, 2022Comments
-
Hubert Solecki almost 2 years
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 :)
-
Hubert Solecki over 8 yearsThanks a lot ! Generating layout and positionning is a bit tricky on runtime !
-
fersarr about 6 yearsthis is tricky but worked. Why is it needed on the layout itself and setting on the layoutparams doesnt have any effect?