Sitemap

Android Code Style Best Practices

3 min readSep 4, 2018

--

Today , I thought to share with you some of the Android best practices that i follow .

Understand the system BUILD

  1. Use Android Studio , the most suited IDE
  2. Your default option must be gradle
  3. Manage and download dependences
  4. App/build.gridle — make sure the min version of your app
android {
compileSdkVersion 23
buildToolsVersion '23.0.2'

defaultConfig {
applicationId “com.cn.app"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}

5. Add the project dependences in

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile ‘com.android.support:appcompat-v7:22.1.1'
}

Project Structure

  1. Create a source package as
com.<company_name>.project

2. Default structure

Default-structure

├─ library-foobar

├─ app

│ ├─ libs

│ ├─ src

│ │ ├─ androidTest

│ │ │ └─ java

│ │ │ └─ com/comp_name/project

│ │ └─ main

│ │ ├─ java

│ │ │ └─ com/comp_name/project

│ │ ├─ res

│ │ └─ AndroidManifest.xml

│ ├─ build.gradle

│ └─ proguard-rules.pro

├─ build.gradle

└─ settings.gradle

3. Professional project Structure

Get Faheem unnisa’s stories in your inbox

Join Medium for free to get updates from this writer.

Follow Model-View -Controller architectures

├─ library-foobar

├─ app

│ ├─ libs

│ ├─ src

│ │ ├─ androidTest

│ │ │ └─ java

│ │ │ └─ com/company/project

│ │ └─ main

│ │ ├─ java

│ │ │ └─ com/company/project

│ │ │ │ └─ activity

│ │ │ │ └─ adapter

│ │ │ │ └─ bean

│ │ │ │ └─ config

│ │ │ │ └─ fragment

│ │ │ │ └─ service

│ │ │ │ └─ util

│ │ │ │ └─ views

│ │ │ │ └─ CustimApplication.java

│ │ ├─ res

│ │ │ └─ anime

│ │ │ └─ drawable

│ │ │ └─ drawable-hdpi

│ │ │ └─ drawable-mdpi

│ │ │ └─ drawable-xhdpi

│ │ │ └─ drawable-xxhdpi

│ │ │ └─ layout

│ │ │ └─ menu

│ │ │ └─ raw

│ │ │ └─ values

│ │ │ └─ values-v21

Activities and Fragments

  1. Use Fragments in all the UIs
  2. Use Activitys to manage Fragments
  3. Avoid nested Fragments
  4. Have a short modular code in its Fragment.java classes
  5. If you are working on a large projects have separate logger class , Allow it to through out the app

Resources

  1. Organise the layouts
  2. One attribute per line
  3. android:layout_ attributes on the top
  4. Style attributes at the bottom
  5. Keep the common attributes in styles.xml
  6. Dont hard code the android:text and any dimensions , Define them in the string.xml and dimes.xml .
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
style="@style/text_subhead"/>

Use styles. Almost every project needs to properly use styles, because it is very common to have a repeated appearance for a view. At least you should have a common style for most text content in the application, for example:

<style name="ContentText"><item name="android:textSize">@dimen/font_normal</item><item name="android:textColor">@color/basic_black</item></style>

7. You can also split the large style file into several small files .like styles_mainscreen.xml , styles_productdetails.xml

8. The colour Palette :Use proper colours instead of

Don’t


<resources>
<color name="button_foreground">#FFFFFF</color>
<color name="button_background">#2A91BD</color>
<color name="comment_background_inactive">#5F5F5F</color>
<color name="comment_background_active">#939393</color>
<color name="comment_foreground">#FFFFFF</color>
<color name="comment_foreground_important">#FF9D2F</color>
<color name=“comment_shadow">#323232</color>
</resource>

DO

<resources>
<!-- grayscale -->
<color name="white" >#FFFFFF</color>
<color name="gray_light">#DBDBDB</color>
<color name="gray" >#939393</color>
<color name="gray_dark" >#5F5F5F</color>
<color name="black" >#323232</color>
<!-- basic colors -->
<color name="green">#27D34D</color>
<color name="blue">#2A91BD</color>
<color name="orange">#FF9D2F</color>
<color name="red">#FF432F</color>
</resources>

9. Dimensions are same as colours :

<resources>
<!-- font sizes -->
<dimen name="font_larger">22sp</dimen>
<dimen name="font_large">18sp</dimen>
<!-- typical spacing between two views -->
<dimen name="spacing_huge">40dp</dimen>
<dimen name="spacing_large">24dp</dimen>
<dimen name="spacing_normal">14dp</dimen>
<!-- typical sizes of views -->
<dimen name="button_height_tall">60dp</dimen>
<dimen name="button_height_normal">40dp</dimen>
</resources>

10. Strings :

strings.xml

Name your strings with keys that resemble namespaces, and don’t be afraid of repeating a value for two or more keys. Languages are complex, so namespaces are necessary to bring context and break ambiguity.

Bad

<string name="network_error">Network error</string>
<string name="call_failed">Call failed</string>
<string name="map_failed">Map loading failed</string>

Good

<string name="error.message.network">Network error</string>
<string name="error.message.call">Call failed</string>
<string name="error.message.map">Map loading failed</string>

11. Views in XML:Avoid heavy nested views.

12. For more you can also check out the

References: https://github.com/futurice/android-best-practices

Note : This is the article i have written few years back in my personal blog but its offline now , I hope its still relevant and helps some one.

--

--

Faheem unnisa
Faheem unnisa

Written by Faheem unnisa

Data Analytics | Marketing | AI | LIFE | Sql, Python ,Mathmatics, AI , ML , Tableau my Fav https://www.linkedin.com/in/faheemunnisa/