Coding is fun.
But, some of us developers are lazy, and try to find shortcuts and hacks.
After all, we all want to look smart, don’t we?
Get ready because today I’m going to share 6 hand-picked advanced tips that you can use in your next project. The best part about this article is, all of these tips are tested by me. So, if you’ve any questions, ask in the comment section. I’ll get back to you as soon as possible.
#1: Make ListView Scrolling Smooth
The main key to make a smooth scrolling in ListView is to keep the main thread free from heavy processing.
When you run/test your application, your code might call the ‘FindViewById()’ often during the scrolling of ‘ListView’ and it can slow down app’s performance.
To avoid this, use the “view holder” design pattern. View holder is an object that stores each component views inside tag field of the layout, which means you can immediately access them without having to look them up frequently.
#2: Use Include Tag For Common Layout
Android comes with a variety of different widgets. They are small visual construction blocks used to present complex interfaces to the users. But, Android application needs high level of visual components. And to create such high-level visual components, you need to use custom view. However, it can be done more easily in Android XML layout. The Include tag is used in XML layout to create pure XML and complex visual components.
By using this include tag, you can make your app layout more efficient as it includes another XML layout, meaning a better performance.
#3: Use a Background Thread Or Async Task
When it’s the matter about threading, all Android applications have at least one main thread. This thread is created when the application class is created. They are mainly aimed to draw the UI of Android application. Also, they handle the user interaction, draw pixels in the screen, and launch the activities. So, when you add code to an Activity, it runs with the available resources once the UI thread is finished. Thus, your app stays responsive to user.
#4: Create Resizable Bitmaps (9-Patch files)
Bitmap images are especially suited for Android applications.
They are the images that automatically resize to accommodate the contents of views and size of the device screen. This means, you will no longer have to take slicing of images for different resolutions, you can use just one image for all resolutions.
Benefit:APK size will be reduced and load the visual contents of Android app faster.
#5: Use Fragments To Optimize App For Various Screens
Fragments are similar to modular sections of any activity. They represent a portion of user interface in an Activity. It has its own lifecycle and it receives its own input events.
You can think of a fragment as a sub activity, but the additional benefit is, we can reuse the code through fragments in different activities, which is easily manageable by their parent activity.
Benefit: Reduced complexity which results in faster execution.
#6: Displaying Bitmaps Efficiently
As we know, by using resizable bitmaps, we can use one image for various resolutions. But sometimes, these bitmaps can consume the available memory and lead the application crash to a dreaded exception. However, to avoid this, you’ve to manage these bitmaps more efficiently.
Decode your bitmaps ensuring that it doesn’t exceed the application memory limit and remember never to place Bitmap processing on the main UI thread. Use a memory and/or disk bitmap to cache them to improve the responsiveness and for quick execution.
Remember to bookmark this page so you can directly check it in your next project. Ensuring all these things while developing an Android app can help you manage your code, reduce complexity of app, and improve the responsiveness. It means, your Android app will be more efficient than ever and your users will be more likely to use your app again.
Author Bio: Satish Solanki is a senior Android app developer at Space-O Technologies, a leading Android app development company. In his spare time, he likes to read fiction or write articles to share his knowledge about Android development.