SleepNot
SleepNot

Reputation: 3038

Change ListView font

Is there any way that I can speed up the changing of fonts in my app?

I have a ListView that loads its list from an XML online. It's all good, but I need to change its font to a custom font from my assets folder.

When i applied:

 TxEventName = (TextView)row.findViewById(id.eventNameTx);
 TxEventName.setText(getEventsCount(position));
 TxEventName.setTypeface(Utilities.textFont(this.getContext()));

it becomes slow, since I applied the setTypeface.

Sometimes it would load the UI layout first then the list, sometimes it would just be a delaying black screen then, when the next activity comes out, the list is already there.

I am even using AsyncTask to load the values for the list which is something like:

private class initList extends AsyncTask<Void, Void, Void>{

    @Override
    protected Void doInBackground(Void... params) {

        SimpleDateFormat formatterc; 
        Date xdatec=null; 
        formatterc = new SimpleDateFormat("dd/MM/yy");
        try {
            xdatec = (Date)formatterc.parse(date);
            SimpleDateFormat frmtr = new SimpleDateFormat(dateTemplateForChecking);
            selectedDate = frmtr.format(xdatec).toString();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        for(int i = 0; i < XMLParser.ppEvents.size(); i++) {
            EventsObj evObj = XMLParser.ppEvents.get(i);

            for(int j = 0; j < evObj.date.size(); j++) {
                if(selectedDate.equalsIgnoreCase(evObj.date.get(j))) {

                    StringBuilder sbldr = new StringBuilder();
                    sbldr.append(evObj.act_id);
                    sbldr.append("!");
                    if (SelectLanguage.lang == "ENG"){
                    sbldr.append(evObj.name_en);
                    } else if (SelectLanguage.lang == "TCH"){
                        sbldr.append(evObj.name_tc);
                    } else if (SelectLanguage.lang == "SCH"){
                        sbldr.append(evObj.name_sc);
                    }

                    mainEvents.add(sbldr.toString());
                    eventID.add(Integer.toString(evObj.act_id));
                }   
            }
        }
        adapter = new events_adapter(PublicProg.this, layout.events_adapter, id.eventNameTx, mainEvents);
        return null;
    }

    protected void onPostExecute(Void result){
        eventList.setAdapter(adapter);
    }

    protected void onPreExecute(){
        Bundle ext = getIntent().getExtras();

        position = ext.getString("position");
        date = ext.getString("date");

        mainEvents = new ArrayList<String>();
        eventID = new ArrayList<String>();
    }
}

Upvotes: 0

Views: 663

Answers (3)

Trung Nguyen
Trung Nguyen

Reputation: 7532

I use this to create simple cache for Typeface

public class Typefaces{

private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();

    public static Typeface get(Context c, String name){
        synchronized(cache){
            if(!cache.containsKey(name)){
                Typeface t = Typeface.createFromAsset(
                        c.getAssets(), 
                        String.format("fonts/%s.OTF", name)
                    );
                cache.put(name, t);
            }
            return cache.get(name);
        }
    }

}

In adapter constructor init your custom Typeface

myriadBold = Typefaces.get(mContext, "MYRIADPRO-BOLD");

and use it in getView

txt.setTypeface(myriadBold);

This way decrease the number system call skia to generate custome font.

Upvotes: 1

Mohsin Naeem
Mohsin Naeem

Reputation: 12642

you can set Typeface through your custom TextView

public class TextViewGeorgiaBold extends TextView {

Resources res = getResources();
String font_path = res.getString(R.string.MAIN_FONT);

public TextViewGeorgiaBold(Context context, AttributeSet attrs, int defStyle) {
    super(context.getApplicationContext(), attrs, defStyle);
    Typeface font = Typeface.createFromAsset(getContext()
            .getApplicationContext().getAssets(), font_path);
    setTypeface(font);
}

public TextViewGeorgiaBold(Context context, AttributeSet attrs) {
    super(context.getApplicationContext(), attrs);
    Typeface font = Typeface.createFromAsset(getContext()
            .getApplicationContext().getAssets(), font_path);
    setTypeface(font);

}

public TextViewGeorgiaBold(Context context) {
    super(context.getApplicationContext());
    Typeface font = Typeface.createFromAsset(getContext()
            .getApplicationContext().getAssets(), font_path);
    setTypeface(font);
}

}

as It sets the TypeFace at the time of inflation of layout.

may be it will help you.

Upvotes: 0

Marcio Covre
Marcio Covre

Reputation: 4556

Maybe where you set the Typeface is not good place.

Also Utilities.textFont(this.getContext()), with this you are probably reading the font from the filesystem, which can slow your process and depending on where this line is called, you are locking the UI.

Upvotes: 0

Related Questions