Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Project: How best to organize the files [closed]

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

like image 355
Gareth Parker Avatar asked Aug 06 '13 03:08

Gareth Parker


People also ask

What folders are important in Android project?

The resource folder is the most important folder because it contains all the non-code sources like images, XML layouts, and UI strings for our android application.


2 Answers

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_
like image 134
Dhaval Jivani Avatar answered Sep 19 '22 22:09

Dhaval Jivani


As far as i know, there is no convention, but here is an example of how you can put your files in packages :

  • mainPackage
    • LauncherFragment
    • LauncherActivity
    • MyApplication
  • uiPackage
    • DetailsFragment
    • DetailsActivity
    • OtherTabletFragment
  • viewPackage
    • custom views
  • databasePackage
    • MainContentProvider
    • MainDBHelper
    • SecondContentProvider
    • SecondDBHelper
  • dataPackage
    • CustomAdapter
  • utilsPackage
    • xmlUtils
    • textUtils

And many others. You can search for android projects on GitHub and have a look.

like image 40
Rémi F Avatar answered Sep 20 '22 22:09

Rémi F