Android layout packaging - android

I have android project where I am using lots of layout as common resources in other activity layouts. for example, I have a layout for headers , which I include in 4 activity layouts for visual consistency. Since I have lots of common layouts created, my layout folder is very cluttered. I was looking for a way to package them separately. What I wanted to do is to create another sub-folder inside the layout folder and put all the shared resources there like :
under res folder
layout(folder)
--activity_home.xml
--other_activity.xml
--common ( folder)
--header_layout.xml
--other_shared_layouts.xml
From this link in SO, I found that its not supported in android which was answered 4 years ago. It mentions that, gradle can resolve this issue by proper mergings, but I would prefer other options if there are any.
I was wondering if there are any other ways to handle this scenario? I am just trying to organize the layouts file so that they don't clutter in one folder.
Is gradle the ONLY way to go ?
This is my SDK details if that matters .
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="21" />
Thanks in advance for any kind of input.

This is still unsupported. Android studio's Android scoping looks slightly better.

Related

android shared layout from library to app

I am creating three apps that are almost the same except some changes in the assets and raw files between them.
So I wanted to create a library from the first one, that will contain all the logic and activity and all the layouts and resources, and then import this library to the other two apps, and only change the assets and raw files.
the problem is that when the layout from the library loads, it is loading the assets from the library itself, and not the assets of the main app.
I am sure that my design here is wrong then I would also appreciate if someone can explain to me what is the right way to share activities and layouts as libraries in android.
NOTE: I don't want to use flavors. I want to build it as a completely different projects.
cheers

Why the editing area or the main activity of Android studio where we design our UI can't be seen?

I'm a newbie to Android studio and is going to start with my first project.
As per a tutorial, I created an empty activity project. But when my project screen opens, I can neither see any main file that is created after making a new project nor I can see the editing area where buttons, text, etc are dragged to make the UI.
I can only see a black screen on the right side with some tips and 2 folders on the left side - Gradle files and project.
So where's the problem? And how to fix it. I searched this question on many sites but was unable to find an easy and understandable answer. Pls if anyone can answer, I'll be obliged...
EDIT
As this was my first question on SO, the question was poorly stated and formatted. Apologies for that...
Although, I have solved what the problem was (was a silly first-time thing), I am attaching a screenshot of the problem(it's not the exact same as what I was encountering 2 years ago but similar to it) image.
One needs to wait for all the Gradle background tasks to finish and needs to expand the app folder to see java classes or layout files, drawable etc..
Take a look at the docs on how to make a new project and the project folders hierarchy
here https://developer.android.com/training/basics/firstapp/creating-project
Mainly after you create a new project and open it, you will get two main folder app and Gradle scripts, so in order to access your project packages you only need to expand the app folder then you will find the manifest file, java folder where you will find your project activities, and a res folder, here you will find your main ressources folders like drawable, layout and values.
In order to change and add UI elements you need to access the layout folder and that's what you are asking for.
So to resume, to access the java code
app > java > com.example.myfirstapp > MainActivity
To change and create your UI
app > res > layout > activity_main.xml

Manage layout sudirectories

My layout dirctory starts to be messy. As the java directory, can I create subdirestories for layout? I creates Res subdirectory in studio 1.5. I can see them in the directory, but not through the studio.
I've read Can the Android Layout folder contain subfolders? specially the very good comments from hitch.united but I was wondering if that is still valid with studio 1.5?
why can't I see my created dir through the studio?
The solution you referring to is still valid and still the only way of solving it. And, what's even worse, in Android Studio 2.0 it's still not supported out-of-the-box.

Best practice on creating structure in resources

the res/layout* dir of my projects are getting really messy lately and as there are afaik no subdirs allowed there to change that. So it is hard to get structure in there - how do you guys do that?
I don't know about others but for myself I like to use prefixes to help keep things sorted, essentially taking the place of subdirectories. For example, all my activity layouts start with 'activity_' and all my UI controls start with 'control_', notifications with 'notification_', etc.
I am then left with a flat directory with at least some structure, something like:
activity_graph.xml
activity_main.xml
activity_map.xml
control_graph.xml
control_title_bar.xml
notification_just_label.xml
notification_with_progress_bar.xml
...
It's not perfect, but it works for me.
Sorry subfolder inside the layout folder is not possible other than docs specification.
Just check this and this and this question.
Another possibility is to refactor some elements of your application into libraries.
For example you could extract unrelated fragments (as fragments should per se be unrelated) into libraries, mark that project as an Android library (check "Is library" in project properties) and then use your own libraries in your main project.
The resources you need within the separate libs will not collide anymore, but will be "thrown together" at build time, so access is fine.
Here are the docs for that.

organizing android apps in eclipse

I have an app on the iPhone and need to port it to android. For this I would like to group screen related files like classes and xml per screen in one "screen group" per screen somehow, ideally also strings and other value files
if I use folders I can only group res files separately and src files separately.
what would be the best way?
Thanks very much!
EDIT:
If that should not be possible, how to best then solve this issue? Do you create a subfolder in the src and another in the res for each screen?
The way you group files for the iphone is not possible for an android project. Android has pre determined folders which hold specific files, if you break this structure, your building process will fail. Its not ideal but that just how it it.
When it comes to source java files, they follow the concept of packages which are basically folders. The 'src' folder is the part where you can create sub folders as you desire. If you are adamant about keeping the files related to a screen in one place, you should create the layouts with java code and not use layout xml files.
But using xml layout files make development much easier and faster. Consider that as the presentation and java files as the logic+data. So group java files as you want and leave xml files in the layout folder with easy to identify names.
android uses certain directory layout for project structures (i.e. convention over configuration). Basically you will want to put your XML layout files in res/layout directory. Please read http://developer.android.com/guide/developing/projects/index.html#ApplicationProjects for further information.
Unfortunately, there's no easy way to do this in Eclipse. You can't create custom directories in your Android app's /res directory, you can only use permitted dir-names. E.g. you can't have a /res/layout-myscreen1 and /res/layout-myscreen2. You also must put your resources in /res, and your code files in packages, so they're at separate places in your project.
You can use Working Sets to group related files together however, but they're quite painful to use IMHO. Check the eclipse docs and tutorials out on them.

Categories

Resources