May 10, 2013

Tips: Android design pattern

Design Pattern, design pattern is a general reusable solution to a commonly occurring problem, In case of Android, I personally feel that existing design patterns like MVC, Abstract Factory,Singleton,etc. are not fit with the Android. Most of design patterns was evolved before the era of Android/Mobile. I think that code should be simple and optimise, does not matter which design pattern you are using. 

In this article I am going to explain few rules/pattern which I am following in my projects.

1. Package Structure
                      - contain Application class only
              - contain all activities
       - contain all fragments
      - contain all web services
         - contain all adapters
                 - contain all db related classes
           - contain all models
               - contain utility/misc classes
           - contain extended/custom views
          - contain all services
        - contain all animations

2. Naming convention for Xml files

  • activity_<ACTIVITY NAME>.xml - for all activities
  • dialog_<DIALOG NAME>.xml  - for all custom dialogs
  • row_<LIST_NAME>.xml - for custom row for listview
  • fragment_<FRAGMENT_NAME>.xml - for all fragments
3. Naming convention for component/widget in xml files.

  • all component for x activity must be start with activity name
  • all component should have prefix or short name like btn for button
  • For example,name for login activity component should be like following.
    • activity_login_btn_login
    • activity_login_et_username
    • activity_login_et_password
  • Short name of major components
    • Button - btn
    • EditText - et
    • TextView - tv
    • Checkbox - chk
    • RadioButton - rb
    • ToggleButton - tb
    • Spinner - spn 
    • Menu - mnu
    • ListView - lv
    • GalleryView - gv
    • LinearLayout -ll
    • RelativeLayout - rl
4.  Reference links related to Android design pattern / architecture