Problems implementing tabs in ActionBarSherlock 4 - android

I really want to get this to work, but I feel like I've been floundering about for hours. I'm starting up a new app and want to try to follow the UI guidelines as close as possible, which for anything below ice-cream sandwich seems to mean I need to use ActionBarSherlock. Looking at the example here it looks like it should be straightforward.
I try to implement it and it doesn't work because FragmentActivity does not have a getSupportActionBar() method.
Taking a look at the demo app and it seems they don't use FragmentActivity, they use SherlockActivity. Well, SherlockActivity does have a getSupportActionBar() method, so that's promising. Continuing on with the example for a while...
Okay, now it seems the com.actionbarsherlock.app.ActionBar.TabListener doesn't pass FragmentTransaction to its events? Not only that, but the SherlockActivity doesn't have a getSupportFragmentManager() method...
Am I (hopefully) just doing something completely wrong? How do I keep encountering all these issues that no one else is? I feel like I'm going nuts here. The only potentially related issue I've found is here and it's not very useful.

You need to extend from SherlockFragmentActivity instead of only SherlockActivity or SherlockFragment. This way you will get access to ABS and the Fragment Support.

I ran into a similar problem trying to get a map view and fragments to work with actionbarsherlock. See my post on the mailing list.
The solution I came up with was to get the sources for the compatibility library and modify the FragmentActivity to extend a SherlockActivity instead of a standard android Activity.
Then you'll need to compile the sources for the support library along with your app(in some fashion).
In my case I went an additional step to modify the SherlockActivity to extend MapActivity, but since you didn't mention maps, you wouldn't need to do that.
There may be other solutions, but this at least got things to work for me.
I would recommend the mailing list as this project seems to be changing frequently.

Try to use extend from com.actionbarsherlock.app.SherlockFragmentActivity and take a look at https://github.com/inazaruk/examples/tree/master/MapFragmentExample to see how to use a Map with Fragments.

Related

How can I replace/customize an Android API class?

I couldn't find any guidance on that yet.
I want to make a small change on an Android API class (ViewPager) and import it to my project. I want to customize a parameter of a method that is not public.
I tried to extend this class and override the method, but since the it is protected I cannot access super.method() from my package, so I don't know how to proceed.
I decompiled it in Android Studio, tried to copy and paste the class, but it wouldn't work. I'm clueless right now.
How can I achieve it?
This is what I tried:
You can find the source of ViewPager on the AOSP project: https://android.googlesource.com/platform/frameworks/support/+/android-5.1.1_r2/v4/java/android/support/v4/view/ViewPager.java and copy it into your project. You can then modify it and extend it as you need.
However, you will not get the benefit of updates that come from new AOSP versions of ViewPager if you follow this course. I highly recommend finding another way to accomplish your goal without duplicating ViewPager like this.
The possibilities you have:
Extend the class and override a public or protected method.
Copy and paste the whole class from the Android source code, make the needed changes.
Change the behavior using reflection.
Regarding the velocity of the ViewPager: check the following SO questions:
Slowing speed of Viewpager controller in android
Android: velocity-based ViewPager scrolling

other alternatives to sliding tabs

seems that starting Lollipop the ActionBar "setNavigationMode" method is deprecated. i know that google now offers to use sliding tabs and view pager however I am unable to understand the code completely. is there any alternative that I can use? if there isnt then can someone suggest for a good tutorial on that?
I need to achieve two tabs when each one holds a fragment.
I plan on using the SlidingTabsLayout technique instead of the new ActionBarCompat or Toolbar. It seems you don’t like it when reading from your original post. However there is sample working code at SlidingTabsBasic.
If you download the zip file, you may have to import the project instead of opening it. When you import, find the build.gradle file of the project. So far it seems this is not much difference than the link you posted at Github https://gist.github.com/eluleci/199be9a0d1af42653b5b.
I predict when the new Toolbar is stable, I can integrate some of this code like the ViewPager and PageAdapter.
I am thinking of the same thing since I want to support the new Lollipop. There is a Stackoverflow link at Action bar navigation modes are deprecated in Android L
Let me know what you found or decided. Perhaps we can work together. Have fun!

Easy way to go with Android Support Library?

I've got a project written for API 11 and up, no I need it available for 2.3.3 OS devices either, so need to use Support Library. What is the easiest way to go with it? I know some methods have different names for Fragments, for instance. Can it be possible to use "Find and Replace All"?
For the most part, yes it will be possible. You'll also have to change less than you think, for example the Fragment class is fine.
You will have to change getFragmentManager to getSupportFragmentManager, as well as changing your base Activity to a FragmentActivity. Those are the most common two off the top of my head.
The easiest way is to change the target to 2.2, add the support-package as a library, and see what breaks and then fix it-- and once you have a replacement fix you can use find and replace all to fix all instances of the issue.
Can it be possible to use "Find and Replace All"?
As far as I'm concerned, it's never a good idea to do a simple "Find and Replace All"... it's far too easy to make a mistake.
What is the easiest way to make the transition?
The change is very simple and should not require you to change your build target at all (assuming you are targeting Android 1.6 or above, of course). The key changes you must make are:
For each Activity that makes use of the support libraries, have it extend FragmentActivity instead.
In each FragmentActivity, replace all calls to getLoaderManager() with getSupportLoaderManager().
In each FragmentActivity, replace all calls to getFragmentManager() to getSupportFragmentManager().
Replace all relevant android.* imports with their corresponding support.v4.* import statements.

My first ever Android code with Fragment

As an exercise, I am trying to rewrite the following google tutorial with Fragment class. The original tutorial implements tabs by using the old TabActivity class and TabHost/TabWidget annotation.
Tab Layout Google Tutorial
I have converted all Activity class with Fragment. I couldn't make my new code to work. I think I am stuck.I could not find any 'complete' Tab sample code using Fragment class.
Here are my questions
1. Should I define in the res/layout/main.xml or calling Actionbar.addTab(...) in my entry class, or both?
2. What would be complete res/layout/main.xml looks like? What would be the root element (i.e. LinearLayout, FrameLayout...etc)?
3. Any additional info would be greatly appreciated.
Check out this example from the compatibility library demos: FragmentTabs.java
and the corresponding layout: fragment_tabs.xml
Really, though, I wouldn't start with Tabs if you're trying out Fragments for the first time. Tabs in Android are a little bit of a mess. The above example (from Google itself) uses a hack just to get things working. Tabs just add a layer of unnecessary confusion when you're just learning.
Here's a more straightforward starting-out Fragments example/tutorial: http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html
(Just make sure to replace things like getFragmentManager() with getSupportFragmentManager() if you're using the compatibility library.)

Get a specific Activity from TabActivity or TabHost

How can I get a specific Activity from TabActivity or TabHost?
I tried TabHost.getChildAt(), but a View instance can't be casted to an Activity instance.
Or how do you get an Activity which is running in background?
I couldn't find TabActivity.getLocalActivityManager().getActivity().
For me it worked using TabActivity.getLocalActivityManager().getCurrentActivity().
found the answer: just use TabActivity.getLocalActivityManager().getActivity()
I know this isn't what you're looking for, but I HIGHLY recommend not using TabActivity. In all the projects I've worked on, we wound up yanking it out and just using buttons that look like tabs.
I also think its deprecated in 3.0, and certainly Ice cream. They push the action bar. I'd look for one of the available action bar libraries.

Categories

Resources