Android Project: How best to organize the files

Gareth Parker picture Gareth Parker · Aug 6, 2013 · Viewed 15.7k times · Source

I'm building my first android app, and it's gotten a little messy already. I'm using List/detail patterns because they're what fit the circumstances, but because I'm developing for both mobile and tablet it's getting a bit out of hand. For every screen (List/detail being one screen), four files are created. ListActivity, ListFragment, DetailActivity, DetailFragment. Having four screens so far, and literally just starting the project, I have 12 files, plus three helper files for one database table.

What I'm asking, is what's the best way to organize this? I'm using Android Studio, and it seems I can't sort the files into folders without putting them in separate packages. So do I do something like com.domain.app.screen1.(Fragments|Activities), com.domain.app.screen2.(Fragments|Activities) and so on? Or do I just put up with it? Or is there a better way of doing this?

If I'm being unclear, just let me know and I'll try to clear it up

Answer

Dhaval Jivani picture Dhaval Jivani · Jan 30, 2016

Good Explain by @Eric Oestrich:

Writing a medium to large Android app requires having code structure. In creating our latest Android development project, I came across a structure that has helped me out.

Java Code :

  • com.example

    • activities

      Contains all the activities. Classes are all named with Activity at the end. That way, you can immediately know what it is when reading Java code that doesn't have its full package name.

    • adapters

    Contains all the adapters.

    • authenticator

    Contains any class related to signing a user in. I create a local account and having all related classes together is very handy.

    • data

    Contains all classes related to data management such as ContentProvider and SQLiteHelper.

    • data.migrations

    Contains all of my SQLite migrations.

    • fragments

    Contains all fragments.

    • helpers

    Contains helper classes. A helper class is a place to put code that is used in more than one place. I have a DateHelper for instance. Most of the methods are static.

    • interfaces

    Contains all interfaces.

    • models

    Contains all local models. When syncing from an HTTP API I parse the JSON into these Java objects using Jackson. I also pull Cursor rows into these models as well.

    • preferences

    Contains all classes for custom preferences. When creating the preferences I required a custom PreferenceDialog as well as a custom PreferenceCategory. They live here.

    • sync

    Contains all classes related to syncing. I use a SyncAdapter to pull data from an HTTP API. In addition to the SyncAdapter a SyncService is required, so I created a package.

Layouts :

  • Activity Layout name start with activity_
  • Adapter Layout row name start with row_
  • Fragment Layout name start with fragment_