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

2

27

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

Aha answered 6/8, 2013 at 3:18 Comment(3)
I recommend not even thinking about fragments until you have done at least a couple of different apps using just activities.Sansbury
I wouldn't be using fragments, but since I'm going for a tablet app to start with, anything but a List/Detail flow seems stupid for my purposes. I'm getting the hang of them, even having them communicating, it's just how do I organize them that's the problem. That and Android Studio makes the fragments and activities automatically, so it's not so hardAha
See also overflow.buffer.com/2016/09/26/…Indeciduous
E
36

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_
Exsanguine answered 30/1, 2016 at 12:51 Comment(2)
THIS must be accepted.Moniz
You will just end up with a fragment folders with 100 files not related to each other, and what about viewModels? Consider this medium.com/hackernoon/… and this buffer.com/resources/android-rethinking-package-structureLubow
E
13

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.

Esdras answered 6/8, 2013 at 20:58 Comment(3)
Here is a good example of such structureGloucester
good example, thanks for the linkCleptomania
@YoushaAleayoub Explain your criticism.Alms

© 2022 - 2024 — McMap. All rights reserved.