Search code examples
javaandroidandroid-layouttruetypeandroid-fonts

slow listview when set custom font


I have some data in an xml file put it in /res/values/mydata.xml. I want to show data in a listview with a custom font. Everything is great in emulator but in real device (using samsung galaxy tab 10.1 2 with android 4.0.3) is too slow when scroll listview. Actually it works great with default font but the problem appears when set the custom font.

This is my java code:

public class ShowFoodCalorie extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // reading data from xml file
    setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1,
            R.id.textView1,  getResources().getStringArray(R.array.food_cal)));
}
private class MyAdapter extends ArrayAdapter<String> {
    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] string) {
        super(context, resource, textViewResourceId, string);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.show_all, parent, false);
        String[] item = getResources().getStringArray(R.array.food_cal);
        TextView tv = (TextView) row.findViewById(R.id.textView1);
        try {
            Typeface font = Typeface.createFromAsset(getAssets(),"myFont.ttf");
            tv.setTypeface(font);
        } catch (Exception e) {
            Log.d("Alireza", e.getMessage().toString());
        }

        tv.setText(item[position]);
        return row;
    }
}

what is this problem? it's about my device? Any solution can help me. Thanks


Solution

  • Your problem is that line:

    Typeface font = Typeface.createFromAsset(getAssets(),"myFont.ttf");
    

    You should do that once in the constructor of your Adapter, make font a member variable and than just use the variable to call setTypeface(font) on your TextView.

    Heavy loading in the getView() method should be prevented.

    Also read about the convertView / ViewHolder pattern for adapter, that would give you a performance boost, too.

    Update with example:

    private class MyAdapter extends ArrayAdapter<String> {
        Typeface font;
    
        public MyAdapter(Context context, int resource, int textViewResourceId,
                String[] string) {
            super(context, resource, textViewResourceId, string);
            font = Typeface.createFromAsset(context.getAssets(),"myFont.ttf");
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater)
    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.show_all, parent, false);
            String[] item = getResources().getStringArray(R.array.food_cal);
            TextView tv = (TextView) row.findViewById(R.id.textView1);
            try {
                tv.setTypeface(font);
            } catch (Exception e) {
                Log.d("Alireza", e.getMessage().toString());
            }
    
            tv.setText(item[position]);
            return row;
        }
    }