Android Tabs + Sliding, how do I implement ActionBarSherlock? - java

I'm using Google's new tools to start an application that has the ability to switch between three tabs.
This is great, but lacks support for older devices.
1. I added ABS with the support library to the application.
2. I changed public class MainActivity extends FragmentActivity implements ActionBar.TabListener { to public class MainActivity extends SherlockFragmentActivity implements ActionBar.TabListener {
3. I'm still left with a ton of errors, and I don't even know if this will work properly on older devices. Does anyone have any tips on how to implement sliding tabs that are compatible with 2.x and up?
Update:
I'm stuck on step 6 of alextsc's answer

I tried this wizard once and I think I threw away the generated code completely when I implemented this exact pattern with ActionBarSherlock, so I suggest you start with a normal "Blank" activity from scratch. Here is a small step-by-step guide. Not all steps are completely described, but you should find enough documentation with the keywords to get started.
1) Add ActionBarSherlock to your project (obviously)
2) Create a new activity that extends SherlockFragmentActivity and set a proper abs theme
You should have a blank activity with an action bar at this point.
3) Change the layout and include a ViewPager that fills the viewport
4) Write your fragments (or placeholders for now) and the adapter for the ViewPager, wire these together
There are a lot of tutorials out there that explain everything neccessary here, e.g. this blog post.
This should give you an activity with an action bar and a swipable layout. You can swipe between your fragments now.
5) Add action bar tabs and attach a blank tab listener to them
Example:
actionBar = getSupportActionBar();
sampleTab = actionBar.newTab()
.setText(R.string.title)
.setTag(TABTAG_SAMPLE)
.setTabListener(tabListener);
actionBar.addTab(sampleTab);
Make sure that you give each of your tabs an individual tag (a string const is fine). This will be used to identify which tab is clicked in a second. Also make sure that you keep the created tab instances in a class variable. You will need them later on. Repeat the above snippet for each tab. You can use a normal TabListener, but I recomment using the SimpleTabListener since you only need to override one method later.
Now you should have an activity with the action bar, swipeable fragments and (non-functional) tabs.
6) Fill the tab listener and connect it to the viewpager
private SimpleTabListener tabListener = new SimpleTabListener() {
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
final String tag = (String) tab.getTag();
if (TABTAG_SAMPLE.equals(tag)) {
viewPager.setCurrentItem(INDEX_SAMPLE);
} else if (TABTAG_SECONDTAB.equals(tag)) {
viewPager.setCurrentItem(INDEX_SECONDFRAGMENT);
}
}
};
This should be straightforward. You listen for a tab select event, check which tab is selected via the saved tag and call the viewpagers setCurrentItem() method with the index of the fragment that is associated with a certain tab.
Now you should be able to select a fragment via a tab as well as swipe around. You will note that swiping to a certain fragment wont sync the tabs accordingly, they wont get selected properly yet.
7) Attach an OnPageChangeListener to your ViewPager and select tabs accordingly
Again, you can use a SimpleOnPageChangeListener here as well instead of the interface. Short example:
private SimpleOnPageChangeListener onPageChangeListener
= new SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
switch (position) {
case INDEX_SAMPLE:
actionBar.selectTab(sampleTab);
break;
case INDEX_SECONDFRAGMENT:
actionBar.selectTab(secondTab);
break;
}
};
};
This should also be self-explanatory. You watch for a swipe action that changes the displayed fragment, check it's index and select the appropriate tab. You see here why you had to keep the tab instances from step 5 around, you need them to select a tab.
Everything should work now.

The exact same process worked for me. I removed all the imports and then pressed ctrl+shift+o and selected the compatibility classes. It worked absoulutely fine. see the post here.

I would recommend taking a look at ViewPagerIndicator it works with ActionBarSherlock and the compatibility library, I currently have an app using this for swiping tabs on 2.2 and up.

I have implemented very complicated application using actionbar sherlock. It works fine on 2.2 to ICS devices I have tested. I also used tabs and everything works fine. I changed it later to navigation list because there should be 6 tabs.
Try demos included with actionbar to start with.
This errors are probably some classpath problems. You need to add sherlock as lib project and
support library must be included in both sherlock lib project and your project. Also check if both support libraries must have same versions.

I cannot add comments so this is my comment to ViewPagerIndicator. I used to it but when I started to use loaders to load data and fragmentadapter sometimes there were weird issues when new data came, orientation changed... But to use tabs with actionbarsherlock ViewPagerIndicator is not necessary.

Related

How to add / remove tabs from inside a fragment (AppCompat)

I am having a really tough time with one small issue in my AppCompat activity with tabs. I am using the Android Design Support Library, and have implemented a tabbed application with fragments.
Now, I have no problem creating tabs & fragments in the Activity's onCreate() method, but I cannot for the life of me find if it is possible to add tabs programmatically from within a fragment.
For reference, all of my tabs use the same fragment (OneFragment.java). I have tried using FragmentManager / FragmentTransaction, but although this creates a fragment (I think!), it does not make a tab.
I have also tried to add a tab to the FragmentPagerAdapter and set the Tab Layout's adapter again, but this also seems to do nothing in the UI. Any help would be much appreciated!
I have also tried to add a tab to the FragmentPagerAdapter and set the Tab Layout's adapter again, but this also seems to do nothing in the UI
If you managed to add a tab to the FragmentPagerAdapter but the UI didn't change then you probably forgot to call the notifyDataSetChanged() method on it.

Android Development, Google Tutorial

I am following the Google tutorial for building your first android application. I got to the point where I needed to implement the actionbar actions with the functions openSearch() and openSettings().
I implemented all of this in the MainActivity.java file.
My question is this:
In the example app you can type a message and then send it and it displays it in a second activity. In the second activity, the top action bar changes and does not display my Search icon or perform the action when the settings button is clicked. In order to have these icons displayed in the action bar for this activity as well, do I need to add those methods and update onOptionsItemSelected method in DisplayMessageActivity.java as well as in MainActivity.java? Is this the only way to carry the action bar icons/actions over? To retype the same methods in each activity that you want them in? Or is there a better way to do it?
My other somewhat related curiosity is this. The method openSettings() is called when I click the 3 vertical dots and then settings. These 3 vertical dots show up on every activity, and settings is always in the list. However clicking settings obviously doesn't perform the call to openSettings() when in the DisplayMessageActivity and not MainActivity. How is it that settings and the vertical dots are carried over?
Second to last, how can I add other selections to the drop down list from the options/vertical dots in the action bar? Settings is always there although it responds differently in each activity which was my first question. But I would like to add certain things to the options menu that are on all activities, and some things that are unique to some activities. I assume there must be a better way than repeating switch statements and methods in every Activity.java file.
And finally, what is the best practice to implement an action bar over multiple activities?
Obviously different activities will often have different icons/actions in the action bar, however some things like the 3 vertical dots(options) and settings within that would obviously be acceptable to have in every Activity, while it would be nice to add other things to the options list I don't see why settings should ever change across activities. Yet as I stated before the method is not called in DisplayMessageActivity unless I repeat the code in DisplayMessageActivity.java that I had added to MainActivity.java. I'm confused as to where I can add these so that they are displayed on all activities without repeating code. And I'm confused as to how the actionbar's options/vertical dots are carried over to all activities while others require the repeating of code in each activities' java file that I want them to show up in.
I know this was a bit of a long winded quesiton, I will clarify if necessary. I'm just a bit confused. I was able to make it through the tutorial fine as I have a decent understanding of java. However google's guide isn't written that well and the Android environment is very confusing to a beginner.
I do understand how things work to a degree, I just want to ensure that I'm actually doing it in a way that when my app grows in complexity it won't be a mess of unnecessarily repeated statements and methods.
Thanks in advance for any assistance and tips.
In order to have these icons displayed in the action bar for this activity as well, do I need to add those methods and update onOptionsItemSelected method in DisplayMessageActivity.java as well as in MainActivity.java? Is this the only way to carry the action bar icons/actions over? To retype the same methods in each activity that you want them in? Or is there a better way to do it?
That is certainly one solution, but as you obviously know, it's not a very good one. There are at least two alternative solutions:
Create a MenuActivity class which implements all the logic for common menu items and then extend this class from all of your activities, rather than extending the standard Activity class.
Use fragments to implement your UI. Fragments are similar to activities in that they create UI elements from an XML layout. One difference is that they live inside a "host activity". In this particular case, the host activity will provide the common menu functionality and each fragment can customize it further depending on your needs.
How is it that settings and the vertical dots are carried over?
Most likely your DisplayMessageActivity overrides onCreateOptionsMenu() and inflates a menu XML layout which was created by Android Studio (or Eclipse?) when you created the activity class.

Implementing Instagram like in-app navigation system on Android

I have to implement a navigation system similar to the one used in the Instagram Android client.
There should be a permanent tabbar on the bottom of the screen all the time.
When the user navigates deeper within one of these tabs, lets say to a detail view, then switches to another tab, and then switches back to the previous tab, the last shown (deeper) detail view should be shown, and on back presses, it should be iterating back till the main view of the said tab.
What I have came up with so far is the following:
I have a MainAcitvity showing the menu on the bottom.
On selecting a menu point, the appropriet Fragment is shown.
When the user navigates further within a Fragment, it then asks the MainActivity to change its content by the given criterias, resulting in changing the Fragment shown.
I add all the Fragment changes to the backStack, by calling the FragmentTransaction's addToBackStack() method.
I am stuck at this point, and cannot figure out how to switch fragments on back presses, and how to handle tab navigations when deeper views are shown instead the main views of the tabs.
I am thinking of using my own separate "backstack implementations" for every tab. When the user navigates deeper within a tab, i generate a unique "tag" and use that tag when calling addToBackStack() and also putting the tag in the "backStack" implemented by me. In case the user navigates again to this tab, i can check if i have any tags in the "backStack" for that tab, and if so, then look up that entry in the real backStack in the fragmentManager of the MainActivity, and switch to it.
I could not come up with anything better yet. Is there any better/simpler way to attchieve the said behaviour? Am i missing something? (I know this is really bad application design in the Android world, but it is another question)
I am posting an answer since the question is pretty dead, yet the conclusion might be helpful for others.
We ended up sticking with the old fashioned NavgationDrawer pattern, which worked well. But in the meantime I had to implement a library project which provided a Fragment to the hosting Application which had its own custom logic. This Fragment then used its ChildFragmentManager, to add another Fragments inside itself. ChildFragmentManager is ported back in the Android Support v4 lib, so you can use it basicly everywhere.
So lets say you want x Menu points in which you can navigate deeper. Those will be the Fragments using their own ChildFragmentManagers to add other Fragments to navigate deeper within that Menu. ChildFragmentManagers have their own back stack, so you dont have to worry that much about handling states. If another Menu is selected, you can look up the corresponting Fragment in the MainActivitys FragmentManager, and change back to it, or add it if it has not yet been added.
Be careful, you have to implement back functionality yourself, since ChildFragmentManagers will not get the backPressed event automatically. You can do this by handling the onBackPressed event in your MainActivity.
#Override
public void onBackPressed() {
boolean handled = false;
if(getFragmentManager().getBackStackEntryCount() == 0){
// No menu added
return super.onBackPressed();
}
Fragment frag = getFragmentManager().getBackStackEntryAt(getFragmentManager().getBackStackEntryCount() - 1);
if (frag instanceof XMenuFragment && frag.isVisible()) {
FragmentManager childFm = frag.getChildFragmentManager();
if (childFm.getBackStackEntryCount() > 0) {
// pop the last menu sub-Fragment
childFm.popBackStack();
handled = true
}
}
if(!handled){
super.onBackPressed();
}
}
I used a different code, so it might contain errors, but i hope that the point of the concept is clear.

Architecture for a drawer layout app

I am building an app with a drawer layout similar to the Android Facebook app. I am wondering what the best method for architecture is. Should I have a main activity which is responsible for the action bar, and then have it use fragments to display the content of each menu item, or should I be using one activity to manage the action bar, and then have each menu item kick off entirely separate activities?
I could also imagine building multiple activities, which each have to manage the action bar. This option seems the worst.
You have two architecture options here
MainActivity with Fragments
ParentActivity that handles drawer and lots of Activities that extends this Activity.
I have tried both in different projects and found some things worth sharing.
For me The MainActivity that handles drawer and then using Fragments to fill the display is the best.
You will need to handle callbacks from specific Fragments in your MainActivity and redirect them to the specific Fragment they came from. This is mainly if you use Interfaces in objects lower in the Arcitecture chain since you sometimes need to pass down Activity to certain objects. This generates more code that are not as generic as one might want in top level architecture node.
If you are using a ParentActivity and extending it for each ChildActivity, you can write all specific code in the child, meaning that the toplevel ParentActivity will almost only have generic code.
If you are using the ParentActivity with ChildActivities and you are switching between Activities, you fill get the graphic when an Activity closes and the next opens every time a user switches between navigation objects. If you use Fragments this wont happen as the Fragment will be switched in the background. The user will also experience that the navigation drawer will be closed and recreated each time he clicks on an item there.
Its also unnessecary to recreate the navigation drawer with each click on an item. This is a minus for the ParentActivity approach.
With the ParentActivity approach you will also have to keep track of how the backbutton should function, this will be autoaticly handled for you with Fragments. Also when starting new Activities you have to choose if a new Activity should be created or if the old should be killed etc.
Just my 5c, hopes it helps :)
The best way is to use one Activity with one Fragment per section/view.
Take a look at the design documentation.
Also see the Tutorial and Sample Application. It's fairly straight-forward.
You will have one activity which manages ActionBar, Drawer (ListView!) and Fragment.
Every time it clicks an item in the ListView it updates the fragment with the new view.
If you use different Activities then you should use intents with a very bad effect, use a different activity only if needed (if it's totally unrelated to the current activity maybe?)
Official documentation: http://developer.android.com/training/implementing-navigation/nav-drawer.html
If you got any problem in creating this, online you can found more tutorials but the official is very great.
You should have the activity holding the actionbar & drawer
When using a drawer you should not start new activities from within the drawer but fragment instead
Good post & video about it: https://plus.google.com/u/0/+RomanNurik/posts/3nMVVQzUTjG
another good read: http://www.androiduipatterns.com/2013/05/the-new-navigation-drawer-pattern.html
And finally this is a must see also (check the slides or the video): https://plus.google.com/u/0/+NickButcher/posts/1jeyV2n1ZpM

Action Bar as Fragment

I have just started developing an Android app and have no experience at all. I have read a lot about Activities / Fragments / Widgets, but don't seem to find a clear answer to my question which is:
Can I create the Action Bar for the app as a fragment so whenever I change an activity I will simply call the one action bar (i.e. the one fragment)? I intend to develop a dynamic UI to create fragments for individual option and thought that it would be easy to have a general Action Bar appearing on all pages.
When you want to customize the ActionBar in all your Activities, the first step is to create a custom Theme in XML.
In this theme, you can customize nearly everything
Please refer to this excellent blog post: http://android-developers.blogspot.be/2011/04/customizing-action-bar.html
Using a Fragment for the ActionBar would be crazy!
If you want to add some code programatically in all your Activities, simply extends a custom Activity, for instance MyCustomActivity, that extends Activity.
public class MyCustomActivityextends Activity{
In this class, you can use getActionBar() and tweak it according to your needs
If you wanna have one ActionBar for all your Activities use inheritance. Create an Activity which simply handles the ActionBar like you wanna have and make it the Superclass like this.
public class ActionBarActivity extends Activity{
public void onCreate(... ) {
ActionBar actionBar = getActionBar();
// + some other method calls of your choice
}
public onCreateOptionsMenu(Menu menu){
// create your actionbaritems here
}
public boolean onOptionsItemSelected(MenuItem item) {
// handle your click events for the items here
}
}
Now you can use this Activity for all your Activities with inheritance:
public class MyActivity extends ActionBarActivity{
...
}
With this setup you are free to use Fragments as you like.
Keep in mind that every time you call a new Activity the callbacks of the super class will be invoked.
I think what you're thinking of is this:
You want your action bar to be the same on every screen, and only need to program it once.
The approach I use, is that the actionbar live in a root activity, containing a single viewpager. And all of the screens that the user interacts with are Fragments in that view pager.
If you create a blank android project in eclipse, and select actiobar with tabs, the project will set this up for you and you can see how that works.
In most cases this would be an unnecessary complication. Once you get started, you will probably find that just a few simple lines of code will create an "identical" Action Bar in each activity .Themes etc can be added later. Better questions at this stage might be should I use Action Bar Sherlock to enable better support of old devices ? and you should think about the overall structure of your app e.g activities / fragment activities / fragments / tabs so that you are able to get something working quickly that can be readilly extended as you develop you full solution.

Categories

Resources