I've been slowly porting my iOS App to Android so I can take my time and make sure that it functions correctly and I'm now at the part where I need to create a TabBarController like environment. I've been searching for days and have come across numerous examples but with each example there's a problem.
First, most examples refer to using a TabActivity, which when I just checked is deprecated and can't be used. The one's that don't use TabActivity either use Fragments (sometimes with an Action Bar) or a Tab Host. It looks like Fragments is the way to go, but then how to you support older devices (from the Android website over 50% of there users are still on API 10).
Basically what I need to set up is a Tabbed environment that will let each tab load a new Activity, and then each of those Activities will have their own navigation based hierarchy as well so I'm trying to see what is the best option to pursue and if anyone has seen some examples that can help.
I've found 4 tutorials so far that have fragments and different activities for each Tab, so I'm going to look into those now but just wanted to see if any other developers that have already come across and issue like this did to work with the compatibility issues with older devices.
You can use Fragments all the way down to API 4 using the Support Library. I'd use a single Activity with multiple Fragments in ActionBar tabs, though you might need to do it with TabHost to support lower APIs.
Related
I'm trying to port an iOS app to Android. I'm an Android noob, but I'm quite experienced in iOS. Here is a screenshot of my app.
The UITableView has custom cell types: Switch cells, tap to toggle the switch on and off. Multi-value cells: tap to expand a UIPickerView. My iOS app also has a tab bar for more screens.
So I started with the default TabLayout template in Android Studio, and made it to show three different fragments.
I then moved on to making the main interface. That was when the PreferenceFragment popped up in my searches, and seemed like a very good idea – my iOS app has a relatively complicated architecture to keep track of the state, and the PreferenceFragment promised to take care of that transparently, so I was in.
However, the TabLayout does not seem to like PreferenceFragments because they are not the same as compat fragments (did I get that right?). Anyway, the linked question suggests a couple of solutions that I'd rather not follow (re-invent the wheel? Seriously?).
Reading around, people suggest I might get away with a PreferenceActivity instead, but I really liked the idea of fragments, mostly because I want my app to scale to tablet and have a "different" UI, with the two different lists of parameters showing up side by side – a job for fragments, right?
Now, the best I can figure is to go back to RecyclerViews, so my question is: is that it? And if so, do RecyclerViews support simultaneous different types of cells like UITableViews do? I should also say that I don't particularly care for Android <5.0, if that helps…
Any other hints or pointers for me to follow when porting iOS apps to Android?
The reason TabLayout does not seem to like a PreferenceFragment is because PreferenceFragment extends android.app.Fragment not android.support.v4.app.Fragment (you did get that right), which the TabLayout (in reality, the FragmentPagerAdapter) expects.
If you want the functionality of a PreferenceFragment to be paired with other app-compat widgets, import the v7 Preference Support Library:
compile 'com.android.support:preference-v7:24.2.1'
Then you can extend android.support.v7.preference.PreferenceFragmentCompat, which itself extends from android.support.v4.app.Fragment.
I'm a long-time Windows developer who is now doing Android development.
I would like to use the exact functionality contained in the following officially documented example on tabs and swipe:
http://developer.android.com/training/implementing-navigation/lateral.html
Deprecated Functionality
However, the downloadable example contains methods & functionality which is deprecated.
I'm confused about why the Android official example which shows how to handle tabs and swiping ends up including code which is deprecated.
Is it not the most recent version?
What Functionality Am I Looking For?
I'll show you two screen shots which display tabs and allow the user to select the tabs to navigate among them or swipe left or right to move through them.
Looks like the following:
I want the user to be able to easily switch between the two tabs and I have a plan to indicate to the user that information on the other tab has been updated by displaying an icon on the current tab.
My Main Question
Is there a similar example which does not include these deprecated
methods?
Related Questions
If I choose to implement the code in my application -- because the solution is quite beautiful and looks easy to implement -- what will the consequences be?
Does it mean that some time in the future the application wouldn't even work properly?
Does it mean that it may not work on some devices (API versions)?
Here's some of the code which is deprecated:
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
actionBar.setSelectedNavigationItem(position);
actionBar.addTab();
actionBar.newTab();
It seems that the deprecated functionality is actually related to the ActionBar.
After quite a bit of searching, I found a better example of what I wanted -- sans ActionBar and sans deprecated functionality :
http://developer.android.com/samples/SlidingTabsBasic/index.html
When you get to that page, you will see that there is no documentation for the subject. It's odd. Look very closely in the upper right corner and you'll see a download link which will allow you to download a sample. It looks like:
The sliding tab solution is even more elegant and does not include any deprecated functionality.
Here's what it looks like:
The example is fantastic, because you can traverse through the tabs by swiping the bottom panel. Or you can click any of the tabs to move to the associated tab.
Also, the example gives you some more fucntionality above the tabs with the Show Log menu item which switches out the top portion.
Definitely worth a look and this answers my question.
Hope this helps someone else who is looking.
I'm confused about why the Android official example which shows how to handle tabs and swiping ends up including code which is deprecated.
Not all documentation gets updated. There has been stuff that has been out of date for years on the site, and despite filing issues, it does not get fixed. This is not unique to Android; it is reasonably commonplace in software development.
Is it not the most recent version?
It is the most recent version.
Is there a similar example which does not include these deprecated methods?
I do not know what qualifies as an "example". There are many, many implementations of tabs for ViewPager, both from Google (TabLayout) and from third parties (about half of the entries in the "View Pagers" category in the Android Arsenal). Some of those third-party libraries have sample projects as part of their repositories.
Does it mean that some time in the future the application wouldn't even work properly?
Quite possibly, particularly depending upon your definition of "work properly". We have no means of knowing exactly what will happen in the future (as we used up our last flux capacitor a week ago). Deprecated stuff usually keeps working, but it is at somewhat greater risk of bugs simply due to no longer being aggressively maintained. In the case of action bar tabs, the appcompat-v7 action bar backport does not support them at all.
I'm having serious problems getting three layers of nested tabs to work in an app that runs from Android 2.1 up and looks like Android 4 (uses support library fragments).
The goal
App should have an ActionBar (works, currenly uses ActionBarSherlock)
3 fixed tabs on the main screen, that don't move into the ActionBar even if the screen is large enough. The second of these tabs contains...
About 4 tabs that were loaded from a server when the user logged in the first time. Each of these contains
About 10 swipable tabs (like in the Play Store) that were loaded from a server when the user logged in the first time. My idea here is to use ViewPagerIndicator, since that library is written by the same guy who ActionBarSherlock which should keep problems down to a minimum. But I'm open to ideas here). Each of these swipable tabs contains something that is currently a Fragment, but could be changed.
The Problem
When this was an Android 2 app, it simply used nested TabActivities, but these don't work with all the Android 4 stuff. I don't understand how to do this probably, especially the "you can't nest fragments" restriction is causing me headaches. Also, it seems that you can only use one FragmentManager per Activity, so my idea to have one in each of the second row tabs didn't work (All except for the first tab remained empty).
How to do this the right way?
(Please understand that "Use a different GUI design" is not an option since this is what the customer asked for and he won't reconsider)
PagerAdapter does not require Fragments as children. You can inflate/manage your own custom views in there. So you could continue to use nested TabActivities. Or, you could put Fragments at the top-level, and manage your own Views in the bottom-level ViewPager.
You could also, theoretically, use ViewFlipper if you want to keep the Fragments in the ViewPager. You'll have to write your own LinearLayout with Buttons as tabs, but this is easy. If you're looking for the Holo look, simply set the style to the ones found in ABS.
Another option is to use TabHost without using the TabActivity. You can even use it with Fragments. See the example here.
Also note: If you're looking for the Google Play style of ViewPagerIndicator, thats now inlcuded in the Support package: PagerTitleStrip.
I'd imagine that your best option is to use Fragments as the top-level, as this will help with memory consumption.
That said,
I must say that this sounds like a terrible UI pattern. Even worse, we are talking about a lot of inflated views in one Activity. You may run into memory issues here, depending on whats being shown. I suggest heavy usage of ViewStubs and recycling if you keep the ViewPager at the bottom.
Keep trying to push the client toward using the ActionBar spinner pattern for top-level (main 3 tabs), or even consider the fancy sliding drawer pattern. Perhaps that smooth animation could be enough to sway their opinion.
Refer them to the official design website, and show examples of popular navigation patterns like the ones found in Gmail, YouTube, Google+, Evernote, etc. I recently dealt with a client requesting the exact-same pattern you describe, and after weeks of pushing was able to convince them that more-than` 2 layers of tabs is unacceptable.
You can also show them my Wall of Shame Google+ page, highlighting bad design patterns used in popular Android apps: Android UI Anti-Patterns. :-)
I have been browsing the web for a while now and looking at different examples of how to implement a tab bar.
The problem I am having is deciding the correct approach to creating this feature. The option that seems most used is to have one main activity called MainActivity or something and using fragments as the tabs. This is all well and good and I have created an application like this fine.
The problem I am having is I am in two minds as to whether this is the right way to go about it as coming from an iOS development background it just doesn't seem right due to these tabs having different functions.
Any pointers would be great.
EDIT I have also looked into ActionBarSherlock but couldn't get to use it in a project as it kept coming up with errors and I couldn't find a thread that sorted the problems I was having out.
Disco
You should be using fragments, yes. There is an example of how to do it here: http://developer.android.com/guide/topics/ui/actionbar.html#Tabs
This "only" works with Tabs that are part of your Actionbar, though. Shouldn't be too much of a problem, because they usually should be part of your Actionbar. You can also manually move the tabs if you're not content with their location, or roll your own buttons that look like tabs.
Keep in mind that tabs in Android aren't quite the same beast as the tab bar is in iOS. They look similar and behave similar, but there are subtle conceptual differences in how they are supposed to be used. Don't use them for your basic app navigation, but rather for switching between similar "things".
If you want to employ this on device pre-3.0 though, you will have to use ActionBarSherlock. It does work fine and we're using it in several projects, so if you're having problems integrating it I suggest creating a new question for that, I'll be happy to help.
I've a requirement to show tabs in an app. I've actually never used TabHost etc before, and the docs suggest the whole thing is a mess. TabActivity has been deprecated. I can't easily use it anyway as I'm using ActionBarSherlock so I cannot inherit from it. Then of course as of ICS, I'd not use a TabHost anyway, I'd use tabs on the Action Bar. As my tabs are just text and are simple in nature I see three options:
Kick TabHost into working with ActionBarSherlock. It'll look pig ugly on ICS devices.
Use ActionBarSherlock and implement tabs, on the assumption it renders the ActionBar tabs in some way on versions of Android prior to v4 (I have no idea if it does, I suspect not)
Just use Views as "tabs" to jump between activities
I'm leaning towards the last option, but would appreciate clarification.
Use ActionBarSherlock and implement tabs, on the assumption it renders the ActionBar tabs in some way on versions of Android prior to v4 (I have no idea if it does, I suspect not)
It would appear that ActionBarSherlock supports tabs on all Android API levels that ActionBarSherlock itself supports.
For example, they demo it on an Android 2.3 environment:
And the FAQ talks about tab support "on pre-3.0 devices".
And the theming page lists theme attributes for styling tabs.
And the Google Group has all sorts of discussion on using tabs.