6

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.

1
  • Try to add your views in a LinearLayout with vertical orientation instead of in a RelativeLayout. Commented May 27, 2013 at 10:19

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it worked fine. I just changed the parent layout as linear layout. :-)
1
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);
            }

Comments

0

Change your Relative Layout to Linear Layout and it's solved

1 Comment

Thanks it worked fine. I just changed the parent layout as linear layout. :-) with android:orientation="vertical"
0

JFI, why don't you simply use ListView. In post, you mentioned that number of items is unknown. So there can be case that, you will have number of items more than what you can have on visible screen space. Listview will handle scrolling for you.

P.S.: Here, instead of Relative or LinearLayout, use scrollView

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.