Slow listview when custom font is installed

I have some data in an XML file that puts it in /res/values/mydata.xml. I want to show the data in a list with a special font. Everything is fine in the emulator, but in the real device (using samsung galaxy tab 10.1 2 with android 4.0.3) it is too slow when viewing the scroll list. In fact, it works fine with the default font, but the problem arises when setting up a 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? is it about my device? Any solution can help me. Thanks

+4
source share
1 answer

Your problem is the line:

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

You have to do this once in the constructor of your adapter, insert the font member variable and just use the variable to call setTypeface(font) on the TextView .

Avoid heavy loading in the getView() method.

Also read about the convertView / ViewHolder template for the adapter, which will also give you better performance.

+14
source

Source: https://habr.com/ru/post/1442188/


All Articles