Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center elements in LinearLayout after creating it programmatically

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 :)

like image 768
Hubert Solecki Avatar asked Sep 30 '15 14:09

Hubert Solecki


People also ask

How do you center elements in LinearLayout?

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.

How do you align text in LinearLayout?

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".

How do I center a view programmatically in android?

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.


2 Answers

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

like image 140
Pavan Avatar answered Sep 23 '22 23:09

Pavan


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);
like image 37
Jordi Castilla Avatar answered Sep 20 '22 23:09

Jordi Castilla