I'm trying to figure out the best course of action when it comes to drawing various control configurations. In my case, I'm using images as buttons and would like to create "screens" for different control configurations. Below is an example:
Basically, I'd have a layout for each configuration. Depending on which configuration the user requests, different control layouts will be used. There's only ever one configuration set loaded at any given time.
What's the general direction I should take for this? Should I just use Fragments for each configuration? I imagine I'd want to use a layout for each control configuration. Controls are required to be precise, so perhaps this library, in conjunction with layouts, will work for my needs?
I would save the requested layout in SharedPreferences and in the onCreate() method get the layout id like this:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
int requestedLayout = prefs.getInt("requestedLayout", R.id.layout_default);
setContentView(requestedLayout);
Note: this only works if all layouts contain the same views, otherwise fragments will be better for this purpose.
Related
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.
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 want the same application to be delivered 2 different set of layouts. Ie the functionality is same but the graphics will be different for two different versions of the app. So i want to keep the same code and based of some variables want to decide which layout to be set for each activity. SO for each activity i will define two different layout.
This is my requirement. What is the best way to implement this. I can have an if else in each activity and define which layout to be set. Is that the right and best way. Please give your options on this
Take a look at this answer. It's about accessing a resource file from identifier, ie file name. You can do this with any type of resource (I think).
How to use getResource.getIdentifier() to get Layout?
Basically, you can do an if-else statement and assign the id of the layout you wish to use to a variable then load the layout using the identifier.
Actually there are many ways for ex you can change your layout based upon the orientation i.e landscape or portrait or you can change your layouts using languages for ex- you can create various folders for different languages.
Please explain your requirement briefly and if possible post some code also.
You can follow below links also.
http://developer.android.com/guide/practices/screens_support.html
http://developer.android.com/training/multiscreen/screendensities.html
Language Specific layout for android
http://www.c-sharpcorner.com/UploadFile/0e8478/supporting-different-languages-layouts-in-an-android-appli/
Can anybody briefly explain the advantages and disadvantages of dynamically modifying layouts as opposed to having static layouts? I faced this question in a quiz. Please explain your answer in detail. The following are True/False questions.
Dynamically-created layouts will appear on the screen and will respond noticeably faster than static layouts will.
Dynamically-created user interfaces can adapt to an application's runtime state, such as the amount of data that needs to be
displayed at any one time.
Dynamic layouts can take advantage of contextual information that's not tracked by Android's configuration system (such as current location, usage time, or ambient light measurements).
Static layouts can't take advantage of contextual information, such as the device's orientation.
The first statement is False because the idea of allowing static and dynamic layouts isn't to improve efficiency but to better seperete the view from the model/controller and to allow changes to the layout without recompiling the code. See here for more information: Android xml vs java layouts performance.
The second and third statements are True because this is information that can only be determined at runtime so to take advantage of that you would need to create some dynamic layout settings e.g. updating a position on a map, or updating the current weather for the area you are in.
The bottom statement is False because you can have layouts in XML files that are named specifically for the devices orientation e.g. layout-land.xml. Android will correctly pick this layout when the configuration is changed to landscape.
Remember that Android allows you to use both static and dynamic layouts but from what i've read most people opt for the static layout options where possible as this separation makes changes to the layout much easier. Dynamic vs XML layout in Android?
Good luck with the course, I believe i'm doing the same one.
Cheers,
Alexei Blue.
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.