New to android, have a question about layouts in general - android

I'm making a new application and its basically filled with information about Warcraft.
I have similar apps on my phone that have similar information and when I looked inside their .apk they only had like 10 layouts.
The app that I am making already has 5 layouts and it seems like I will need about 50-60 layouts.
So now my question is it normal to have that many layouts? Or do I have to learn to make one general layout and keep reusing it? For example, like if I need to display information about a topic for instance the classes in warcraft which are 10 different classes with 2-3 different guide pages on average for each class, would I need to make a different layout for each page or is their a better way of doing it?
I would really appreciate any input/suggestions.

What I recommend is having one layout for every type of screen (basically one per Activity) and use Java to fill in all of the info. Use getResources().getString(int id) and pass something from R.string. That means you need to keep all of your information in a strings.xml file in your values folder (located in /res/values). List all of your views in the layout XML file and then find them by ID and set their values.
It's best to keep all of your string resources in a separate XML file and not hard code them into the layout (otherwise it's a pain to replace every instance of a word you realized you misspelled or something). If you don't know how to write XML, that's okay since there are tools in Eclipse, but I HIGHLY recommend learning it.

You can re-use layouts without any problems. In fact for maintaining the code it is a very good idea to do so. No one wants to maintain 50+ layouts and associated code.

Related

How to present an Android project when only the layout has to be done

I have to make a new design for an Android App, but I only have to create the visual part (native Android). The app logic would be created by another guy based on what I present to him.
My question is? How would this be managed correctly? Do I have to make the XML's of each layout? How could I show the other person my progress?
What things should I consider when doing this?
Thanks
You need to mock the app first (create a prototype) as suggested by NoChinDeluxe. Then if you guys go ahead and decide to code it, the answer to your problem is separation of responsibilities. As Jeffrey said UI work is not only about layouts, but code as well. What I would suggest is that you and the other guy get together first and define some contracts (interfaces) that will allow you guys to split the work and work in parallel. Therefore, he can create the business logic of the app without worrying about the UI implementation. You, on the other hand, will have to mock that business logic he's implementing at the beginning so it doesn't block your UI work.
you could create layout XML files for all the Activities/screens, using resources (icons, etc as suggested by #NoChinDeluxe). However since you'd want to run the mock app, you might want to also create a "throw-away" Activity that allows you navigate to different screens of the app. Here you can add a number of buttons and when you click on each button, your app shows a specific activity. This way, you will be able to show your colleague all the screens you have created. I hope this helps.
This may not be what you want to hear, but creating Android layouts isn't a design task. They are closely tied to the code, and the design of them is going to depend on how the engineer chooses to implement the app.
Here's an example. You might have a grid with 4 cells. You could use a RelativeLayout, a LinearLayout, or GridLayout, or a GridViewLayout. Which will use choose?
I'd suggest providing your engineer with mockups and graphical assets where required. Let him / her take those and create the layouts. If you want to create layouts as a (visual-only) reference for engineering, great, but it's certainly a non-optimal tool for that task.
Things You will consider when doing visual part:-
You have to work on the resource folder of your application
Layout : All Layout you have to prepare.
Drawable : Images and drawable .xml.
Inside Values folder you will find
dimen.xml : For different devices dimen you can set.
string.xml : You can store string for hint or other purpose.
style.xml : For designing or theme or custom design.
color.xml : Color which are going to used in the application.

Dynamic or xml layout? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am little confused about the best way how should I design my application. I have used XML to define the position of objects. But now when I'm developing application with more complex design I'm starting to think dynamic define of object can be more practical and suits all screen resolutions. Basically I would like to combine imageViews, ImageSwitcher, TextSwitcher etc... Now I used XML code and it has more than 270 lines and also it is very chaotic, difficult to suit to all screen resolutions and every small change will crash my app. I would like to use percentage for set positions of objects, set scale of objects etc. Should I go on with XML or is it better if I start to use programmatically way to set positions of objects. I was looking for some books or tutorial about dynamic designing app but I always found only easy tutorials can be also done in XML. Has someone good hint for some book or tutorial about dynamic designing? Thanks a lot.
Now I used XML code and it has more than 270 lines
If you have that many lines in one xml file then I think you need to reconsider your design. Use Fragments, ViewPager, ListView, etc.. depending on your needs. But I don't know how you can have that many lines in one layout file.
difficult to suit to all screen resolutions
I'm not sure how it is any easier doing the layouts programmatically. It really isn't that hard to support multiple screens. Use dp, layout_weight with LinearLayout when necessary, and other various tricks. See this part of the docs about supporting different screens. For the most part, you just create different layout folders (3-6 maybe depending on what your need)
Should I go on with XML or is it better if I start to use programmatically way to set positions of objects
It is typically easier to do your layouts through the xml. There are certain properties that need to be set in xml (can't think of any off-hand but they exist). I would only suggest doing it programattically when absolutely necessary. There have been times when I spent a bunch of time creating a layout programmatically then realized there was a much easier way to do it in xml.
The main reason, that I know of, to create a layout/view programmatically is if you don't know the number of/ what kind of Views will be used. Say, for example, if a user can press a Button to add another field to contacts. Even then you can do it in xml if you know the amount of Views available.
Make sure to go through the Design Sections of the Android Docs and you can Google design patterns, principles, etc... and find a lot of information.
Speaking from experience - the best way to create layouts is using XML. This should not break your app every time. This sounds like you are not following best practices. One of the major drives behind using XML rather than programmatic view creation includes supporting different screen sizes and resolutions, and simplifying the use of Fragments.
That aside, there was a time about a year ago that I asked myself the same question. The result was an open source library for creating layouts using Java or XML (interchangeably), and supports customizing sizes based on the current display, using percentages, etc. It is called AbLE (Annotation-based Layout Engine), and is available for download here: https://github.com/phil-brown/AbLE .
I do not currently use this library (I use XML instead), and do not currently maintain it.

managing ID's in android layout file

I don't know whether i am asking the right question or a wrong one.
it may sound foolish but still i feel to clear my basic doubts.
the question is:
i have three different layout files for a single activity.
depending upon the purpose for which the activity is started, it will take one of these layout files as its content.
these layouts have various fields in common.
i have a question that in these different layout files can i have the same "id's" for the common fields.
for ex: i have a save button for all the three layouts.
in all those three layout files..... can i have (For the button)
android:id="#+id/save_button"
the same ID attribute in all the three files.
i require this because i have too many elements(components) in my layout files.
if they can be identified with common names (as they serve the same purpose in their respective layouts) there would be very less names/ids to remember, which will make my program easy to be readable and less things for me to remember.
else i will have to write the same code for components with same functionality.
thankyou in advance.
your answers will help me clear my doubts. please correct me if i am moving with wrong concepts.
Not only is this allowed, but I would encourage it. Using the same id across files allows you to create new layout files without having to change any of the code referencing the button. As long as the id is descriptive of what it relates to, then it shouldn't cause any problems.

Android GUI XML vs Code

I have a short question according to creating GUIs in android. What way is better xml or coding?
I always read that xml is way better but imagine you have a scrollview.
Inside the scrollview is a relativelayout. Inside that there shall be several rows with an ImageView next to a TextView next to a RadioButton. The number of rows can vary.
Is it really better to make lets say 50 Views in the xml or a loop in code where these views are created?
Each has its pros and cons. Just to name a few:
XML
pros -> fast GUI development, keep code clean
cons -> static
Dynamic (code)
pros -> able to react to runtime conditions
cons -> more code, which means poorer maintainability and potentially buggier
If you need to add components dynamically, only way is go with code (or) mixed approach (define layout in XML and add components in code). If your components are static XML may be best.
Dynamic content is, of course, added dynamically. So your example would require some java code. You should only add the dynamic part programmatically though, so you'd still use an xml document for the static parts (it's very unusual for a layout to be completely dynamic).
If you have a fixed number of views then yes, I'd write 50 of them in xml rather than with a loop. I guess you're wondering about code duplication and, as far as I know, you'll get some when using xml.
(One way to minimize code duplication within xmls' is with the usage of styles and themes)
I agree with the above. XML is a better approach to this even when you require dynamic updates you can still use XML bits and pieces to render the content. your code will be based on XML elements but XML files will be independent. hence if you break a funcitonality in the code you know that its your business logic thats broken not the UI part, which will make it easier to develop and find problems easily.
Why you do not use a ListView instead of a ScrollView.
It will be simplier to implement and performances must be better with it.
Create a XML file with a ListView and in your activity implements your own adapter to instanciate the rows.
You can find a lot of tutorials on internet talking about that, I'm sure you will find what you need !
Good luck.

how to dynamic load two different layout based on condition in android code

I have a small project, which will require the one android application be used by different group.
For example: one group is the regular user, another group is the admin user.
My question is, if I like to create one similar UI android application, how to dynamically load the layout based on different condition (condition may be embedded in the local sqlite database), and move forward, the same condition will be kept, and the layout will be slightly different for different group of users.
as Android uses Java I believe that actually your problem is an exact match for an Abstract Factory implementation. Take a look at Abstract Factory Design Pattern. I believe that is what you need.
http://en.wikipedia.org/wiki/Abstract_factory_pattern
http://www.oodesign.com/abstract-factory-pattern.html
http://www.mydeveloperconnection.com/html/gof_design_patterns.htm
regards.
[]s
The code is the same, just different layouts? I would simply inflate different layouts depending on your condition.
Just a fast shot:
res/layout/group01_main.xml
res/layout/group02_main.xml
...
res/layout-land/group01_main.xml
res/layout-land/group02_main.xml
...
And in your activities setContentView() to the correct one.

Categories

Resources