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).
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;
}
}
}
}