Android Best Practices is quite crucial for app developers to follow. App development is quite an intimidating task. App crashes and Bugs can give you nightmares and make your app extremely slow and buggy.
Here are some top Android Best Practices you should follow from experienced developers for your app.
Android Best Practices for Developers
Use Android Studio
If you are using some other IDE for your android app development like Visual Studio, better switch today because you are simply wasting your time.
To know how to run Flutter in Android Studio, click here.
With Android Studio you can faster Build, great logcat to track bugs, app layout preview, etc.
Keep your IDE updated
An Integrated Development Environment (IDE) is a software application that provides comprehensive facilities to computer programmers for App development.
Android Studio or Eclipse whatever IDE you use for Android development, make sure to keep it updated to the (stable) latest version.
Develop for all
Being a versatile OS, Android is available for many different screen sizes, so if you’re targeting mobile or tablet, make sure you develop it for all gadget screen sizes that fall under mobile or tablet category.
Optimize for Speed
Unlike desktops, many mobile gadgets don’t come with good processing speeds also RAM is limited. Therefore optimize your app for better speed.
Limit permissions required by your app, information like user location or Internet access if not required then shouldn’t be asked for as it will make your app bulky and may take more battery which usually frustrates a user and compels them to uninstall the app.
Nowadays, even Google penalize apps that collect too much of user information.
Images for Big Screens (
Many devices like Pixel 3XL, use images from xxxhdpi folder for better user interface. Make sure you have images in drawable folder for different device screen sizes as well.
Launcher icons in mipmap folders
Always place your launcher icons in the mipmap folders. When building apks for different screen sizes, some drawable folders get stripped from main package.
However because mipmap folders don’t get stripped, placing icons in mipmap folder won’t make your icons blurry.
Use Selectors and Shapes instead of Images
You can create simple layouts like a round circle or a rounded rectangle using shapes tag in an xml file. These shapes shapes are always sharp and do not need to be created for multiple device screen sizes.
Simply, selectors can be used for creating slector state for widgets.
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/numpad_button_bg_selected" android:state_selected="true"></item> <item android:drawable="@drawable/numpad_button_bg_pressed" android:state_pressed="true"></item> <item android:drawable="@drawable/numpad_button_bg_normal"></item> </selector>
Accessing text string resources from strings.xml can help you add more languages versions to your app and help boost your app downloads.
Create reusable Layouts
Is there a button or a layout that you will be using again and again in your app? Then its important for you to reuse layouts and not waste time in creating them again and again in different android layout folder.
The <include /> tag makes it possible to have a single layout, re-used across multiple activities and fragments.
See the code below for better understanding:
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/button_color" android:textAllCaps="false" android:text="Reusable Button" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="We have reused a button, see below:" /> <include layout="@layout/reuse_button.xml" /> </LinearLayout>
Creating Nested Layouts
Creating deeply nested layouts can make your app User interface extremely slow and make animating them impossible task for devices.
Prefer using Relative layouts to Linear Layouts wherever possible.
Using AsyncTask vs AsyncTaskLoader
When compare AsyncTaskLoader vs. AsyncTask, as you may know when you rotate your device screen, it may destroy and re-create your activity, to make it clear let image rotate your device while networking transaction is going on:
AsyncTask will be re-executed as background thread again, and previous background thread processing was just be redundant and zombie.
AsyncTaskLoader will be just re-used basing on Loader ID that registered in Loader Manager before, so avoid re-executing network transaction.
In summary, AsyncTaskLoader prevent duplication of background threads and eliminate duplication of zombie activities.
Using Parcelable for passing intent data
Parcelable is an Android-only interface which is used to serialize a class so its properties can be transferred from one activity to another.
Read more about Parcelable here.
Running Long task on UI Thread
Don’t run too many background task also don’t run any task until necessary on UI Thread.
If UI Thread stops responding as background task consumes its space, Android will show Not responding dialog box to your app user.
Something that many developers take for granted is the XML language. Extensible markup language (XML) is a markup language that’s the foundation for the Android user interface (UI) and elements.
Having prior knowledge about them can help you create better apps with complex layouts.
90% of bugs faced by Android developers are NullPointerException bugs. Therefore Android Best Practices recommends learn Kotlin.
Kotlin’s type system is aimed at eliminating the danger of null references from code, also known as The Billion Dollar Mistake.
Read more about Kotlin’s null safety here.
Android Best Practices can be developed by building a habit of reading code built by other developers is a great habit. It helps you in not only quickly learning new concepts but also helps in quickly identifying mistakes.