user3477926
user3477926

Reputation: 1

Android ListView Iterate to set font/text

Need some help with ListView to set font. I set font and now have an issue to set String to ListView AGAING. How should I use loop to do that?

ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu_item, items){ 
            @Override 
            public View getView(int position, View convertView, ViewGroup parent) { 
                String[] items = {
                        getResources().getString(R.string.menu_item_play),
                        getResources().getString(R.string.menu_item_settings),
                        getResources().getString(R.string.menu_item_help),
                        getResources().getString(R.string.menu_item_exit)
                };
                String fontPath = "fonts/28.ttf";
                typeface = Typeface.createFromAsset(getAssets(), fontPath); 
            LayoutInflater inflater = getLayoutInflater(); 
                View view = inflater.inflate(R.layout.menu_item, null, false); 
 TextView textView = (TextView) view.findViewById(R.id.text); 
                    textView.setText(items[0]); // right here must be a loop or smt
                    textView = (TextView) view.findViewById(R.id.text); 
                    textView.setText(items[1]);
   textView.setTypeface(typeface);
return view; 
            } 
        };  

Upvotes: 0

Views: 115

Answers (1)

Cruceo
Cruceo

Reputation: 6824

getView() technically is iterating over your ListView's items, so that should work. But a better approach would be to subclass TextView and have it automatically set the TypeFace for you:

public class CustomTextView extends TextView{

public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public CustomTextView(Context context) {
    super(context);
    init();
}

public void init(boolean bold) {
    setTypeface(Typeface.createFromAsset(getAssets(), "fonts/28.ttf"));
}

And then an even better approach would be to use a static reference to the typeface so you don't have to create it every time your View loads, but that's a bit more than this simple example.

Upvotes: 1

Related Questions