Best approach for handling same layouts for different activities - android

I have been working on application which has 3 types of accounts related to it. We create a single layout and view/hide items on it depending on, from which account you are currently logged in.
With this approach, we have activities/fragments doing a lot of different things, they handle all cases wrapped in if/else checks etc. With growing project, it is becoming hard to maintain these classes.
Say, if I have to hide a view in certain scenario, I have to look around many if/else checks just to hide a single button because if I hide it on one place other check will make it visible again, really hard to maintain.
I am looking forward for best advises on this issue from the experts.

If you are struggling with a lot of if/else scattered in the code, maybe you should use polymorphism in your code.
Create an abstract class for the Activity, then specialize it for each particular type.
Use the Factory method pattern for creating objects of this hierarchy. This method will use the parameters for deciding which concrete class to instantiate, and then it will initialize the instance being returned.
Use the Template Method pattern if there is an algorithm common to all sub-classes but that contains some open steps that should be implemented by each class.
Use the State/Strategy pattern if you need polymorphic code that may be modified at runtime.

If your separate apps require minor customization and theme changes, but are really the same base app, multiple flavors is definitely the way to go. However, if both apps require a lot of custom code differences, you might want to rethink using multiple flavors strategy.
Also, take notice of the difference between flavors and build types. Use flavors for situations where you might need different versions of the same app in the Play Store, for example, free and pro, or for situations where you are customizing the same app for multiple clients.
for details http://www.androidauthority.com/building-multiple-flavors-android-app-706436

you have create new xml files in which has common view's for your activity and fragment then need to use include tag in xml for adding those common view's into your activities & fragments xml.

Create different xml for same layout and use <include layout="#"/>
Tag to create the layout, it will reduce if/else and also provide you the code re-usability

I think you should create separate layout for all 3 types of account and you can create PickLayout static class/method to pick the layout by type
int getLayout(int type){
return layoutMap.get(type);
}
if you have re-usable layout then you should use include, merge or you can use ViewStub also.
if you have chain of if/else then you should use Map link that will be scale-able, error-prone free.
And try to follow android suggested design-pattern that will be helpful for writing test case also.

Related

How to add functionality to existing classes?

Currently, I am developing an android library. Basically the idea is to add some functionality to any android widget that there is. The library is used to create compound views, and the user should be able to convert every existing android widget/view into a compound view with some additional functionality I want to add dynamically.
Every one of these views should still be useable inside XML files, which means I cannot change the constructor too much.
Another requirement is that I need an option for the user to provide some classes for the views. By that, I mean that the created compound view is going to have a public variable named viewStore. Thy type of viewStore would either be the user's implementation of the ViewStore interface (preferred way but I am pretty sure this would require code generation as discussed later) or would be provided via generics.
In the end, a compound view would have this folder structure like this:
MyView Folder
MyView extends CoolViewWithViewStore extends AndroidWidgetChoosenByUser
MyViewStore implements ViewStore <- used in CoolViewWithViewStore
One option is to extend every single widget. I don't think I need to explain why this is a bad idea. Furthermore the user couldn't provide the additional classes that are needed.
Another one I thought of was Annotation with code generation. The problem I came across here was that the user needs access to variables of CoolViewWithViewStore inside MyView which wouldn't be possible because CoolViewWithViewStore would be generated at compile-time and furthermore the user could accidentally use his class inside XML instead of our generated one.
I would like to hear if anybody has a better idea of how to handle this or if there even is a clean solution to this to achieve this kind of architecture. If anybody has a better idea of how to structure my library I would like to hear this as well.
Using Kotlin extension function you could extend a class with new functionality without having to inherit from the class.

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.

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.

New to android, have a question about layouts in general

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.

Categories

Resources