Organizing Code / XML Files for Android SDK

Can someone give some strategies for organizing my project so that it is clean? Say I have a bunch of things to do; Is it useful to place them in a separate package, while other classes (for example, custom adapters) in another package are better to separate the "logic"?

Also, when creating XML files for layouts, how would I logically separate XML layout files if I have some layouts that are for specific actions and other XML layout files for custom “strings” (for use with the adapter) I don’t want to just throw them in res / layout - it will become such a big problem when the project becomes really big.

+6
source share
2 answers

Say I have a bunch of things to do; Is it good to place them all in a separate package, when placing other classes (for example, custom adapters) in another package, in order to better separate the "logic"?

I'm not sure what the best practices are, but here is how I organize my applications: I try to post my actions in com.foo.appname.activity , content providers in com.foo.appname.content , services in com.foo.appname.service and common utilities in com.foo.appname.utils .

For helper classes, such as Adapters , that are used by only one action, I usually make them static inner classes. If they are used in several actions, I would give them the visibility of the package level in the action package.

I do not want to just throw them in res / layout

I don’t think res directories are allowed to have subdirectories, so best of all you can create a good naming scheme. I usually prefix the layout file with type: activity_foo.xml , fragment_foo.xml , etc.

+8
source

All these offers, of course, are your choice. But when I develop something, I use to separate the logical layer from the "visible" layers and classes. I mean, I use different packages for

 a) Activites b) Classes or Objects c) Interface classes d) Database classes e) Interaction with Database 

I also create different packages for everyone, so you can organize them better. But it is always your choice.

And with your layout ... I don't know if you can better organize the layout. When you create your project, if you see, in your gen folder there is a class R.java. This class automatically detects folders such as layout, drawable, raw ... But I'm not sure if you can make subfolders inside it.

+2
source

Source: https://habr.com/ru/post/898623/


All Articles