0

So, I want to create a custom Android TextView with a border INSIDE AN XML FILE, so not programmatically, and create 10 of those using a for-loop. Something like this:

LinearLayout layout = new LinearLayout(//something, //something); 
TextView tv;
String[] data = //Certain data which I'm getting

for(int i = 0; i < data.length; i++) { 
tv = (TextView) findViewById(R.id.tvTest); 
layout.addView(tv); 
} 

But this doesn't work for me for some reason. So to summarize:

  • > Create custom TextView in XML (NOT PROGRAMMATICALLY with Java, but in XML)
  • > Create an x amount of this particular TextView
  • > Add it to the screen in Java

Can you help me with this?

1
  • You need to use Layout Inflator for this.
    – Aditya
    Apr 2, 2012 at 9:36

2 Answers 2

1

This is code snippet

 LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    for (  ) {
      View listItemView = inflater.inflate(R.layout.detaillistitem, _ListLayout, false);
      _ListLayout.addView(listItemView);

      TextView descriptionText = (TextView) listItemView.findViewById(R.id.Text1);
      descriptionText.setText("");

    }

R.layout.detaillistitem is item u want to inflate. _ListLayout is LinearLayout to which you can add above item.

0

Try like this..

Create your textview in an external layout file..

for(int i = 0; i < data.length; i++) { 
View v = LayoutInflater.from(this).inflate(R.layout.textview, null);//R.layout.textview is your textview id you want to repeat..
layout.addView(v);
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.