Easy way to go with Android Support Library? - android

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.

Related

Android Studio inconsistent with generating fragments vs. support fragments

Using Android Studio on a project with the minimum SDK set to API 15, if I use the UI to generate a new "Blank Activity with Fragment" (or any other activity template that uses fragments), the generated fragment will inherit from android.support.v4.app.Fragment. But if I use the UI to generate a new "Blank Fragment," it inherits from the native android.app.Fragment. What logic does the IDE use to select which one to inherit from? Is there any way to force it to always use one or the other, so I don't have to go about changing the imports?
You can not change the default Android Fragment class creation in the IDE and I do not think this is a huge thing.
But if you are really worried about this, :) you can create your own custom File Template for Fragment in Android Studio and reuse it all the time or Gist it and keep it.

Add Action Bar without Subclassing ActionBarActivity

Is it possible to add an Action Bar to an android application:
1) without subclassing ActionBarActivity
2) support for gingerbread and newer
I've searched google and SO, no results.
The reason I ask this is because I have an activity that already subclasses from another library, and I can't make the ActionBarActivity the root subclass.
Normally no, multiple inheritance isn't part of Java.
Of course, the real question is if ActionBarActivity will actually be useful on Gingerbread. It depends on what specific functionality you need from it.
What you can try to do:
Make your own "ActionBar" via layout.
If the library you're using is open source, modify it so its Activities extend ActionBarActivity instead.
If not, both ActionBarCompat is open source - you can download the source and incorporate the functionality into your Activity. ActionBarActivity does extend FragmentActivity, so you may need to work with the raw support-library source as well.
I know the answer for Q2 is YES. You use support library v7 to support Action Bars on devices running GingerBread (Eclairs and Froyos as well).
For Q1, i believe the answer is an YES. You just use the Window.requestFeature() to add Action Bars. But i am not very sure about this.
HTH.

Problems implementing tabs in ActionBarSherlock 4

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.

Customizing a project library in Android

I'm using this library project in my Android application. But I need to make some customization for it.
For example, if I need a EditText instead of the provided TextView, what is the best practice to customize the library for my needs without writing code in the Project Library?
I made this customization by defining in the library's actionbar.xml layout a EditText instead of TextView, but I don't like this approach.
Do you have any guidelines, tutorials that could help me out?
In your special case I would suggest to use original compatibility's-package actionBar.
But to answer your question: you could always extend classes from the Library, which I think is the best practice if the library should kept untouched. Overriding Methods which you want to change their behavior keeps anything clean. You do the exact same every time you extend android-sdk classes, which you cannot change like you want.
In the case of changing layouts I'm not quite shure. I think I can remember that if the lib has an actionbar.xml and you have an actionbar.xml inside your project, too, yours will win. just like an "overriding layouts" feature

ViewPager can replace ActivityGroup?

I am doing an application similar to Viber or Linphone, a SIP application basically. I have a main tabhost at the bottom of the screen. And inside each tab I set different activities(for example the historic). I have done this using ActivityGroup. Is that the right approach? As I have seen that ActivityGroup is deprecated, but I dont want to use fragments(most of phones dont support API 11). I have seen the ViewPager component, could be also an alternative? I guess I can change the event onFlip with my finger for the onClick on a specific botton. Am I doing it right using Activitygroup, or should I change to another component?
Usually the kind of thing you are describing can be done using a simple TabActivity.
I'm not sure why you decided to use ActivityGroup, but regarding your hesitation related to fragments - you can use the Compatibility library, which will give you fragment support for older platforms, albeit with some limitations.

Categories

Resources