6

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

IndieBoy
  • 1,102
  • 3
  • 14
  • 25

1 Answers1

16

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;
    }
}
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • 1
    Below link has good info about convertView/ViewHolder pattern that @WarrenFaith suggested. [http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/](http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/) – MemoryLeak Jul 16 '14 at 22:51
  • Can you update with an example? So others could benefit with it :) – sanjeev Aug 14 '18 at 14:32
  • @sanjeev done! Though I would discourage using this old code and go with a recyclerview instead – WarrenFaith Aug 14 '18 at 14:34
  • @WarrenFaith actually I am using a recyclerview and unfortunately when I invoke the class from the menu, the app just hangs in and takes time to open the activity.. I came across this when I was looking for solutions :) – sanjeev Aug 14 '18 at 14:38
  • Does setting the font from layout using `android:fontFamily` decrease performance? Note: Am using the downloadable Google font.. – sanjeev Aug 14 '18 at 14:41