Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically add 2 textview in a linear layout

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.

like image 569
Rethinavel Avatar asked May 27 '13 10:05

Rethinavel


1 Answers

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

like image 183
Jaytjuh Avatar answered Oct 13 '22 04:10

Jaytjuh