Add Action Bar without Subclassing ActionBarActivity - android

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.

Related

Can I use AppCompat theme with Activity/FragmentActivity classes?

Given the following:
Android documentation says :
AppCompatActivity - Adds an application activity class that can be used as a base class for activities that use the Support Library action bar implementation.
I'm not considering adding action bar to my activity
I need some material design widgets, which I can control only through AppCompat or material theme, but the latest need API >= 21, which is not my case.
I tested Theme.AppCompat.Light.NoActionBar with Activity class and it works well.
--
Is there an issue with using Activity class with AppCompat theme in my case?
The AppCompat library is intended to make compatibility with olders API, so the Theme and all components may work well in older systems.
I think the only concern is to always use the AppCompat elements and not the regular ones.
Example, use AppCompatEditText, AppCompatTextView, etc... And always refers to they with the AppCompat (AppCompatEditText editText;)
I have used a lot the support library and not have others issues, considering the visual elements may be a little different when using an API minor than 21
No.There is not issues with AppcompactActivity & support libaray. You can refere this link
To gain more rich & amazing look go with support library,Try to do material design

How to extend multiple Android activities

Say someone wants an Activity which both has an action bar and a preference, the first idea in mind is probably
public class MyActivity extends ActionBarActivity, PreferenceActivity
But Java doesn't allow this. I know API 11+ Activities has actionbar builtin. It's just an example of wondering how to use multiple features from multiple base classes.
EDIT: Based on the feedback it seems we have to hack in this case. IMHO it could be as simple as putting all activity utilities as fields in class Activity and implement getter/setter to use those utilities. Well, in reality, it isn't.
No you cannot extend from two classes in Java. Typically in Android to add the ActionBar to the older PreferenceActivity there are a couple of hacks you can do or libraries that also do the same thing. However, recently with the new AppCompat library they introduced the Toolbar widget which can be used to add an Actionbar to your PreferenceActivity in this case. For more information, checkout this post I recently wrote on how to add a Toolbar to your legacy SettingsActivity.
simple solution:
Firstly you can't extend multiple classes..java does not support multiple inheritance see here
Secondly using action bar sherlock library here, this gives you action bar functionality without extending the actionbaractivity plus its backwards compatiable.
Or...you can implement a custom action bar go here
As mentioned in the other answers, Java doesn't allow multiple inheritance.
If you want an ActionBar as well as something such as Preference functionality, consider using a PreferenceFragment
It's not quite the same as multiple inheritance but Fragments allow adding extra functionality to Activities.
You can create a subclass of the PreferenceActivity, called AppCompatPreferenceActivity (or whatever you would like), to use an AppCompatDelegate to provide the SupportActionBar functionality. You can then subclass the new AppCompatPreferenceActivity for your MyActivity class like so:
public class MyActivity extends AppCompatPreferenceActivity
For how to do this, check out the AppCompatPreferenceActivity sample code from the Chromium project.

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.
}

Action bar for phones below API 11

I am developing an android app, where I want to put action bar for phones below API 11.
I am following the below link.
http://hmkcode.com/add-actionbar-to-android-2-3-x/
I am making use of the v7 support library, and I am able to get the actionbar. But, the problem is I want to add a navigational listener or tab listener to the action bar. How is it possible.
Please help! Thanks in Advance.
You should use ActionBarSherlock. It is an excellent library of tools that allow you to use Action Bars in older phones and is widely used. The catch is that all your Activities will need to extend SherlockActivity instead of Activity and you have to be careful to call methods like getSherlockActivity() in Fragments instead of getActivity().
I prefer HoloEverywhere library
It allows much more than ActionBarSherlock, but makes your app ~3Mb bigger in size
I am sure you have not seen this
http://developer.android.com/guide/topics/ui/actionbar.html

ActionbarActivity class or FragmentActivity class

I want to create an activity that uses action bar ui pattern and fragments. Which base class my activity should extend, ActionbarActivity or FragmentActivity?
http://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html
ActionBarActivity is the right answer
As #Pontus said, it depends on the way your requirements are:
If you are aiming API 11+ then you should go for FragmentActivity.
If you want to have your app compatible to older devices version 2.1+ then you will need the support library, ActionBarActivity.
In addition to ActionBarActivity, you have a very customized and easy to use library by Jake, ActionBar Sherlock available and its easy to implement as well. So if you wanna customize the actionbar and want it to be flexible you can even go for this.
Try and choose the best suit o your shoes. Happy Coding :)

Categories

Resources