Android Development Tips - Part I
Last December I’ve decided to embrace the holiday spirit and create a new challenge — to send daily tips via twitter (@cafonsomota) related with Android development. It could go from something code related to some hidden feature on Android Studio. My goal was that it needed to be something worth sharing.
I want to get back at this in a later post — but let me just say that I truly recommend this type of challenges. First, it allows you to go an extra length to find content for your daily publications and secondly, you have the chance to share what you’ve learned with the world (or just the Twittersphere, but who’s counting) and meet new people along the way.
I’ve updated them to a more medium form and divided them into categories:
- Android
- Android Development
- Android Studio
and in part II we’re going to have:
- Kotlin
- Java
- Product Design
Android
- Smaller .apks means that people will use less network data, your app will install faster and it will use less disk space. Don’t forget to check if your build.gradle file has
shrinkResources
andminifyEnabled
properties enabled: - IME options (on xml we have android:imeOptions) correspond to the keyboard action button. On text input fields, after entering the text you can define how the action key will behave and look — search, done, etc. and even create your own action for when the user clicks on it, trigger it automatically.
- You can restrict which data the user is allowed to enter on an input field — by setting the appropriated
inputType
onTextInput
/EditText
. This will also work for paste actions where the text will be filtered in case it doesn’t match the defined criteria. - Starting on API 26 we’ve got autofill. One of the features directly out of the box is text prediction. Briefly, the OS knows the data that you keep entering on different apps and when it detects that you’ll need to enter it again — it automatically prompts you. You an easily achieve this by adding an hint to your editable view — this can be either a “Phone number”, “E-mail”, etc:
android:hint=”Phone number”
Fortunately, you can also disabled it by defining:
android:importantForAutofill=”no”
-
Since we’ve been talking about TextInput you can define an error message for TextInputLayout that will be shown on the bottom. Really useful to provide some feedback when the data entered is not valid.
- It’s important to provide feedback on user actions/reflect visually the current state of your app views. Instead of having multiple conditions on your code that will check a set of attributes you can define this on a
selector
and let Android choose the correct one: - Themes is something that we don’t read much about — but Android has several good options. For instance, you can easily switch between light and dark (night) mode by just defining a theme on values-night and calling:
AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES)
- If your Android app has a direct action to make CS calls — instead of using the intent
ACTION_CALL
useACTION_DIAL
. Doing so, you don’t need to ask forCALL_PHONE
permission which apart from being one less permission required it’s also one less action for the user to take. - I’m seeing several apps where the notification icon is a solid grey. This happens on non-transparent images because the system ignores all non-alpha channels — meaning, the system will merge all colours into one. To show a specific shape you need to use transparency.
For instance, if you’re using push notifications with Firebase, you might need to add an additional meta-data to yourAndroidManifest
file to use a specific icon (that should have transparency) otherwise it will use the default — your app launcher icon. - If your minimum SDK is from the dark ages you’ve got some helper tools that you can use:
-ContextCompat.*
-ActivityCompat.*
They’re available at: com.android.support:support-compat. - There’s a really good video by Colt McAnlis on why we should use IntDef and/or StringDef on Android Development instead of of enums. Nevertheless, while browsing some recent libs I still find a lot of enums (or even hardcoded values). Depending on the complexity of your project, it can be easy to update it. You need to define the type of your constants:
-StringDef
-IntDef
-LongDef
-etc.
and create an interface that’s going to define them.
And then just hit cmd + shift + r
Android Development
- I use Android Studio both as IDE and Git client. Although it’s fairly easy to search for a specific branch you can end up with a lot of local merged branches that are just cluttering your environment. You can easily remove them but entering this cmd:
- If you want to install a debug .apk that’s not signed — don’t forget to the add the flag
-t
otherwise you’re going to have:Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
- A few months ago I’ve written a blog post about useful adb commands for Android Development: Adb command-ments One that I particularly like is
$ adb shell input text “Hello world!”
It’s easier to write on the pc and you don’t risk having hardcoded values that can easily be forgotten.
Android Studio
- There’s something that’s pretty useful to organize your code especially when a class gets a bit to big — code regions! Just enclose the methods between
//region: NAME
and
//endregion
and you’ll see a minimize/expand option near the line numbers
- Do you start to have too many Android projects? Work, side projects, playground… Android Studio allows you to organise all of them into groups — and yes, you can even add an icon to identify them better. How?
- Right button click on projects
- New Project Group And that’s it!
- I’ve talked about adb tips previously and how they can fasten your development. There’s also a really good plugin made by @pbreault that you can use directly in Android Studio — it’s really worth checking it out: pbreault/adb-idea
- Who has clicked on Run android and just a few seconds later realised that something was missing and then furiously tried to stop the building process? 🙋♂️ You have two easier ways (that don’t involve force quite Android Studio): — you can go directly to Android Studio terminal and type:
./gradlew -stop
— you can stop all the daemons on Android Studio by:
- Pressing cmd + shift + A (or control + shift + A on Windows/ Linux)
- Enter Show Gradle Daemons
- Select Stop All
(A special thanks to César Díez Sánchez for this last suggestion)
- So… who randomly pressed some strange key combination on Android Studio and now has multiline selection active? You can opt between both by:
- Mac: cmd + shift + 8
- Windows/Linux: shift + alt + insert
I’ve called this part I… so, this means there’s another one already in the works? 🤔🙌