In order to provide a custom typeface in my ListActivity
, I wrote a class CustomAdapter
extending BaseAdapter
according to this example here.
However, as described there, I wrote the getView()
method like following:
public View getView(int position, View convertView, ViewGroup parent){
String gameName = gameNames[position]; // gameName ist the String[] of the Custom Adapter
TextView tv = new TextView(context);
tv.setText(gameName);
tv.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/gulim.ttf"));
return tv;
}
This works as intended. The only disturbing thing is that it takes about three or four seconds until the list shows up (and this is a very long time in this context). However, in the ListActivity
I set the onItemClickListener
s like this:
private void setOnItemClickListener(){
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int pos, long id){
onClickEntryButton(((TextView) view).getText().toString());
}
});
}
private void onClickEntryButton(String gameName){
Intent intent = new Intent(this, GameActivity.class);
intent.putExtra("gameName", gameName);
startActivity(intent);
finish();
}
Now when clicking on a ListItem
, it takes even more time until the GameActivity
opens. This Activity
is just a couple of TextView
s filled with information taken from a SQLite database. Also here, I set a custom typeface to every TextView
. It happens even that the screen gets black for 2-3 seconds (appearing the app crashed), then the new Activity
shows up. This doesn't happen accessing that Activity
from other places in the application.
In both cases - accessing the ListActivity
and accessing the GameActivity
from the ListActivity
- a couple of
"szipinf - Initializing inflate state"
messages appear in the LogCat. What does they mean in this context? Would it be a better usage to set the onItemClickListener
s into the getView()
method of my CustomAdapter
? Something seems to really inhibit the transitions, but I can't figure out what, since there is nothing big to be calculated or processed (in fact, in the SQLite database are exactly two entries with each 5 fields)?
EDIT If required or desired, of course I can provide more code.
I had exactly same issue and your question gave me answer!
I still do not know exact root cause but this the isuue is because of reading custom font from assets multiple times in my case. If I have 10 widgets in my screen and each of them is using custom font Android loads it from assets everytime. which was not just causing my activity transions to become slow it was also resulting in crash after playing with it multiple tmes.
So I created a cache for my typeface to avoid fetching typeface everytime from assets.
I added this code inside my utility class:
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
public static final String ASSET_PATH="assetPath";
public static Typeface getFont(Context c, String assetPath) {
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
Typeface t =(Typeface.createFromAsset(c.getAssets(),
"fonts/arial.ttf"));
cache.put(assetPath, t);
} catch (Exception e) {
return null;
}
}
return cache.get(assetPath);
}
}
I created my custom class to to setTypeface
public class MyButton extends Button {
public MyButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyButton(Context context) {
super(context);
}
@Override
public void setTypeface(Typeface tf) {
super.setTypeface(Util.getFont(getContext(), Util.ASSET_PATH));
}
}
variable assetPath can be used to provide diffrent fonts at runtime
EDIT: Here is custom typefaceManager I have created as a library to make it more generic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With