Steps to write better ActionBars in Android - android

These are my doubts while using ActionBar.
1) To make it prevalent across various tabs, I had to repeat the same code in all the tabs. Is there a better way to rewrite this part? I could have one ActionBar class which I can call everywhere, but that won't make it dynamic. (More options in certain tabs.)
2) I'm presently defining a menu and inflating it. Is it the best way to go about it? The problem I am facing here is I'm definitely not using the entire space and always running out of space to put in more actions. How to rectify it?
3) The third problem that I'm facing is a bug when I use Theme.Light. The app crashes. If I use Theme.Holo.Light, the app doesn't. Though, it still crashes in a 2.3 supporting phone. Anyway to rectify that? I mostly feel that I've missed out on backward compatibility or is Action bar not compatible with certain themes?

This answers your 3rd question, straight from docs here:
Beginning with Android 3.0 (API level 11), the action bar is included in all activities that use the Theme.Holo theme (or one of its descendants), which is the default theme when either the targetSdkVersion or minSdkVersion attribute is set to "11" or greater.
If you want to use ActionBar APIs, such as to add navigation modes and modify action bar styles, you should set the minSdkVersion to "11" or greater. If you want your app to support older versions of Android, there are ways to use a limited set of ActionBar APIs on devices that support API level 11 or higher, while still running on older versions. See the sidebox for information about remaining backward-compatible.
Sidebox info
If you want to provide an action bar in your application and remain compatible with versions of Android older than 3.0, you need to create the action bar in your activity's layout (because the ActionBar class is not available on older versions).
To help you, the Action Bar Compatibility sample app provides an API layer and action bar layout that allows your app to use some of the ActionBar APIs and also support older versions of Android by replacing the traditional title bar with a custom action bar layout.

Related

Dumping AppCompat for API 21

So I'm creating an app that is Lollipop 5.0 API 21 and up with zero interest in supporting older devices. Do I still need the appcompat library when using Material Design like UI elements & layouting (sidebar aka nav bar, ink, etc) ? When stripping it down I often encounter crashes when trying to move away from the AppCompat stuff. Changing activity types from the AppCompat one to the Normal one, I end up with problems regarding dependencies on layout types like the coordinator layout that aren't there.
I'm still new to android and this is very confusing, as my theme is currently in limbo somewhere between the appcompat theme & material design when I tried to change it from one of the template.
I've read that this is an android studio issue because it always uses appcompat regardless of your set dependencies & minimum API levels. Forcing you to manually override every implicit hidden appcompat call.
When trying to dump AppCompat, what changes to I need to do to make that happen ? Manifest, Activities, Menus, Layout, Styles ? It seems to touch all these things. If going exclusive without Appcompat, do you still need the v21 folders or will it grab the default ones ?
Do I still need the appcompat library when using Material Design like UI elements & layouting (sidebar aka nav bar, ink, etc) ?
If you are using classes out of the Design Support library, such as NavigationView, you generally need to use appcompat-v7 and AppCompatActivity. As of early May 2016, Google has not shipped a Design Support library analogue that works with Theme.Material. You may be able to find third-party library replacements for some of those widgets, and seasoned Android developers can sometimes "cross-port" these components to eliminate the appcompat-v7 dependencies.
I've read that this is an android studio issue because it always uses appcompat regardless of your set dependencies & minimum API levels. Forcing you to manually override every implicit hidden appcompat call.
The only place that Android Studio really cares about appcompat-v7 is in the new-activity wizards, which you do not have to use.
When trying to dump AppCompat, what changes to I need to do to make that happen ? Manifest, Activities, Menus, Layout, Styles ?
That is difficult to answer in the abstract. You would need to:
Stop using Design Support library widgets and containers
Stop inheriting from AppCompatActivity
Change your app: attributes in your menu resources to their android: equivalents
Change your theme to not use Theme.AppCompat
If going exclusive without Appcompat, do you still need the v21 folders or will it grab the default ones ?
-v21 resource directories are not tied to appcompat-v7. They will still be used, on API Level 21+ devices.

Is there any benifit of ActionBarSherlock if my app run using android 4 and above

I was using ActionBarSherlock to make my app run with app version of android and use actionbar, but now I don't need to support the old version and it is enough for me to support 4 and above , is there any benefit of ActionBarSherlock still for my case ?
Not really. Feel free to use the native API Level 11+ implementation of the action bar.
Note, though, that some things in Android may steer you in the direction of using an action bar backport... though the appcompat one published by Google, not ActionBarSherlock. For example, the recommended version of MediaRouteActionButton requires the appcompat action bar, though I am maintaining a port that works with the regular action bar.

In Android do I need to support both compat Action Bar and regular one?

I have an old Android app that was target at API level 8 (2.2). I'm planning to update it to targetSDK 4.4.2. Main change will be using the Action Bar on pretty much every Activity. Right now it uses the old menu.
I know I have to use android.support.v7.app.ActionBar to support Action Bar for older phones.
Is it recommended to implement the "regular" android.app.ActionBar for APIs 11 and greater alongside android.support.v7.app.ActionBar?
Will they look any different?
No, you can use android.support.v7.app.ActionBar for all API levels.
Your ActionBar wont look different at all for APIs 11 or greater.
You can keep using android.support.v7.app.ActionBar.

Sherlock Actionbar RTL

Android 4.2 introduced the support for Right To Left action bar, is there a way to configure the Sherlock Action bar to be in RTL mode?
note that the current Sherlock Actionbar version is 4.2, but I couldn't find anything related to the RTL support.
As of this time, the RTL feature of the ActionBar has not been included in the support library. From what I can gather, it's not scheduled to be, either, since there were some changes to the underlying View structure.
If you're using ActionBarSherlock, it will work the same as if you're using a native ActionBar. From 4.2 up, it will support RTL, and not on lower platform levels. Assuming you have android:supportsRtl="true" in your manifest, that is.
If you want to have a good RTL experience for all users, while still using the native RTL support for those on >=4.2, you can check the build level in your app and plan accordingly.
The alternatives as I see it are:
Use a custom layout for all users and forgo the ease of the new API levels
Support RTL for no users
Your question was: "Is there any way to configure ABS for RTL?". The answer to that is yes, but only if users are on 4.2. For anything lower than that, there is simply no way to do it without custom layouts.

Understanding the action bar in Android

So I have a few questions about implementing the Android action bar. Up to this point I haven't really made any apps that were (in my opinion) good enough to put on the market. This really seems to be my inability to understand how the layouts and action bar will work in different versions of Android.
Of course Google seems to be the best bet for this, but I come across a few grey areas that maybe someone can clear up. I can see that the Action bar was implemented in API level 11, but then what happens when I try to run it on a device lower than 3.0? Also, this seems to be the most helpful when understanding on how and where to use the Action bar, but does this mean the action bar is done all programatically and not in the layout? I guess it's safe to say that I'm a bit "intimidated" by using the action bar because of the different layouts I'll get in different devices... so is there a way to make it "look" like I have an action bar even though it's done purely through layouts? Or is that a bad habit to get into?
I have heard that maybe the compatibility library might work well, but I don't really know what it does and I'm not yet savy enough to understand the android documentation on what it "really" does. If it really is "that" helpful at supporting older devices, why isn't it built/come standard in the SDK?
I know this is a very broad question, but when it boils down to it, I just want to know if there really is a way to make all of my layouts look and work the same, or if it's even worth my time and just have the action bar auto downgrade for old devices.
Just an additional question, I have a Galaxy Nexus and all of the core Google Apps look awesome with the action bar, but is there a way to see these updated apps running on API 11 or lower?
Lastly, if anyone has stumbled upon a great tutorial on any of this please list it. I'm really trying to become knowledgeable on how Android layouts work in all API levels.
I can see that the Action bar was implemented in API level 11, but then what happens when I try to run it on a device lower than 3.0?
By default, the user will get options menus, as before. Or, use ActionBarSherlock to have an action bar working back to Android 2.1, as Mr. Weening noted.
but does this mean the action bar is done all programatically and not in the layout?
It is the same as options menus, using mostly menu resources and Java code.
If it really is "that" helpful at supporting older devices, why isn't it built/come standard in the SDK?
You are welcome to create a time machine and go back in time to add that code to Android devices dating back to 2009. You might be able to find a lightly-used flux capacitor on eBay, though tracking down a Mr. Fusion unit may be somewhat more troublesome. Or, if you're in the UK, see if you can find a used TARDIS dealer.
The rest of us, in the meantime, will use the Android Support package in part for its backports of fragments, GridLayout, etc., so we can use new capabilities that are not native to older devices, while maintaining API fidelity with the native implementations on newer devices.
To support older version of android with a same looking action bar you could take a look at Action Bar Sherlock
http://actionbarsherlock.com/
From their website:
The library will automatically use the native action bar when appropriate or will automatically wrap a custom implementation around your layouts. This allows you to easily develop an application with an action bar for every version of Android from 2.x and up
This works fairly well.
If you're a little more adventurous and you'd want more control over it's layout, look and feel, you could always try to implement it yourself. It's not an easy task but the action bar is more of a ux design than an actual component. The code for the compatibility libraries is a good place to start then.
The easy way if you just need a menu, a title and an icon up there:
choose a Theme with ActionBar, e.g. 'Theme.Holo'
create a new menu i.e. actionbar.xml
Implement this in your Activity
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbar, menu);
return true;
}
Since this is roughly the same as a menu in older versions, the older versions just lack the ActionBar and have the old menu instead.
Or
If you want the same ActionBar in all your Versions including before API 11, as Geert already suggested:
http://actionbarsherlock.com/
If you try to use the ActionBar on a version of Android that is not supported then it will just crash your application. To get around this, you can implement the library ActionBarSherlock which uses the native ActionBar if available, otherwise it creates its own that has the same functionality. You can also check the Android version during runtime and handle it yourself. I would check this link out as it actually has an ActionBar example:
http://developer.android.com/training/basics/supporting-devices/platforms.html#version-codes

Categories

Resources