The problem is: I haven't done the huge project before (with bunch of activities and layouts). I don't know how the structure should be organize especially for layouts. I try to create folders and put it there but it doesn't work, just underlines elements. I create folders in this way: layout -> new -> directory (see bellow on the screen)
The question is: how to organize layouts? I don't want to have list of all my project layouts in one folder 'layout'.
They all have to be in the 'layout' folder (can be also in 'layout-land', etc).
Subdirectories not allowed.
If this or that part of a layout repeats often in other layouts you can use <include> xml tag.
Related
My Android app has two layouts, one for <= normal, and one for >= large. I can re-use my corresponding activity 99.9% in both cases, but I need it to know different bits of information for the two layouts. I could of course write a giant if expression, but I'd rather let Android work its magic, since it's already doing it by loading the two different layouts.
So, can I embed pieces of information in the two XML files and retrieve them in my activity class? Or am I completely off the map and the right approach is completely different?
Sure you can, just in the values directory define values for each size and retrieve them dynamically in your program.
/res/values-xxx
-> a.xml
/res/values-yyy
-> a.xml
...
here is an example:
<resources>
<integer name="maximum">100</integer>
...
</resources>
in your program just put:
int max = getContext().getResources().getInteger(R.integer.maximum);
for each size android will magically do the job and give you the correct value!
If you're willing to go the custom View route, then yes, you can. What you have to do is create custom attributes and apply them to your custom views which are parsed when they are created. Here is a thread that goes in to a great bit of detail about it.
The Views themselves don't have to be special. You can say, have a view called a PropertyView view which extends FrameLayout and has a method called getProperty(). You then put the property in the XML like so:
<com.example.ProperyView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:property="My Custom Property"/>
Then you would parse it using the methods described in that link.
EDIT:
Alternatively, there are other elements in the XML landscape that can be put in to buckets similar to how Layouts are. Any folder in the /res folder can have the same buckets that the Layouts can. That includes anything in the values, drawables, raw, or xml folders. You can reference these in your Layouts and the Android system will pick which ones you want. More example for how dimens work.
If you are using values to differentiate between the two layouts, then you can have different values that overload depending on screen size. See this answer.
You could do the same sort of thing with the layouts directory to create different layouts, but then use common subsections using the < include > tag to make the different views based on common sections.
Or a combination of the two. If you the want to change the behaivoir of the Activity/Fragment, you could key that on the presence of missing or present screen widgets.
I am creating different layouts to support different screen sizes.So I created folders like layout-small,layout-large and so on.But I am stuck with the error cannot find the declaration of element RelativeLayout. How do I solve this?
#jobin , android till now does not allow to create seperate layout folders/packages , so please create all your layouts in the layout folder only, u can use prefix or postfix tags to differentiate between layouts.
for ex
activity_main_small ,
activity_main_big ,
activity_main_verybig
but do remember to keep all layout Xmls under the single layout folder provided .
I want to make something that is a vertical LinearLayout on the phone version of an app, a horizontal one for tablets.
The view in question is located inside a large XML file that specifies the entire view.
The naive way to do this is to just copy and paste the file from res/layout to res/layout-sw600dp. I did this and it works. However this means I know have to maintain two files if I want to make other UX changes. This is inefficient.
What is the easiest way to abstract away the LinearLayout in question?
I have looked at Custom Components but it goes farther than I would like to go. I do not want to have to make code changes for this, it should be self-contained in XML changes.
You can reuse layouts using < include > and < merge> tags.
Doing that, you can have different containers in sw600dp and layout folders including the same common layout.
Official doc here
In my android project, I have a folder called layout to store all xml files for layout, but there is also a folder inside called layout_settings
layout---->layout_settings--->about.xml
In my code
I have setContentView(R.XX.about). How do I set it such that reflect the about.xml inside layout_settings folder.
The reason I am doing is I don't want to scatter all the layout files into flat hierarchy.
No, you can't configure or modify a folder structure in android packages res/layout because they are read only file system.
The resources mechanism doesn't support subfolders in the layout directory, so you need to keep that hierarchy flat.
If you have to do like that just you can give a xml files name like, layout_setting.xml , layout_display.xml etc and use it.
Is it possible to create subfolders under res/layout and place the layout XML files there so that one can call a view like setContentView(R.layout.questions.create); or setContentView(R.layout.questions/create); ?
From my tests, no.
You might want to consider a naming convention:
questions_create
questions_list
answers_list
Or, investigate Android library projects: http://androidblogger.blogspot.com/2010/09/android-library-projects.html - seems pretty good to add more structure.