Ogechi chiama
ogechi chiama's

ogechi chiama's

Android Good Practices

Android Good Practices

Simple concept to know while building an android project

Ogechi chiama's photo
Ogechi chiama
·Sep 23, 2021·

3 min read

Introduction

Due to tight deadlines, we tend to produce codes that are better for the primary time, resulting in a large number of codes and repetitions, making the project difficult to read.

I recently worked on a project where I made a simple but costly error, which resulted in additional time spent modifying and cleaning the codebase.

I'll be writing about a simple principle that should be adopted in order to avoid making such blunders as a beginner when working on any project. Small errors like this might lead to countless hours of reworking.

Simple things to look out for

  1. Formatting of code
  2. Storing hard-coded string in the value/string.xml file
  3. Storing dimension in values/dimen.xml file
  4. Storing colour in value/colour.xml file

Screenshot 2021-08-21 at 21.08.08.png

The res/value folder holds all resources used in the android project like strings,colour, dimension and styles.

1. Formatting code

Not formatting your code makes it difficult to understand, makes it harder to notice potential bugs in your project, and makes it difficult for someone else to read your code later on. Formatting, in a nutshell, increases readability.

Screenshot 2021-08-21 at 21.36.30.png To format your code, navigate to the file you want to format and highlight the whole code in the file. On the top menu bar click on the Code option, inside click on the Reformat Option to format your code in the specific file

2. Storing hard-coded strings

The versatility of translating your program into different languages is limited by hard-coded strings that are static. You'll have to go through the project and replace every single string.

Hard coding strings into your layout files is not a smart idea. You should save them in a string resource file and then use them in your layout. This allows you to update every instance of a word, such as "Voting results", in all layouts at the same time by just updating your strings.

Before storing the hardcoded string

Screenshot 2021-08-21 at 21.12.33.png

After storing the hardcoded string

Screenshot 2021-08-21 at 21.10.03.png

3. Storing of Dimension

A dimension is defined by a number and a unit of measurement, such as 5dp, 3sp, and so on. Dimensions can be stored in a res/dimen.xml file and reused throughout your project.

Before storing the dimension

Screenshot 2021-09-14 at 22.40.00.png

After storing the dimension Screenshot 2021-08-21 at 21.09.44.png

Storing of colour

Colours too are not left out which are also stored in a res/colour.xml file for reusability.

Before storing the colours Screenshot 2021-08-21 at 21.13.12.png

After storing the colour Screenshot 2021-09-14 at 22.41.17.png

Conclusion

These small ideas may seem insignificant, but they can go a long way toward making your project more readable and with less repetition.

I hope you find this information to be very useful.

 
Share this