How to programmatically add 2 textview in a linear layout

13,722

Solution 1

Replace the RelativeLayout for a LinearLayout and add all TextView's to that. Dont forget to android:orientation="vertical" in the LinearLayout

Solution 2

for(int j=0;j<30;j++)
            {

                 LinearLayout childLayout = new LinearLayout(
                            MainActivity.this);

                    LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT);
                    childLayout.setLayoutParams(linearParams);

                    TextView mType = new TextView(MainActivity.this);
                    TextView mValue = new TextView(MainActivity.this);

                    mType.setLayoutParams(new TableLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT, 1f));
                    mValue.setLayoutParams(new TableLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT, 1f));

                    mType.setTextSize(17);
                    mType.setPadding(5, 3, 0, 3);
                    mType.setTypeface(Typeface.DEFAULT_BOLD);
                    mType.setGravity(Gravity.LEFT | Gravity.CENTER);

                    mValue.setTextSize(16);
                    mValue.setPadding(5, 3, 0, 3);
                    mValue.setTypeface(null, Typeface.ITALIC);
                    mValue.setGravity(Gravity.LEFT | Gravity.CENTER);

                    mType.setText("111");
                    mValue.setText("111");

                    childLayout.addView(mValue, 0);
                    childLayout.addView(mType, 0);

                    linear.addView(childLayout);
            }
Share:
13,722
Rethinavel
Author by

Rethinavel

Updated on June 17, 2022

Comments

  • Rethinavel
    Rethinavel almost 2 years

    enter image description here

    Hi everyone,

    I am getting the following fields "Name" and "Skoda" from API. There will be x number of items like this. As per design, i should show them as like in the following image.

    So, I decided to create two textview programmatically in a linear layout named "childLayout" like the following.

    -- RelativeLayout
    
      -- Linear Layout
            -- TextView  Textview --
      -- Linear Layout 
    
      -- Linear Layout
            -- TextView  Textview --
      -- Linear Layout      
    
      -- Linear Layout
            -- TextView  Textview --
      -- Linear Layout 
    
    --RelativeLayout
    

    But i am not getting the desired output. Please help me to fix this issue.

    Here is code :

    TextView mType;
    TextView mValue;        
    for (int i = 0; i < getDetailedDescAL.size(); i++) {
    
        LinearLayout childLayout = new LinearLayout(
                DetailedCategories.this);
    
        LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        childLayout.setLayoutParams(linearParams);
    
        mType = new TextView(DetailedCategories.this);
        mValue = new TextView(DetailedCategories.this);
    
        mType.setLayoutParams(new TableLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT, 1f));
        mValue.setLayoutParams(new TableLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT, 1f));
    
        mType.setTextSize(17);
        mType.setPadding(5, 3, 0, 3);
        mType.setTypeface(Typeface.DEFAULT_BOLD);
        mType.setGravity(Gravity.LEFT | Gravity.CENTER);
    
        mValue.setTextSize(16);
        mValue.setPadding(5, 3, 0, 3);
        mValue.setTypeface(null, Typeface.ITALIC);
        mValue.setGravity(Gravity.LEFT | Gravity.CENTER);
    
        mType.setText(getDetailedDescAL.get(i).getmPropertyType());
        mValue.setText(getDetailedDescAL.get(i).getmPropertyValue());
    
        childLayout.addView(mValue, 0);
        childLayout.addView(mType, 0);
    
        RelativeLayout.LayoutParams relativeParams = 
            new RelativeLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        relativeParams.addRule(RelativeLayout.BELOW);
        Details.addView(childLayout, relativeParams);
    
        // Details is the relative layout declared in XML
    
    }
    

    The output is :

    enter image description here

    It seems like the textviews are overriding. How to solve this.