Android - how to check object is existed in layout? - android

I want to know how to chech whether object is existed in layout.
To make clear, I want to check ListView is existed in layout for different screen resolution. If client device is tablet, I'll process for ListView and will make some action. If client device is mobile, I'll ignore this ListView and set visible mode to GONE.

You should define the default (say, phone) layout in /res/layout and the tablet layout in /res/layout-w720dp or something similar. See here for more information on qualifiers for layouts. Then you have defined your different layouting in xml and don't need to jump through hoops in code to make it fit to different screen types.

To know whether object exists or not, just try to find it with findViewById and compare result to null

Disclaimer: This is a link to post on my blog (there is no advertising). I think it might help you think about the way you construct your app to deal with phones vs tablets.
http://damianflannery.wordpress.com/2011/10/16/architecting-a-single-apk-app-to-handle-phones-and-tablets-on-android/

It sounds like your intention is to hide a listView from the screen if the users device is not a tablet. This is easily done using different layouts for different as mentioned in Damians blog post.
However the new correct way of doing this is to use fragments. The basic idea is you combine the code and layout into a fragment and only use the fragment if there is space on the screen. This means that your screen would be formed of 1 fragment if phone and two if tablet, one of which would contain and code for your listView.
Fragments - Android Developer

Related

Reuse layouts and fragments

Lets say that in my app i have 2 screens with exactly the same views, the only thing that changes are the texts.
Whats the best way to be more efficient here?
Using the same layout with the same fragment and same VM? And handle
all the texts logic in the VM
Use the same layout but with different fragment and different VM?
For me, if the screens are 100% the same but the only thing that changes are the texts i would go with the first option but im not sure thats the correct way to do it.
I would say go with option 1 if everything is same other than string changes. You can have a flag and based on that flag you can update strings.

When use Android fragments?

I have to write a simple app which will look different depending on the screen size.
On a small screen it would have a list of items and after click it will display details. On a bigger screen it would have a list of items and details next to it.
My question is: should I use fragments to do such an app or just write different layouts and place them in layout-large or layout-land folders?
If it is a new app I would always go with fragments, basically for future proofing your app. You may have a single list now, but later you may want to introduce a menu for selecting different types of lists for example. By using fragments, on a phone you can use two activities, one for selection, and the other for the list, but on a tablet in landscape mode you can place the selection and list side-by-side.
Coding for possible app enhancements and features now, saves you time and headaches later.
You can do it in both way , but the suggested solution is using fragment
check this out :
https://developer.android.com/training/basics/fragments/fragment-ui.html
and this :
http://www.vogella.com/tutorials/AndroidFragments/article.html
You should use Fragments, since Fragments were created for that purpose, besides, if you make 2 layouts, you will have to do 2 completely diferent layouts and that'll mean that the activity would not handle that, at least not in an easy way, so try fragments to achieve that, the official documentation example is a case like yours, that would be enough to believe that the correct way to do it is with fragments.

How and when to use Fragments in applications?

I am very in doubt of the structuring of my application. When to use Fragments is a big question for me still. I understand the concept of fragment, but I would like to know how more experienced programmers use them. Is it really only when there is a specific task the fragment should do?
For instance, POP-UPs, what layout would be better used?
And back to the fragments, do you usually have a skelleton fragment that can be used for more than one thing, and then reshape it to different final forms (minor changes) or would you just use different fragments if the layout changes?
You should use fragments in a few situations but its going to depend on your app really.
If you have a layout that is going to be used in multiple places and the code is relatively the same, that is a very good candidate for a fragment so you can keep code down.
If you are making an app where the layout changes based on orientation or device type (tablet vs phone) then fragments are highly recommended to hold different layouts. It makes it easier to change or show multiple layouts if need be on a tablet.
I'm sure there are more scenarios but I would say these are the basics for fragment decision
The most common way I use them is as a kind of plugin layout for my activities. Let's say I have several activities and all of them need to display an address, phone number and some buttons (call number, launch navigation/gps, etc). I pass the fragment the id and the fragment does the legwork of grabbing the information and populating fields for my activities. This way, if I need to add anything, say an email address, I just need to modify one section of code rather than each activity and layout file.

Handle different layout and different orientations

I have an application I want to adapt to tablets and handheld devices
When handheld device is in portrait or landscape I want to display ListView and when list item is selected I start another activity.
When tablet device is in portrait - see above.
When tablet device is in landscape I want to display ListView in one part of the screen and selected item content in another part of the screen. Basically something like this:
How should I achieve that? I was thinking different activities and layout resources for each orientation. But I have a feeling that there is a smarter and better way.
Of course I could stuff everything in one activity somehow but it doesn't feel right. Nor looks nice :)
Thank you
What you want to achieve can be done using Fragments. Here is an example depicting the use of Fragments.
To communicate between fragments see this link.

Enormous form: how can I do it in Android?

I'm developing an Android Tablet application with Android 3.1 SDK.
I have to implement a form in an Android Tablet. Now I'm doing on one screen with TableLayout, TableRow, TextView, Spinner, buttons, etc.
At this moment I have more than 80 views and then I get a warning about it.
The form is divided into sections and I think I can divide it into tabs but I don't know if I will have the same problem (I'm very new on Android development and now I learning how tabs work).
What do you recommend me? I will have, probably, 160 view or more.
I recommend that you split this huge form into multiple screens / steps somehow, it seems much more useable and managable to me. You could use fragments to hold the steps, and use some paging mechanism to navigate between these fragments. By switching fragments and saving their state you can keep the number of Views on the screen relatively low.
Check out the ViewPager component for this to navigate between fragments by swiping. Or you may use the plain old button based navigation (next/previous step e.g.).
If you really need to display all the form elements on one screen and want to keep the number of instantiated Views, you may be able to do this by using the virtualizing ListView, though it seems quite awkward to me. ListView constructs the rows as needed during scrolling, and you need to tell its adapter that you have X type of rows where X is the number of form-parts.
Why don't you logically break this Enormous form and using something like a Next button show the form in multiple activities. This would keep the screen clean, won't bombard the user with too much of information and finally won't give the warning of excess views on screen.

Categories

Resources