Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organizing code files/XML files for Android SDK

Can someone provide some strategies as to organizing my project so that it is clean? Say I have a bunch of activities; is it good to place them all into a separate package, while putting other classes (such as custom Adapters) in another package to separate "logic" better?

Also, when creating XML files for layouts, how would I separate the layout XML files logically if I have some layouts that are for certain activities and other XML layout files for custom "rows" (to use with an Adapter) I don't want to just throw them all into res/layout -- it would become such a huge hassle when the project gets really big.

like image 873
volk Avatar asked Oct 04 '11 19:10

volk


People also ask

What is leveraging Android XML?

XML tags define the data and used to store and organize data. It's easily scalable and simple to develop. In Android, the XML is used to implement UI-related data, and it's a lightweight markup language that doesn't make layout heavy. XML only contains tags, while implementing they need to be just invoked.

Which layout is best in Android?

Use FrameLayout, RelativeLayout or a custom layout instead. Those layouts will adapt to different screen sizes, whereas AbsoluteLayout will not. Definitely right. I recommend RelativeLayout since it keeps the view hierachy flat.

In which directory are the XML files stored in Android Studio?

Specifically, Android considers XML-based layouts to be resources, and as such, layout files are stored in the reslayout directory inside your Android project. Each XML file contains a tree of elements specifying a layout of widgets and containers that make up one View.


1 Answers

Say I have a bunch of activities; is it good to place them all into a separate package, while putting other classes (such as custom Adapters) in another package to separate "logic" better?

I'm not sure what best practices are, but here's how I organize my applications: I tend to put my activities in com.foo.appname.activity, content providers in com.foo.appname.content, services in com.foo.appname.service, and generic utilities in com.foo.appname.utils.

For helper classes like Adapters that are only used by one activity, I typically make them static inner classes. If they're used in multiple activities, I'd give them package level visibility in the activity package.

I don't want to just throw them all into res/layout

I don't think that the res directories are allowed to have subdirectories, so the best you can do is to come up with a good naming scheme. I typically prefix the layout file with the type: activity_foo.xml, fragment_foo.xml, etc.

like image 65
Erich Douglass Avatar answered Sep 21 '22 05:09

Erich Douglass