10

I have an array of length n, I now need to create n number of LinearLayouts and add different stuffs on each of them. How can it be done dynamically?

3 Answers 3

9
LinearLayout lLayout = new LinearLayout(context);
parentWidget.addView(lLayout);
Sign up to request clarification or add additional context in comments.

4 Comments

And somewhere in there you assign all of its properties (assuming you need something other than defaults).
You also can set LayoutParams for child LinearLayout. lLayout.setLayoutPatams(layoutParams),
@ihrupin how to get parentWidget? I'm inside an onCreate method of a class which inherits Activity class and view is setContentView(R.layout.someLayout)
LineatLayout parentWidget = new LinearLayout(context); setContentView(parentWidget); Thats all :)
6

The easiest way is to create a layout in xml and inflate it using

LayoutInflater.from(context).inflate(R.layout.my_linear_layout);

You may also want to setId() your added views so you can access them easily later on.

Comments

5

I solved it using RelativeLayout which I found a little easier to work with. Yes of-course like the guys pointed out above I used setId(). Here is the code I implemented:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ScrollView sv = new ScrollView(this);

    //Parent RelativeLayout
    parentLayout = new RelativeLayout(this);
    parentLayout.setBackgroundColor(Color.WHITE);
    params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    parentLayout.setLayoutParams(params);
    sv.addView(parentLayout);

    final String[] comList = getCommunication();
    int listLength=0;
    try{
    listLength= comList.length/3;
    }catch(Exception e){System.out.println(e);System.exit(0);}

    childLayout= new RelativeLayout[listLength] ;
    TextView[] tvName  = new TextView[listLength];
    TextView[] tvDate  =new TextView[listLength];
    TextView[] tvMsg =new TextView[listLength];

    for(int i =0;i<listLength;i++){
        try{

        childLayout[i] = new RelativeLayout(this);
        childLayout[i].setPadding(5, 5, 5, 5);
        params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 75);
        if(i==0){params.addRule(RelativeLayout.BELOW);}
        else{params.addRule(RelativeLayout.BELOW,i);}
        childLayout[i].setId(i+1);
        childLayout[i].setClickable(true);
        childLayout[i].setLayoutParams(params);
        childLayout[i].setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {


                //Create the intent
                  Intent i = new Intent("ACTIIVTY");
                   startActivity(i);
            }       
        });

        tvName[i] = new TextView(this);
        params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        tvName[i].setLayoutParams(params);
        childLayout[i].addView(tvName[i]);
        if(comList[i*3].length()>24){
            String name = comList[i*3].substring(0,24)+"...";
            tvName[i].setText(name);
        }else{
            tvName[i].setText(comList[i*3]);
        }
        tvName[i].setId(listLength+1+i);
        tvName[i].setTextSize(12);
        tvName[i].setTextColor(Color.BLACK);

        tvDate[i] = new TextView(this);
        params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        tvDate[i].setLayoutParams(params);
        childLayout[i].addView(tvDate[i]);
        tvDate[i].setTextSize(11);
        tvDate[i].setTextColor(Color.BLUE);
        tvDate[i].setText(comList[i*3+1]);


        tvMsg[i] = new TextView(this);
        params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.BELOW, listLength+1+i);
        tvMsg[i].setLayoutParams(params);
        childLayout[i].addView(tvMsg[i]);
        tvMsg[i].setTextSize(11);
        tvMsg[i].setTextColor(Color.GRAY);
        if(comList[i*3+2].length()>96){
            String msg = comList[i*3+2].substring(0,96)+"...";
            tvMsg[i].setText(msg);
        }else{
            tvMsg[i].setText(comList[i*3+2]);
        }

        parentLayout.addView(childLayout[i]);

        }catch(Exception e){System.out.println("Errrorrrrr");}
    }

    setContentView(sv);
}

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.