Android development tips — Part I

Carlos Mota
Feb 11, 2019 · 7 min read
Image for post
Image for post
Copyright @ Android, Google

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

1. 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 and minifyEnabled properties enabled:

Image for post
Image for post
build.gradle file with shrinkResources and minifyEnabled properties enabled

2. 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.

Image for post
Image for post
IME Options for “done” with a specific callback to deal with the user click action

3. You can restrict which data the user is allowed to enter on an input field — by setting the appropriated inputType on TextInput/EditText. This will also work for paste actions where the text will be filtered in case it doesn’t match the defined criteria.

4. 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”

5. 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.

6. 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:

Image for post
Image for post
Custom selector for a button (with the states disable and pressed defined)

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

8. If your Android app has a direct action to make CS calls — instead of using the intent ACTION_CALL use ACTION_DIAL. Doing so, you don’t need to ask for CALL_PHONE permission which apart from being one less permission required it’s also one less action for the user to take.

9. 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 your Manifest file to use a specific icon (that should have transparency) otherwise it will use the default — your app launcher icon.

Image for post
Image for post
Android Manifest with a specific icon defined for Firebase messages

10. 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

11. 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

Image for post
Image for post
Example of a StringDef and IntDef for different types of data

Android Development

1. 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:

Image for post
Image for post

2. 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]

Image for post
Image for post
Install a debug .apk via adb

3. A few months ago I’ve written a blog post about useful adb commands for Android Development:

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

  1. 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

Image for post
Image for post
Code regions

2. 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?

  1. Right button click on projects
  2. New Project Group And that’s it!
Image for post
Image for post
Organisation of different Android projects

3. 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:

4. 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:

  1. Pressing cmd + shift + A (or control + shift +A on Windows/ Linux)
  2. Enter Show Gradle Daemons
  3. Select Stop All

(A special thanks to César Díez Sánchez for this last suggestion)

5. 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? 🤔🙌

Code, Procedure and Rants

Trying to be a kind of CPR for code(rs).

Carlos Mota

Written by

👨‍💻, 🔭🌖, 🗺, 📸, 🏃‍💨, 🇵🇹, 🖖 - is this how I should use emojis? Also: https://www.cafonsomota.xyz/

Code, Procedure and Rants

Trying to be a kind of CPR for code(rs). Or maybe its the acronym for Code & Practice makes Results, or Came, Pwned and Returned…. You decide.

Carlos Mota

Written by

👨‍💻, 🔭🌖, 🗺, 📸, 🏃‍💨, 🇵🇹, 🖖 - is this how I should use emojis? Also: https://www.cafonsomota.xyz/

Code, Procedure and Rants

Trying to be a kind of CPR for code(rs). Or maybe its the acronym for Code & Practice makes Results, or Came, Pwned and Returned…. You decide.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store