1

I have a relatively complex setup of nested linear layouts and textviews that are all created at runtime depending on user preferences.

The problem is it takes about five seconds to launch the application because of this (an eternity in smartphone time).

enter image description here

Here is a diagram of the set up. I have about 30 of the red linear layouts in the scrollview and each one contains anywhere from 2 to 30 purple textviews wrapped into 1 to 6 orange linear layouts.

Is there anything I can do to speed up the loading of this UI or alternatively save it such that when the user exits and reopens the app it doesn't have to regenerate everything?

Thank you.

My code is below, it utilizes the populateLinks function from this post.

public void displayUnits(){
    LinearLayout ll = new LinearLayout(MainActivity.this);
    ll = (LinearLayout) findViewById(R.id.fromunitslayout);

    if (ll.getChildCount() > 3) {
        ll.removeViewsInLayout(2,ll.getChildCount()-2);
    }

    fromunitlls = new ArrayList<LinearLayout>();
    fromunitlls.clear();

    int colorcount = 0;

    for (int utcount = 0; utcount < unittypes.size(); utcount++) {
        UnitType currenttype = null;
        for (int typecount = 0; typecount < unittypes.size(); typecount++) {
            if (unittypes.get(typecount).getOrder() == utcount) {
                currenttype = unittypes.get(typecount);
                Log.v("unittype disp", String.valueOf(unittypes.indexOf(currenttype)));             
            }
        }
        if (currenttype.getHidden() && !showallunits) {
            continue;
        }
        for (int unitcount = 0; unitcount < ft.size(); unitcount++) {
            if (ft.get(unitcount).getType().getID() == currenttype.getID()) {

                // Create and set up Red LLs
                LinearLayout llhorz = new LinearLayout(MainActivity.this);
                LinearLayout llvert = new LinearLayout(MainActivity.this);
                LinearLayout lllist = new LinearLayout(MainActivity.this);
                llhorz.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
                llhorz.setOrientation(LinearLayout.HORIZONTAL);
                llvert.setLayoutParams(new LayoutParams(140,LayoutParams.MATCH_PARENT));
                llvert.setOrientation(LinearLayout.VERTICAL);
                lllist.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
                lllist.setOrientation(LinearLayout.VERTICAL);

                if (colorcount % 2 != 0) {
                    llhorz.setBackgroundResource(R.color.DarkGreyT);
                }
                colorcount++;

                // Add Blue Text View to Vertical LL            
                AutoResizeTextViewSmall txtTypeName = new AutoResizeTextViewSmall(MainActivity.this);
                txtTypeName.setTag("unittype" + unittypes.indexOf(currenttype));
                txtTypeName.setPadding(5,0,5,0);
                txtTypeName.setText(currenttype.getName().toUpperCase().replace(" ","\n"));
                txtTypeName.setLayoutParams(new LayoutParams(140,LayoutParams.WRAP_CONTENT, Gravity.CENTER));
                txtTypeName.setTextSize(12);
                txtTypeName.setTypeface(Typeface.DEFAULT_BOLD);
                txtTypeName.setGravity(Gravity.CENTER);

                txtTypeName.setOnLongClickListener(new OnLongClickListener(){
                    public boolean onLongClick(View view){
                        String typenum = view.getTag().toString();
                        typenum = typenum.substring(8);
                        Log.v("typenum",typenum);
                        Intent intent = new Intent(MainActivity.this, CatSettingsActivity.class);
                        intent.putExtra("TYPENUMBER", Integer.parseInt(typenum));
                        startActivity(intent);
                        return false; 
                    }
                });
                llvert.addView(txtTypeName);
                fromunitlls.add(lllist);
                fromunitlls.get(fromunitlls.indexOf(lllist)).setId(unittypes.indexOf(currenttype));

                llhorz.addView(llvert);
                //llhorz.addView(txtTypeName);
                llhorz.addView(fromunitlls.get(fromunitlls.indexOf(lllist)));

                ll.addView(llhorz);

                txtTypeName.reSize();
                break;
            }
        }
    }

    // Call other function to populate red LLs with orange LLs and purple TVs
    for (int utcount = 0; utcount < unittypes.size(); utcount++) {
        if (unittypes.get(utcount).getHidden()) {
            continue;
        }
        for (int unitcount = 0; unitcount < ft.size(); unitcount++) {
            if (ft.get(unitcount).getType().getID() == unittypes.get(utcount).getID()) {
                populateLinks(unittypes.get(utcount), ft);
                break;
            }
        }
    }
}
Community
  • 1
  • 1
user2137040
  • 151
  • 1
  • 4
  • When you ran Traceview to determine specifically where your problems lie, what did you learn? – CommonsWare Mar 05 '13 at 19:00
  • @user2137040 please let's see what you do BEFORE calling `displayUnits()`. Post the code before. – Lisa Anne Mar 05 '13 at 19:07
  • 2
    You should think about switching to use a ListView with a CustomAdapter to handle creating your rows. That way the Activity will not generate every row when it launches, just the ones that it needs in order to fill the current screen. And it will load more as the user scrolls. – FoamyGuy Mar 05 '13 at 19:07
  • Although as others are saying the UI probably isn't the cause of a five second delay, you should consider using a ListView instead of a ScrollView so that you can recycle and reuse these views and instantiate far fewer of them. EDIT: Beaten to it by @FoamyGuy :) – Kevin Coppock Mar 05 '13 at 19:08
  • I have the same problem. I already tried to use a ListView, but then the scrolling of this list view is very very slow. I have about 18 different views that the listview should be able to display. Maybe this is to much? Is anybody here, who has experience with something like that? – jennymo May 06 '14 at 15:29
  • I ended up adding a clicklistener to the red LLs and loading the orange LLs only when each title (blue tv) has been clicked. It did the trick. – user2137040 Dec 21 '14 at 04:26

1 Answers1

0

You can try to reduce the number of layouts by using RelativeLayout instead of nested linear layouts.

Ridcully
  • 23,362
  • 7
  • 71
  • 86