-1
public void setFont(String font_type, TextView[] fontArray)
{
    Typeface face = Typeface.createFromAsset(getAssets(), "fonts/" + font_type);

    for (int i = 0; i < fontArray.length; i++){
        fontArray.setTypeface(face);
    }
}

I want to add different textviews to different typeface. i could not find out how to implement foreach loop correctly.

SeinopSys
  • 8,787
  • 10
  • 62
  • 110
coreprojectz
  • 134
  • 1
  • 3
  • 12
  • Not clear!! can you elaborate more? – Aman J Mar 06 '13 at 14:36
  • I'm not sure I get the question. You need to access each element of the font array--maybe brushing up on some Java basics would be a good idea. Also, `for (TextView textView : textViews)` would make more sense than calling it `fontArray`, since it's not an array of fonts. – Dave Newton Mar 06 '13 at 14:37

2 Answers2

4

You are missing the index in the loop:

fontArray[i].setTypeface(face);
         ^^^
      add this

...or use a "for each" (as you wanted):

for (TextView tv : fontArray)
    tv.setTypeface(face);
dacwe
  • 43,066
  • 12
  • 116
  • 140
1

Like any other Java array, you can iterate over its elements using a for-loop

for(TextView tv: fontArray) {
    tv.setTypeface(face);
}

See Java: Array with loop for another example.

Note, the name fontArray is a bit misleading... You could rename it to textViews or something more appropriate...

Community
  • 1
  • 1
Veger
  • 37,240
  • 11
  • 105
  • 116