Reputation: 2417
My layout is taking too much time to load approximately 2-3 seconds on actual device.My layout have lots of view ranging from 40-60 views. I am checking for various condtion as well in OnCreate()
.So can anyone suggest me how can i load my activity faster. And most of the views have background image.In actuality my entire layout is full of ImageView
.
Below is the code-snippet of the onCreate()
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fueladd_layout);
face2=Typeface.createFromAsset(getAssets(),"digital.ttf");
typewriter=Typeface.createFromAsset(getAssets(),"veteran_typewriter.ttf");
Display display=getWindowManager().getDefaultDisplay();
width=display.getWidth();
height=display.getHeight();
dbc=new Databaseconnection(this);
sdb=dbc.getWritableDatabase();
sdb=dbc.getReadableDatabase();
c = Calendar.getInstance();
fyear = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
date();
mp1=MediaPlayer.create(getBaseContext(), R.raw.button_click);
newcarmenu=(RelativeLayout) findViewById(R.id.newcarmenu);
topmenu=(RelativeLayout) findViewById(R.id.topmenu);
newnotelayout=(RelativeLayout) findViewById(R.id.newnotelayout);
layout_mileage=(RelativeLayout) findViewById(R.id.layout_mileage);
img_tabdashboard=(ImageView) findViewById(R.id.img_tabdashboard);
img_tabdashboard.setOnClickListener(this);
img_tabexpenses=(ImageView) findViewById(R.id.img_tabexpenses);
img_tabexpenses.setImageDrawable(img_tabexpenses.getResources().getDrawable(R.drawable.tabexpensespressed));
img_tabexpenses.setOnClickListener(this);
img_tabfinancial=(ImageView) findViewById(R.id.img_tabfinancial);
img_tabfinancial.setOnClickListener(this);
img_tabtrip=(ImageView) findViewById(R.id.img_tabtrip);
img_tabtrip.setOnClickListener(this);
img_menu=(ImageView) findViewById(R.id.img_menu);
img_menu.getLayoutParams().width=(int) (width*0.109375);
img_menu.setOnClickListener(this);
img_preferences=(ImageView) findViewById(R.id.img_preferences);
img_preferences.getLayoutParams().width=(int) (width*0.109375);
img_preferences.setOnClickListener(this);
img_mycar=(ImageView)findViewById(R.id.img_mycar);
img_mycar.setOnClickListener(this);
imgnotesdone=(ImageView)findViewById(R.id.imgnotesdone);
imgnotesdone.setOnClickListener(this);
img_aboutus=(ImageView)findViewById(R.id.img_aboutus);
img_aboutus.setOnClickListener(this);
img_editcar=(ImageView)findViewById(R.id.img_editcar);
img_editcar.setOnClickListener(this);
img_exit=(ImageView)findViewById(R.id.img_exit);
img_exit.setOnClickListener(this);
img_fuel=(ImageView) findViewById(R.id.img_fuel);
img_fuel.setBackgroundResource(R.drawable.fuel2);
img_fuel.setOnClickListener(this);
img_toll=(ImageView) findViewById(R.id.img_toll);
img_toll.setOnClickListener(this);
img_parking=(ImageView) findViewById(R.id.img_parking);
img_parking.setOnClickListener(this);
img_puc=(ImageView) findViewById(R.id.img_puc);
img_puc.setOnClickListener(this);
img_oil=(ImageView) findViewById(R.id.img_oil);
img_oil.setOnClickListener(this);
img_service=(ImageView) findViewById(R.id.img_service);
img_service.setOnClickListener(this);
img_add=(ImageView) findViewById(R.id.img_save);
img_add.setOnClickListener(this);
img_view=(ImageView) findViewById(R.id.img_view);
img_view.setOnClickListener(this);
img_addnewcar=(ImageView) findViewById(R.id.img_addnewcar);
img_addnewcar.setOnClickListener(this);
img_addnewbike=(ImageView) findViewById(R.id.img_addnewbike);
img_addnewbike.setOnClickListener(this);
img_usedcar=(ImageView) findViewById(R.id.img_usedcar);
img_usedcar.setOnClickListener(this);
img_usedcar=(ImageView) findViewById(R.id.img_usedbike);
img_usedcar.setOnClickListener(this);
img_menuexit=(ImageView) findViewById(R.id.img_menuexit);
img_menuexit.setOnClickListener(this);
img_loc_picker = (ImageView) this.findViewById(R.id.img_loc_picker);
img_loc_picker.setOnClickListener(this);
txt_title=(TextView) findViewById(R.id.txttitle);
txt_title.setTypeface(face2,Typeface.BOLD);
txt_title.setText("FUEL");
layout_list=(RelativeLayout) findViewById(R.id.layout_list);
animationBottomIn = AnimationUtils.loadAnimation(layout_list.getContext(), R.anim.slide_in_up);
animationBottomOut = AnimationUtils.loadAnimation(layout_list.getContext(), R.anim.slide_out_down);
animationRightIn = AnimationUtils.loadAnimation(layout_list.getContext(), R.anim.slide_in_right);
animationRightOut = AnimationUtils.loadAnimation(layout_list.getContext(), R.anim.slide_out_right);
list_type= (ListView) findViewById(R.id.list_type);
list_type.setOnItemClickListener(this);
list_location= (ListView) findViewById(R.id.list_location);
list_location.setOnItemClickListener(this);
txt_type_loc=(TextView) findViewById(R.id.txt_type_loc);
txt_type_loc.setTypeface(typewriter);
img_arrow_down = (ImageView) this.findViewById(R.id.img_arrow_down);
img_arrow_down.setOnClickListener(this);
txtselectdate=(TextView) findViewById(R.id.txt_currentdate);
txtselectdate.setTypeface(typewriter);
txtnewnotes=(TextView) findViewById(R.id.txtnewnotes);
txtnewnotes.setTypeface(typewriter);
txtcdate=(TextView) findViewById(R.id.txtcdate);
txtcdate.setTypeface(typewriter);
txtcdate.setOnClickListener(this);
txtcurrentkm=(TextView) findViewById(R.id.txt_currentkm);
txtcurrentkm.setTypeface(typewriter);
txtpreviouskm=(TextView) findViewById(R.id.txt_previouskm);
txtpreviouskm.setTypeface(typewriter);
txtfuel=(TextView) findViewById(R.id.txt_quantity);
txtfuel.setTypeface(typewriter);
txtrate=(TextView) findViewById(R.id.txt_rate);
txtrate.setTypeface(typewriter);
txtamount=(TextView) findViewById(R.id.txt_amount);
txtamount.setTypeface(typewriter);
txtmileage=(TextView) findViewById(R.id.txt_mileage);
txtmileage.setTypeface(typewriter);
txtlocation=(TextView) findViewById(R.id.txt_location);
txtlocation.setTypeface(typewriter);
txtbrand=(TextView) findViewById(R.id.txt_brand);
txtbrand.setTypeface(typewriter);
txtnote=(TextView) findViewById(R.id.txt_note);
txtnote.setTypeface(typewriter);
editbrand=(EditText) findViewById(R.id.edit_brand);
editbrand.setTypeface(typewriter);
editlocation=(EditText) findViewById(R.id.edit_location);
editlocation.setTypeface(typewriter);
editnewnotes=(EditText) findViewById(R.id.editnewnotes);
editnewnotes.setTypeface(typewriter);
img_date_picker = (ImageView) this.findViewById(R.id.img_date_picker);
img_date_picker.setOnClickListener(this);
img_loc_picker = (ImageView) this.findViewById(R.id.img_loc_picker);
img_loc_picker.setOnClickListener(this);
img_brand_picker = (ImageView) this.findViewById(R.id.img_brand_picker);
img_brand_picker.setOnClickListener(this);
imgnotepicker = (ImageView) this.findViewById(R.id.imgnotepicker );
imgnotepicker .setOnClickListener(this);
//this will set the text of edit view
//the 1st record value is retrived and displayed
//if no record is present than editview is empty
loc_select_Query = "select item From FuelLocation";
Cursor cursor_loc = sdb.rawQuery(loc_select_Query, null);
int count=cursor_loc.getCount();
cursor_loc.moveToFirst();
if(count>1)
{
editlocation.setText(cursor_loc.getString(0));
}
//this will set the text of edit view of brand
//the 1st record value is retrived and displayed
//if no record is present than editview is empty
brand_select_Query = "select brand From FuelBrand";
Log.i("select_Query", brand_select_Query);
Cursor cursor_brand = sdb.rawQuery(brand_select_Query, null);
int count1=cursor_brand.getCount();
cursor_brand.moveToFirst();
if(count1>1)
{
editbrand.setText(cursor_brand.getString(0));
}
editcurrentkm=(EditText) findViewById(R.id.edit_currentkm);
editcurrentkm.setTypeface(typewriter);
editpreviouskm=(EditText) findViewById(R.id.edit_previouskm);
editpreviouskm.setTypeface(typewriter);
editpreviouskm.setOnFocusChangeListener(new OnFocusChangeListener()
{
public void onFocusChange(View v, boolean hasFocus)
{
if(!hasFocus)
{
String select="Select Fuel_CurrentKm from Fuel ";
Cursor selectcur = sdb.rawQuery(select, null);
selectcur.moveToLast();
Log.i("Record Count",Integer.toString(selectcur.getCount()));
if(selectcur.getCount()>0)
{
Log.i("currentkm",editcurrentkm.getText().toString());
Log.i("currentkm",editcurrentkm.getText().toString());
if(Integer.valueOf(editpreviouskm.getText().toString())<Integer.valueOf(selectcur.getString(0).toString()))
{
msg="Enter previous odometer greater than"+selectcur.getString(0).toString();
showToastMessage(msg);
}
}
}
}
});
editcurrentkm.setOnFocusChangeListener(new OnFocusChangeListener()
{
public void onFocusChange(View v, boolean hasFocus)
{
if(!hasFocus)
{
ckm=editcurrentkm.getText().toString();
if(ckm.equalsIgnoreCase(""))
{
msg="Enter Current Odometer reading";
showToastMessage(msg);
}
else
{
if(Integer.valueOf(editcurrentkm.getText().toString())>Integer.valueOf(editpreviouskm.getText().toString()))
{
if(pcount>1)
{
currentkm=Integer.valueOf(ckm);
calmileage=(currentkm-previouskm)/pquantity;
editmileage.setText(Float.toString(calmileage));
Log.i("Mileage",Float.toString(calmileage));
}
}
else
{
msg="Enter Current Odometer reading Greater than Previous Odometer reading";
showToastMessage(msg);
}
}
}
}
});
editfuel=(EditText) findViewById(R.id.edit_quantity);
editfuel.setTypeface(typewriter);
editrate=(EditText) findViewById(R.id.edit_rate);
editrate.setTypeface(typewriter);
editrate.setOnFocusChangeListener(new OnFocusChangeListener()
{
public void onFocusChange(View arg0, boolean arg1)
{
if(!arg1)
{
if(!editfuel.getText().toString().equalsIgnoreCase(""))
{
fquantity=editfuel.getText().toString();
quantity=Float.valueOf(fquantity);
if(!editrate.getText().toString().equalsIgnoreCase(""))
{
frate=editrate.getText().toString();
total=quantity*Float.valueOf(frate);
editamount.setText(Float.toString(total));
}
}
}
}
});
editamount=(EditText) findViewById(R.id.edit_amount);
editamount.setTypeface(typewriter);
editamount.setOnFocusChangeListener(new OnFocusChangeListener()
{
public void onFocusChange(View arg0, boolean arg1)
{
if(!arg1)
{
if(!editfuel.getText().toString().equalsIgnoreCase(""))
{
fquantity=editfuel.getText().toString();
quantity=Float.valueOf(fquantity);
if(!editamount.getText().toString().equalsIgnoreCase(""))
{
famount=editamount.getText().toString();
rate=Float.valueOf(famount)/quantity;
editrate.setText(Float.toString(rate));
}
}
}
}
});
editmileage=(EditText) findViewById(R.id.edit_mileage);
editmileage.setTypeface(typewriter);
editnote=(EditText) findViewById(R.id.edit_note);
editnote.setTypeface(typewriter);
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
readsound=myPrefs.getBoolean(SOUND,sound);
car_id= myPrefs.getInt(MY_CAR,count);
Log.i("car id",Integer.toString(car_id));
if(car_id==0)
{
car_id=1;
Log.i("Car_ID",Long.toString(car_id));
}
Intent editintent=getIntent();
String action=editintent.getAction();
Log.i("Action",action);
if(action.equalsIgnoreCase("com.curioussolutions.autoistdiary.fuel.EDIT"))
{
Log.i("inside","edit");
record=(Integer) getIntent().getSerializableExtra("Id");
Log.i("Record",Integer.toString(record));
Cursor editcursor = sdb.rawQuery("select Fuel_Date,Fuel_CurrentKm,Fuel_PreviousKm,Fuel_Quantity,Fuel_Rate,Fuel_Amount,Fuel_Milege,Fuel_Brand,Fuel_Location,Fuel_Note from Fuel where Fuel_Id="+record, null);
count=editcursor.getCount();
editcursor.moveToFirst();
udate=editcursor.getString(0).toString();
Log.i("date",udate);
txtcdate.setText(udate);
Log.i("date",udate);
currentkm=Integer.valueOf(editcursor.getString(1).toString());
editcurrentkm.setText(Integer.toString(currentkm));
Log.i("currentkm",Integer.toString(currentkm));
pkm=Integer.valueOf(editcursor.getString(2).toString());
Log.i("previouskm",Integer.toString(pkm));
editpreviouskm.setText(Integer.toString(pkm));
Log.i("previouskm",Integer.toString(pkm));
quantity=Float.valueOf(editcursor.getString(3).toString());
editfuel.setText(Float.toString(quantity));
Log.i("quantity",Float.toString(quantity));
rate=Float.valueOf(editcursor.getString(4).toString());
editrate.setText(Float.toString(rate));
Log.i("rate",Float.toString(rate));
total=Float.valueOf(editcursor.getString(5).toString());
editamount.setText(Float.toString(total));
Log.i("cost",Float.toString(total));
if(record>1)
{
calmileage=Float.valueOf(editcursor.getString(6).toString());
editmileage.setText(Float.toString(calmileage));
Log.i("mileage",Float.toString(calmileage));
}
if(record==1)
{
layout_mileage.setVisibility(RelativeLayout.GONE);
}
location=editcursor.getString(7).toString();
editlocation.setText(location);
Log.i("location",location);
fbrand=editcursor.getString(8).toString();
editbrand.setText(fbrand);
Log.i("date",fbrand);
notes=editcursor.getString(9).toString();
editnote.setText(notes);
Log.i("notes",notes);
}
if(action.equalsIgnoreCase("com.curioussolutions.autoistdiary.fuel.ADD"))
{
Log.i("inside","add");
String select1="select Fuel_CurrentKm,Fuel_Quantity,Fuel_Rate from Fuel";
Cursor cur = sdb.rawQuery(select1, null);
pcount=cur.getCount()+1;
cur.moveToLast();
if(pcount>1)
{
previouskm=Integer.valueOf(cur.getString(0).toString());
pquantity=Float.valueOf(cur.getString(1).toString());
rate=Float.valueOf(cur.getString(2).toString());
editpreviouskm.setText(Integer.toString(previouskm));
editrate.setText(Float.toString(rate));
}
else
{
layout_mileage.setVisibility(RelativeLayout.GONE);
editpreviouskm.setText("0");
}
date();
txtcdate.setText(date);
}
else
{
layout_mileage.setVisibility(RelativeLayout.GONE);
editpreviouskm.setText("0");
date();
txtcdate.setText(date);
}
}
Upvotes: 3
Views: 4948
Reputation: 3221
Let the onCreateComplete as quickly as possible. do not hold it for loading your views. Start AsyncTasks to load the views in findViewById. then in asyntasks onPostsExecute you may set values to your views e.g setting images to your imageviews etc.
worth a try.
Upvotes: 0
Reputation: 116
Try to use threads or if it is not possible watch if in your method, OnCreate, is there a call to something that consume resources before the layout to be complete rendered.
Upvotes: 0
Reputation: 17037
Here is a really good explanation how can you optimize your UI so it can load faster : Android Performance in Practice by Romain Guy. I suggest you to take a look.
Upvotes: 0