developer.android.com training actionbar - correct result? - android

I am currently following the tutorials on http://developer.android.com/training/index.html.
I have completed the first section and continued the second section on the same code.
In the tutorial the goal is to make an ActionBar.
I have chosen to support android 2.1 and above using the v7 appcompat library.
I assume it is installed correctly (and have followed this thread).
My question is, what is my result supposed to look like?
I have created 2 AVD's, one running API 18 and one running API 8 and am testing on my own phone (Xperia Ray running Cyanogenmod 9, which is API 15 I believe)
All 3 devices give different results.
The AVD running API 18 shows the app with an actionbar in both the main activity and a secondary activity. Pressing the logo in the Actionbar in the second activity returns to the parents activity.
The Xperia Ray phone (API 15) also shows the actionbar in both activities but does not return to the parent activity after being pressed. I suppose I still need to program that and is not standard in API 15?
The AVD running API 8 shows an actionbar in the main activity with the name of the app but shows no actionbar in the second activity.
This is the code I used, from the tutorial linked above:
public class MainActivity extends ActionBarActivity { ... }
and in the manifest in the < application > tag
android:theme="#style/Theme.AppCompat.Light"
Should this be enough to make an actionbar appear in lower versions of android or is it supposed to not show an actionbar besides the main activity in lower versions?
I'd like to fix this before continuing the tutorial.
If my question needs more clarifications, I'd be happy to provide it. All help is appreciated. Thanks!

I have no experience with AppCompat, but I recommend ActionBarSherlock appCompact, is very easy to use. Link: http://actionbarsherlock.com/
You can also customize the style with: http://jgilfelt.github.io/android-actionbarstylegenerator/
good luck ;)

I found what I did wrong.
Following the tutorial litterally, the second activity will not have an actionBar.
In each of the activities java file, the class must extend ActionbarActivity instead of Activity. (The tutorial only explicitly tells to change the main activity)
--> This makes an action bar appear in every activity.
To make the back or home button appear in all versions, I did it like this:
//Make sure we're running on HONEYCOMB or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB && Build.VERSION.SDK_INT >= 7 ) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true); //for older versions
//getSupportActionBar().setHomeButtonEnabled(true); //without arrow
}
The first if statement should already be there by default (I commented it out as it was left out some of the code examples in the tutorial. Leave it in!).
getSupportActionBar() needs to be used on older APIs (make sure the appcompat library is included correctly). Using getActionbar() on older APIs causes a Forced Close.
Using setHomeButtonEnabled() works too but is without the < icon in the action bar.
Thank you for the replies. If anyone has other suggestions or tips, they are always welcome. (are both if statements needed?)

Related

Can't use Holo themes with ActionBarActivity child (correct API chosen)

I'm going through the tutorial at developers.android.com and I had problems with styling the action bar. I use the newest SDK (the bundle with Eclipse).
Say, that in values-v14/styles.xml I have
<style name="MessageTheme" parent="#android:style/Theme.Holo.Light.DarkActionBar">
I've tried all the variations of that that I could find. Tried without DarkActionBar in values-v11 as well.
It compiles fine but when I open activity styled as such, app crashes and logcat says
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
Like I said, API is set correctly. The target one in project properties (API 19) and android:minSdkVersion="14" in the manifest (tried higher as well).
Now, my activity extends ActionBarActivity (that's how the file was generated). If I make it extend Activity instead, then Holo works fine. That's an answer I found, but I don't understand why that works. What exactly is the difference between ActionBarActivity and Activity that makes this works and is this some hack or is it supposed to be done this way?
Also, that works fine with my additional Activity. If I try to this with the main activity from the tutorial, it doesn't compile because 2 methods used there are undefined - getSupportActionBar and getSupportFragmentManager.
You are using a compatibility library, so to style a support actionBar you need your theme to be descendant of appCompat.
Try this:
<style name="Theme.whatever" parent="#style/Theme.AppCompat.Light.DarkActionBar">
If you are still a little lost, you can generate your theme with this tool: ActionBar style generator and take a look how it's done.
Edit:
Check this out, also: Styling the Action Bar
See "For Android 2.1 and higher"
About the difference between Activity and ActionBarActivity...
As far as I know, you extend ActionBarActiviy if you need to have an action bar while targeting lower than 3.0 android versions. That's why you are having troubles with actionBar or supportActionBar depending on what kind of activity you are coding.
So, to summarize, when working with Acivity call actionBar, but If you are extending ActionBarActivity you should call SupportActionBar. For instance: getSupportActionBar().
More info you could use: Support Library Features
Edit 2: Android is yelling at you because your are trying to use appCompat features. To avoid this in your particular instance, all you need to do is NOT extending ActionBarActivity, but coding regular Activities. Then use ActionBar features as normally you would do.
Edit 3 and probably last:
Let's guess you are using holo as theme, and you are coding a regular Acitivty for API 11 and above. In this case you are not extending ActionBarActiviy, so you don't have to do anything special. That's ok, right? but now, you want the same thing to work for API versions lower than 11 and here comes your real problem. To make that happen you must extend ActionBarActivity, but you didn't. So your only way out (as far as I know) is to have another activity that extends ActionBarActivity, and somehow detect with code, which version of android is running, in order to execute the right code (this is, which class you of the two you should take advantage of) so your app would be able to avoid crashing.
Thats why I think using only appComapt is a nice solution, assuming you don't really need to use holo. But, if you truly want to make things that way...
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
// Use a regular Activity class for API 11 and above.
}
else{
// Use an activity extending ActionBarActivity. Have in mind that here you would be calling a supportActionBar instead of a regular ActionBar.
}

setDisplayHomeAsUpEnabled in Compatibility Library

In my app I have a some fragments that I switch between manually in the phone version and I want to show the < arrow in the ActionBar. To do this I know I need to call actionBar.setDisplayHomeAsUpEnabled(true); but that breaks in lower api levels. I can check the api level and only call it in 3.0+ but LINT gives me an error. Is it alright to just suppress the error? What's the right way to do this?
First setup ActionBarSherlock for compatibility for Android 2.1+ for the ActionBar:
http://actionbarsherlock.com/
https://github.com/JakeWharton/ActionBarSherlock
One you have set up this great compatibility library, you can now use in your Fragment:
getSherlockActivity().getSupportActionBar().setDisplayShowTitleEnabled(true);
Make sure you extend your Fragments to SherlockFragments:
public class TestFragment extends SherlockFragment
If you need anymore help, let me know I have set this up many times!
Regards,
Use Jake Wharton's action bar sherlock library which is more flexible to support in all versions.
https://github.com/JakeWharton/ActionBarSherlock
If you need use the actionbar in android 3 and higher only, you can suppress warnings (be shure that min api level is set). But else you have to use SharlockActionBar library.

Action Bar in API < 3.0 With Google Maps Compatability Library

Yet another compatibility problem from me.
BACKGROUND
I have an application that works in all API's now that I downloaded a custom google maps compatibility support library which is an extension of the Compatibility Support Library and allows me the have fragments along with maps in API levels < 3.0. It does this by making me extend my activity by FragmentActivity, which due to this custom library is also a subclass of MapActivity.
THE PROBLEM
I also need to implement a tabbed Action Bar throughout my application on different API levels. It's possible to do this using ActionBarSherlock. However, that also involves downloading and extending my Activity`` byFragmentActivity` using this custom library and thats a problem as I then lose the ability to have maps because I can't extend using both libraries versions?
THE QUESTION
How can I have both features in my application?
If this isn't the way to do it, how can I do it?
Thanks in advance.
UPDATE
So I have implemented a system where I can display the action bar in api's greater than 3.0 and not display if they aren't.
Here is the code.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.show();
} else {
//Do something else
}
POSSIBLE SOLUTION
Would the best way to go about it be to create a custom ActionBar Module that is just a group of views with some functions to make it act and look like an action bar?
Do you try to extend your FragmentActivity by SherlockActivity ?
So here is the solution that I came up with.
ActionBarSherlock wasn't an option for me due to the fact I was already using a version of the compatability library that allowed me to use GoogleMaps along with Fragments and if I used the ActionBarSherlock library I would have lost this functionality.
My Solution
I decided to implement my own custom ActionBar which is a combination of Views and TextViews.
I have one class extending Fragment (the android support version) called CCActionBar and this inflates three custom objects called CCTab which extend TextView. These three tabs (as it says in the name) are acting as my tabs so we override when they get touched, pass them to our CCActionBar which may then pass it onto our MainActivity to handle any navigation.
To detect if we need to create this Custom Action Bar due to API level I use the following code.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
//setup a normal action bar because the api level supports it
} else {
//setup our custom action bar
}
So that's my solution, it may not be the correct one but it works for me.
Any questions please ask.

Action Bar in API Level > 3.0 but Still Support API < 3.0

Yet another compatibility question.
THE PROBLEM
I need to use a tabbed action bar in api level 3.0 and greater to switch between fragments. However, I also need to be able to switch between these fragments somehow in api level < 3.0.
The spanner in the works is the fact that I have already downloaded a custom compatibility library that allows me to use google maps with fragments and therefore I can't use a library.
THE QUESTION
How can I implement a tabbed ActionBar solution in 3.0 and greater and also cater for the bigger market that is 3.0 and less?
Any help would be grand.
What you want is ActionBarSherlock. It uses the native action bar for API >= 3 and provides backwards compatibility for API levels 2.x. There's also the Action Bar Compatibility sample project (listed as SupportAppNavigation, I believe). I think this is essentially the same thing.
So I managed to figure a solution a while ago, it's just taken me a while to post it here.
This is the solution that works for me. I needed to use an ActionBar but I also needed to use the MapActivity as well as the FragmentActivity hence not being able to use ActionBarSherlock.
What I did was the following:
Created a fragment called CCActionBar which handles the touching of the CCTab's by adding itself as a listener(explained below). It also inflates a layout called action barlayout which has the tabs and images arranged, just reference the tabs at run time.
Created a custom view called CCTab which represent the tabs of the action bar. When its touched it tells its listener (CCActionBar) its been touched.
In my main layout xml file have an action bar container which I show or hide at runtime depending on the API level.
Now in my main activity in my OnCreate method I check whether my API level is greater than 3.0 or not. If it is I just implement the standard ActionBar making sure my custom action bar container is invisible. If it isn't I set up my custom action bar and make sure my action bar container in my xml layout is visible.
Then I make sure that when a tab is pressed whilst using either method it is handled in the same way so once its setup you don't have to handle it any differently.
I hope this helps someone somewhere or at least give you an idea of how to proceed.
What I would recommend is to use ActionBarSherlock to get the tabbed action bar functionality.
I assume that you also need a MapView support in Fragment. For this I would recommend solution from MapView in a Fragment (Honeycomb)
(look at user1414726 answer with sample code).
I think it is a better solution than using library though it is using deprecated LocalActivityManger. But in my opinion it is a better idea than using library where every Activity is a subclass of MapViewActivity which I assume you are using.

implement action bar on android API 7

I am working on Android 2.1 with API v7.
How can I implement the action bar in this old version API which can work like built-in ActionBar on devices supporting API 11 or greater? Is there any support package ?
(A good tutorial on how to implement action bar in old version API is appreciated)
Here is an examle of adding actionBar with slides menu to api>7:
https://github.com/BradleyRL/NavDrawerExampleAppCompat-v7
It's documented here for api>14 http://developer.android.com/training/implementing-navigation/nav-drawer.html. (first link - is upgraded method of second link to support previous versions).
Then, you could read documentation here: developer.android.com/guide/topics/ui/actionbar.html
To do this this way you must add project library to your project (and for exammple in 1t link too).
To do it: File>New>Other>Android Project From Existing Code>Browse (Select "YOUR_PATH_TO_ANDROID_SDK\adt-bundle-windows-x86_64-20131030\adt-bundle-windows-x86_64-20131030\sdk\extras\android\support\v7\appcompat"), check adding it to workspace>Finish.
Then go to properties of your(example in 1st link) project>android>add (choose this library)>OK.
And you must extend your main class from ActionBarActivity insted of Activity. I try this, and it work on 2.3.7 and on 4.1.1 versions)
Hope it helps)
P.S. Sorry for my bad english and hi from Russia)
You can/should use this ActionBar project.
It's very very simple to use and I am using it myself in one project.

Categories

Resources