I want to use Material design at under-Lolipop version of Android.
I know,
I must use AppTheme : Appcompat-v7
To use Actionbar with Appcompat-v7, I must extends ActionbarActivity
But, I don't want to use ActionbarActivity,
because of quite diffrence with Activity and It just looks like unflexible to me, and that makes me nervous to use ActionbarActivity. It looks like just Activity class for Actionbar.
So, I think about use Toolbar(stand-alone) instead of Actionbar.
And It seems when I don't use Actionbar, then don't have to use ActionbarActivity either.
Finally, I find a solution that use below.
AppTheme : Appcompat-v7 21
Toolbar(instead of Actionbar)
Activity(instead of ActionbarActivity)
Did you think this is aright way? or it just doesn't make sense at all?
But, I don't want to use ActionbarActivity, because of quite diffrence with Activity and It just looks like unflexible to me, and that makes me nervous to use ActionbarActivity. It looks like just Activity class for Actionbar.
I can't understand why it's unflexible to you to use ActionBarActivity, after all ActionBarActivity derives from Activity
And if I'm not mistaken you must derive from ActionBarActivity in order to use toolbar and all the menu, title etc stuff that was formerly used by the ActionBar itself.
That told, I think the theming part is ok, using Toolbar rather than ActionBar is also ok, the only part I don't agree with is the Activity instead of the ActionBar Activity.
Hope it helps !
Related
There are two principal ways of creating an app bar for an activity in API 21+ using the Toolbar.
Create an activity which extends AppCompatActivity and then follow the instructions here
Create a standalone Toolbarwhich acts as an app bar (define the Toolbar in xml using android.support.v7.widget.Toolbar) and then inflate a menu into it like this: ` toolbar.inflateMenu(R.menu.homeview_menu_common);
My question is: what are the benefits and drawbacks of doing one over the other?`
A related question to this topic can also be found here (How can an activity use a Toolbar without extending AppCompatActivity)
Short answer: No you should make your activity extend AppCompatActivty
You can create a toolbar without AppCompatActivty but besides an app bar the AppCompat also brings with it the support libraries that allow you to add material design to your app going as far back as API level 7 of Android.
Unless there is a specific reason for not using AppCompat all your Activites should extend AppCompatActivty to model a Material app.
You need to use an AppCompatActivity extended Activity because, when you set up the Toolbar as the ActionBar with setSupportActionBar(Toolbar) you get the ability to reference it through Context.getSupportActionBar() from nearly anywhere in your code i.e Fragment. But, if you don't extend AppCompatActivity you can't easily get a reference to the Toolbar from anywhere else other than the Activity in which it was defined.
I am already extending another class (expandablelistactivity) but I need a toolbar in my activity. Is there a way around this?
Usually I use toolbar in combination with:
setSupportActionbar()
in my activity. And in the xml for my activity, I'll use:
<android.support.v7.widget.Toolbar/>
And in my apptheme, I'll set it to no actionbar as so:
parent="Theme.AppCompat.Light.NoActionBar"
Folks are suggesting that I
import android.support.v7.app.AppCompatDelegate;
However, it says won't be resolved ("cannot resolve appcompatdelegate"). I have updated my Android Support Repository and Android Support Libraries.
You can use AppCompatDelegate with matching activity lifecycle callbacks for anything related to action bar.
From Chris Banes' blog:
There is a contract to maintain when you create a delegate. You must
callback to it at every call it exposes (for instance onCreate()), but
it’s really simple and can be extracted into a base class.
The end result is that you can attach all of AppCompat’s functionality
to any Activity sub-class, as long as you call it as it wants.
Check out this example of AppCompatPreferenceActivity, extending the no action bar PreferenceActivity, and using AppCompatDelegate to provide action bar.
The 22.1 release of the v4 support library offers the new AppCompatDelegate. AppCompatDelegate now exposes methods like setSupportActionbar() that before were part of ActionBarActivity.
Here is the blog post from Google where AppCompatDelegate is introduced if you need more information.
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.
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.
}
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 :)