2

I am using relative layout programmatically. It has multiple textviews but It shows horizontal not vertical. How do i get them as of paragraph view programmatically?

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
                params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);


    int x=10;
    int y=30;
    for(int i=0;i<arrayList.size();i++)
    {
                   String str=arrayList.get(i);
        int strLength=arrayList.size();
        if(arrayList.get(i).equals("Name")){
            y=y+10;
            x=65;
        }
        else
        {
            x=x+strLength+60;
        }

        //tv=(TextView) findViewById(R.id.tv1);
        tv=new TextView(this);
        tv.setText(arrayList.get(i).toString());
        tv.setPadding(x, y, 0, 0);
        layout.addView(tv, params);

    }
     scroll.addView(layout);
    this.setContentView(scroll);
Cœur
  • 37,241
  • 25
  • 195
  • 267
user2381124
  • 125
  • 1
  • 2
  • 9

2 Answers2

2

RelativeLayout has no property named Orientation. You will have to give id to each of your textviews and add the rule like this.

tv.addRule(RelativeLayout.BELOW, someOtherView.getId());

So in your case, I think it will be something like:

for(int i=0;i<arrayList.size();i++)
    {
        String str=arrayList.get(i);
        int strLength=arrayList.size();
        if(arrayList.get(i).equals("Name")){
            y=y+10;
            x=65;
        }
        else
        {
            x=x+strLength+60;
        }

        //tv=(TextView) findViewById(R.id.tv1);
                    tv=new TextView(this);
                    tv.setId(i);
                    tv.setText(arrayList.get(i).toString());
                         tv.setPadding(x, y, 0, 0);
                    layout.addView(tv, params);
                    if (i>0)
                        tv.addRule(RelativeLayout.BELOW, i-1);
    }

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
0

You just need to add rule to params RelativeLayout.BELOW

Try like this...

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
                    params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);


        int x=10;
        int y=30;
        for(int i=0;i<arrayList.size();i++)
        {
                       String str=arrayList.get(i);
            int strLength=arrayList.size();
            if(arrayList.get(i).equals("Name")){
                y=y+10;
                x=65;
            }
            else
            {
                x=x+strLength+60;
            }

            //tv=(TextView) findViewById(R.id.tv1);
            tv=new TextView(this);
            tv.setText(arrayList.get(i).toString());
            tv.setPadding(x, y, 0, 0);
           if(layout.getChildCount() != 0)
                params.addRule(RelativeLayout.BELOW, (layout.getChildAt(layout.getChildCount() - 1)).getId());
            layout.addView(tv, params);

        }
         scroll.addView(layout);
        this.setContentView(scroll);

Hope this helps...

CRUSADER
  • 5,486
  • 3
  • 28
  • 64
  • Thanks..It works proper.. But if I want to show these textviews as paragraph style what should I do? – user2381124 Jun 27 '13 at 07:27
  • If this solves the problem, then mark one of below answer which you find most acceptable as correct for others to benifit.. As for your query refer the [discussion here](http://stackoverflow.com/q/1529068/2345913) – CRUSADER Jun 27 '13 at 07:35