Search code examples
androidandroid-layoutpositionandroid-linearlayoutcenter

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


Solution

  • 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