0
new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            initJourneyStatusList();
            adapterJourneyStatus = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_spinner_item,listJourneyStatus) {
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    View v = super.getView(position, convertView, parent);
                    ((TextView) v).setTypeface(Typeface.createFromAsset(getAssets(), "fonts/calibri.ttf"));
                    ((TextView) v).setTextColor(getResources().getColor(android.R.color.black));
                    return v;
                }

                @Override
                public View getDropDownView(int position,  View convertView,  ViewGroup parent) {
                    View v =super.getDropDownView(position, convertView, parent);
                    ((TextView) v).setTypeface(Typeface.createFromAsset(getAssets(),"fonts/calibri.ttf"));
                    ((TextView) v).setTextColor(getResources().getColor(android.R.color.black));
                    return v;
                }
            };
            adapterJourneyStatus.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerJourneyStatus.setAdapter(adapterJourneyStatus);
            return null;
        }
    }.execute();

I have inserted this code inside onCreate function. and When I run the application, it is still slow and lagging. When I use debugger, it shows me "The application may be doing too much work on its main thread.". Is there any other way to perform tasks on background or implement multi-threading.

ndkcha
  • 175
  • 2
  • 12
  • 1
    initialize-set your adapter in OnPostExecute() instead of doInBackground() – Haresh Chhelana Apr 18 '15 at 10:52
  • @HareshChhelana is correct, accessing UI components from a background thread is not supported in android. Not an solution to the problem, though. Have you read http://stackoverflow.com/questions/14678593/the-application-may-be-doing-too-much-work-on-its-main-thread#answer-21126690? – dhke Apr 18 '15 at 10:55
  • As others have suggested, something seems wrong. You should be crashing by trying to call `setAdapter()` on a background thread. Also, do not use `getApplicationContext()` here. With regards to the time that you are spending, use Traceview to determine exactly what is going wrong. – CommonsWare Apr 18 '15 at 10:58

3 Answers3

1

doInBackground() is not run in UI thread so you can not do any UI thread stuff in it, So try to move adapter initialization and set code in OnPostExecute().

private void setJourneyStatusList(final Context context){
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                initJourneyStatusList();
                return null;
            }

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                adapterJourneyStatus = new ArrayAdapter(context, android.R.layout.simple_spinner_item,listJourneyStatus) {
                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        View v = super.getView(position, convertView, parent);
                        ((TextView) v).setTypeface(Typeface.createFromAsset(getAssets(), "fonts/calibri.ttf"));
                        ((TextView) v).setTextColor(getResources().getColor(android.R.color.black));
                        return v;
                    }

                    @Override
                    public View getDropDownView(int position,  View convertView,  ViewGroup parent) {
                        View v =super.getDropDownView(position, convertView, parent);
                        ((TextView) v).setTypeface(Typeface.createFromAsset(getAssets(),"fonts/calibri.ttf"));
                        ((TextView) v).setTextColor(getResources().getColor(android.R.color.black));
                        return v;
                    }
                };
                adapterJourneyStatus.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinnerJourneyStatus.setAdapter(adapterJourneyStatus);
            }
        }.execute();
    }
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
1

Apart from the good comments others already said, you are creating (on the UI thread) the typefaces many time (at least once for every list row), and this is a slow operation. Create the typefaces only once and save them as local member variables

yoah
  • 7,180
  • 2
  • 30
  • 30
0

Try using singleton classes in order to reduce number of repeated slow operations. For example, Loading fonts is a slow operations. Try singleton class like,

public class ClassName {
    private static ClassName instance;
    public TypeFace fontToLoad;

    public static ClassName getInstance() {
         if (instance == null)
               instance = new ClassName();
         return instance;
    }

    public void loadFonts(Activity activity) {
         fontToLoad = Typeface.createFromAsset(activity.getAssets(), "fonts/calibri.ttf");
    }

    public TypeFace font() {
         return fontToLoad;
    }
}

Now, use this singleton class at the starting of the application (i.e. launcher activity) to load fonts as,

private ClassName className = ClassName.getInstance();

And in onCreate function,

className.loadFonts(this);

And now where you want to use fonts, use it like,

private ClassName className = ClassName.getInstance();

and for editText or any view,

editText.setTypeFace(className.font());

This will load fonts only once throughout application and can be used several times. This may help you with many other functions other than just loading fonts.

ndkcha
  • 175
  • 2
  • 12