2

I have a XML component composed by an ImageView and a TextView. I'm including this component a couple times in other XML codes. I'm wondering if theres some way to have different texts every time I include the mentioned component only using XML, without having to do this programmatically. Any clues?

Thanks!

Filipe
  • 3,398
  • 5
  • 21
  • 35

2 Answers2

0

yes, it is possible. You would need to call TextView.setText().

leave a comment with more details if this isn't the answer your looking for.

Updated:

The best way to do this is through code. its by far the easiest

yoshi24
  • 3,147
  • 8
  • 45
  • 62
  • Well, I would have to call "TextView.setText()" from my java code, I guess. I don't wan't that. I would like to reuse the component and change text from the TextView with some XML code. – Filipe Aug 26 '11 at 14:10
0

I'm sorry, but XML, much like HTML, is static. Making it dynamic involves code.

So, if you really, really, really want to do this, you'd have to extend a TextView object with code. It would set it's own text dynamically by pulling from an array or database or whatever. After doing this, you can place your special TextView objects in any XML file that you want.

Example of extending TextView.

However, after going through that much effort. It may just be easier to make a for loop that finds all of your TextViews and calls setText().

Community
  • 1
  • 1
peter
  • 6,067
  • 2
  • 33
  • 44