Coding Style: Two activities with mostly same code but different content view - android

I have two activities that have the same code. The main difference is that they have different content views (and therefore a few different elements).
What is recommended? Should I leave it like it is in two activities or should I make it one activity and and solve it using if else queries each time when there is something about the view (e.g. setcontentview and a few (5) other view related code blocks)?
For example
if (isLandscape) { //landscape looks different than portrait
setContentView(R.layout.activity_landscape);
} else {
setContentView(R.layout.activity_normal);
}
Is this recommended / good coding style?

If you are setting different views for portrait and landscape this can be done through the resource files using a layout-land directory and adding the new activity layout with the same name in the landscape directory.
So you would have a activity.xml file in both the layout and layout-land folder.
To answer your question in general though, it works well to use a member variable in your activity to distinguish between your two cases. Then when you need to do something based on the state of this variable using the if/else logic.
For example, using a separate layout with a unique view container on tablets. You could check if(findViewById(R.id.tablet_container)) != null) then you would set your isTabletView member variable to true or false based on this. Any time you needed to do something based on if the app is being run on tablet or phone you can reference this variable.

Related

How do I pass information from an XML layout to its Activity class?

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.

android: design code to dynamicaly select layouts

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/

Android XML layout declaration (portrait, landscape modes)

when we have two XML layout files for an activity one for portrait and one for landscape mode, is it necessary for their root views to have the same ID or they may have different?
IF you have separate layout files (i.e.: for different orientations), they can be completely different.
however it depends how you would like to use them in your code.
Update:
To check orientation in code use:
getResources().getConfiguration().orientation
It is either ORIENTATION_LANDSCAPE or ORIENTATION_PORTRAIT.
http://developer.android.com/reference/android/content/res/Configuration.html#orientation
If you do not need to use the view through a findViewById, then it won't be a problem to have different names. Generally, the layout changes depending on the orientation, but it contains the same views so you should ask yourself: Why do I want different name for my root view? If it's only to check the orientation, then you should not use this solution. See the answer on how to check orientation: Check orientation on Android phone

How to set starting Activity for different screen size in android manifest

Is possible to set different starting activity for different screen size?
I have file res/layout-large.xml and this layout is different to res/layout.xml because in layout-large is more buttons etc. and I need start this activity in tablet because for this activity is different source file.
thx for help
Instead of trying to start different activity, use Fragments. And base on different screen size load different fragments with their UI and Business Logic
Please check How to adapt ui
Jedil answer is correct, fragment is better than activity for this.
If you must use activity, run logic to decide which config you want. Then inflate the corresponding layout file with
SetContentView(r.id.layout_you_want_to_use)

tablet/handset layout structure with fragments

In this article http://developer.android.com/guide/practices/tablets-and-handsets.html
we use res/layout/main.xml with one fragment for handsets and res/layout-large/main.xml with two fragments for tablets. We must check if second fragment is in the layout to define if app runs on tablet or on phone.
I have 4 layout (2 for phone and 2 for tablet):
layout-port
layout-land
layout-sw600dp-port
layout-sw600dp-land
I check screen orientation to define if display is in portrait or landscape mode and check if layout contains a fragment to define if it is tablet or phone.
Is there any better way to work with layouts and fragments?
Is it possible to use one layout if we have two fragments for example http://i.stack.imgur.com/FtzKs.png and if a phone display doesn't fit both of them to show only the first one?
Thanks in advance! :)
What you're showing there is indeed possible! That is something called a Master Detail Flow (if you're developing in Eclipse with adt, check out he new activity wizard, which provides this as a template option).
This layout is basically just two fragments inside an activity (or, now, as of android 4.2, they can be nested in another fragment as well!) that interact with each other in a certain way. To create the layouts you linked to, one would detect whether the device is a phone or a tablet, and then set the visibility of the two fragments in different situations accordingly.
You will find a number of methods for detecting screen size here, whether you want to use screen size in inches, pixels, or the manufacturers' default categories.
And properties like visibility and sizing can be set programmatically using Layout Params and its various subclasses.
In any particular case, whether you choose to use multiple layouts to support different screen sizes or to do more of it programmatically is up to you. Personally, I think that it is always a good practice to design your code modularly and then use layouts to put all the pieces together (it'll save you a lot of headaches down the line if you decide you want to change things up). But either way, supporting more devices will always require more code, and there are no two ways around that...one of the curses of android development :P

Categories

Resources