Using ActionBar in AppCompatActivity - android

Can I still use ActionBar in AppCompatActivity? Are there any issues on using it from Android 6 to Android 4?
I know Toolbar is the new ActionBar, but if I still want to use ActionBar, is this is safe? The reason I don't want to use toolbar is because I'm already skinning the ActionBar.
Btw, I see a huge fragmentation in the API, almost confusing I would say.
There's Activity, AppCompatActivity and setActionBar, setSupportActionBar and ActionBar/Toolbar...

Using Toolbar will be a great option it can be highly customizable, which means you can add navigation buttons, logos, titles, subtitle, action menu items, or even your own custom views. If you still want ActionBar,You need to use getSupportActionBar() while using AppCompatActivity
You can also check customizing option which toolbar provide to that of actionbar

Simple Answer: You need to use getSupportActionBar() if you are using AppcompatActivity irrespective of what version your app is running.
Recommended: Should you choose to use the new Toolbar instead of ActionBar, because it's way too flexible.

Related

What is the purpose of setSupportActionBar? What does it do?

If there is a toolbar, it is usually passed into setSupportActionBar(). Why?
As per docs
A Toolbar is a generalization of action bars for use within application layouts. While an action bar is traditionally part of an Activity's opaque window decor controlled by the framework, a Toolbar may be placed at any arbitrary level of nesting within a view hierarchy. An application may choose to designate a Toolbar as the action bar for an Activity using the setActionBar() method.
But in simple ways, this is a way of telling the Activity that you are interested in using the features related to Toolbar. It will delegate the functionalities related to your defined toolbar. It helps activity to understand the many of the requirements some of them mentioned below.
1) Setting menu options
2) Setting Navigation drawer
3) Setting common Toolbar
4) Setting back button on the top left
5) Using an icon for brand identification
6) Setting a common title or subtitle
7) And many more
If you don't mention for these functionalities by telling the activity using setSupportActionBar then you have to create all this by your self and support them back to the older version. With Toolbar it comes free and you have to just tell a activity to use it will take of supporting different functionalities itself.
if you want to apply your custom toolbar instead of default toolbar then to set toolbar into that specific screen/activity you must be use setSupportActionBar() along with your toolbar. ;)

Toolbar as self-managed ActionBar vs Toolbar as framework managed ActionBar

Alright, I am trying to understand what I would lose if I use Toolbar as a self-managed ActionBar and not use setSupportActionBar.
AFAIK, all that ActionBar does is, provide placeholders for logo, navigation and menu items and also let Fragments add/customize the menu items.
The same functionality could be achieved using Toolbar.setLogo(), Toolbar.setNavigationIcon(), Toolbar.setNavigationOnClickListener() and Toolbar.inflateMenu() apis. Of course, directing the menu handling logic to Fragments might be lost but I think that is not that big of deal if the Activity knows which Fragment is on top and changes the menu items accordingly.
I am trying to make sure :
If I can achieve every ActionBar capability just by using Toolbar and MenuItems (and not using setSupportActionBar()).
Is it ok to not have an exhaustive knowledge of ActionBar apis. Some of the ActionBar apis are very confusing. Using setHomeAsUp api to show hamburger icon, back icon etc., doesn't feel right. If someone starts learning android today, do they even need to understand the framework's ActionBar apis?
Update : In the article Android Design Support Library under section CoordinatorLayout and the app bar, I learnt that the new paradigm app bar is a replacement for the action bar paradigm. I think action bar will soon be deprecated and we should get used to the new app bar paradigm which covers the new material design user experiences.
Yes you can use achieve similar capabilities of ActionBar in Toolbar.
Mainly the difference lies is Toolbar becomes part of View so we can much more fun playing with them to get Scrolling Techniques
You can use Toolbar separately as View & perform ActionBar alike functionalities. For example, in one of my app I use 2 Toolbar one which is set to setSupportActionBar() while other is just used for some other functionalities.
Conclusion: Well it depends upon your requirements if you want to use Toolbar as self or framework. None the less you can use it as both.
I hope this answers your question well.
To me you are right.
From AppCompatDelegate source code, method setSupportActionBar(),
When set to a non-null value the getSupportActionBar() method will return
an ActionBar object that can be used to control the given toolbar as if it were
a traditional window decor action bar. The toolbar's menu will be populated with the
Activity's options menu and the navigation button will be wired through the standard
android.R.id.home menu select action.
So these are most, if not all, the benefits you will have. As you said, it is easy to implement navigation and menu inflating through Toolbar APIs. However, I don't see what you would gain by not calling setSupportActionBar().
YES YES YES
using setSupportToolBar() is the same old Actionbar the only reason ToolBar is ToolBar is for versatility,same as Fragments is to Views, all lies on how you implement stuff, and also the old Actionbar is kinda boring and much restricted as to Toolbar

Android ActionBar or Toolbar

I've been reading/youtubing a lot about how I should start my next app and found out that the activities suggested as start activities in Android Studio may contain deprecated elements such as the ActionBar, and that I should rather start from scratch without an activity, and use the newly lollipop-introduced "Toolbar".
My question is: As I need a top bar of a kind (ActionBar/Toolbar) and a navigation drawer in my app, should I invest the time it needs to build the Toolbar opposed to starting with a NavigationDrawer activity from the beggining?
Will using an NavigationDrawer activity (with an ActionBar) mean that I'm staring a new app with old-fashioned kind of top bar?
Very first thing ActionBar is not deprecated and not old style component. It still serves the purpose well. Toolbar only comes into existence when you need more than ActionBar, for instance, you want to increase the height of ActionBar and want to inflate some custom views then ActionBar might not be a right choice. If you just need an Activity with NavigationDrawer, I don't think so you need a ToolBar at all. Just create a new project from scratch and pick NavigationDrawer as a pre-configured option while creating the project and you are good to go. ActionBar is not going anywhere, at least for now. Hope it would help.

how to set the NavigationMode in the ActionBar

When I tried to use ActionBar and specify the navigationMode eclipse says it is deprecated without suggesting the recommende one to use instead.
what should i use instead of this.actionBar.setNavigationMode(mode)?
There is no alternate method to use from the now deprecated setNavigationMode since the Toolbar is basically a stripped down version of the actionbar where you put in the views you want inside the toolbar.
for example if you wanted to replicate the Dropdown navigation mode you would put a spinner inside your toolbar

Android ActionBar backbutton and tabs

I want to create an ActionBar and a tabbed navigation like the google+ app.
I used this example as a starting point and now I have a great actionbar:
http://developer.android.com/resources/samples/ActionBarCompat/index.html
I've also included a ViewPager and a TabHost to have tabs and scrolling left/right Fragments.
What i need is to show the back arrow in version prior to honeycomb.
If I set getActionBar().setDisplayHomeAsUpEnabled(true), the arrow is automatically show in version >= honeycomb.
How can I do that in version prior to honey?
What I also want to have is Tabs like the google+ app.
This is how is my tab bar looks:
removed dead ImageShack link
...and this is what i want:
removed dead ImageShack link
I can't find any example to style tab bars like that one.
I would recommend you the ActionBarSherlock for ActionBar compatibility with Android <3. It is a better implementation that the example offered in the developers page.
Using this package, the back arrow will appear when you add to your activity the following line.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Regarding the Google+ tabs, I have found the widget ViewFlow very useful and looking exactly the same. Moreover, you don't have any backward compatibility issues. Have a look to the CircleFlowIndicator example.
You can use ActionBarSherlock and a ViewPager with ViewPagerIndicator to achieve the desired look. These libraries also work pre ICS.
As miguel.rodelas suggested, use getSupportActionBar().setDisplayHomeAsUpEnabled(true) for the home arrow. In onOptionsItemSelected you can catch it by the id android.R.id.home.
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
//use your custom xml view here
View actionBarView = getLayoutInflater().inflate(R.layout.action_bar_custom_view, null);
actionBar.setCustomView(actionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
//your logic for click listner
setListenerForActionBarCustomView(actionBarView);
As stated, download the ActionBarSherlock and import it as a library here
https://api.github.com/repos/JakeWharton/ActionBarSherlock/zipball/4.2.0
Instead of using getActionBar use getSupportActionBar
Download the support library
Instead of Activity, extend your class as SherlockActivity
Same with fragments, use SherlockFragment
Then just copy your code, should work perfectly.

Categories

Resources