I am trying to change the android action bar style. The minimum API support for the app is 8. So as using android:actionBarStyle attribute in the style xml file in the values folder gives the minimum required API 11 error, I have commented it out and moved it to a separate style xml file in v11 folder.
However, when using the actionBarStyle attribute only for android 2.1 and higher as mentioned in the android documentation, it gives me the No resource found error. Below is the style code:
<style name="AppTheme" parent="android:Theme.DeviceDefault">
<item name="android:typeface">monospace</item>
<!-- <item name="android:actionBarStyle">#style/MyActionBarTheme</item> -->
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
I have tried changing the style parent theme from DeviceDefualt to Holo.Light.DarkActionBar, AppCompat.Light.DarkActionBar, etc. but the error still remains.
If you want to use the standard Action Bar on Android devices v7 appcompat library, which backports the Action Bar to any API 7+ device when you use ActionBarActivity. (Note, the Android documentation you found reference this same library, hence the 'When using the Support Library' comments throughout that document)
To add it to your project, you can follow the directions for adding a library with resources for whatever IDE you use.
Once you have the appcompat library added, you may consider using a tool like the Android Action Bar Style Generator which automatically builds the correct styles/resources needed to fully style your action bar (make sure to change the 'Style Compatibility' drop down to 'AppCompat').
Related
I'm quite confused about how styling in appcompat library works.
According to here:
We now use the support implementation of Toolbar/ActionBar on all
platforms meaning that we no longer read any android: attributes
related to the action bar.
For apps which already have existing appcompat setups, this means that
you should remove your v14+ themes which re-set the same values in the
android namespace. Please note, this is ONLY applicable for
styles/widgets which affect the action bar.
For most apps, you now only need one theme declaration, in values/
So here is my question:
If I want to use material design ActionBar in API 14+, I can just use ActionBar/Toolbar provided in appcompat_v7 and style it in the common value/ folder with the "android:" namespace removed ? but why am I seeing people writing code below:
<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
....
<item name="windowContentOverlay">#null</item>
<item name="android:windowContentOverlay">#null</item>
....
</style>
why is "android:" namespace there? what's the difference between the code above and using value-v21, value-v14, folders?
can someone explain or direct me to the right source?
Yes, if you use AppCompat v21+, you only need a single theme with a parent of Theme.AppCompat (or a subtheme such as Theme.AppCompat.Light) and you do not need the android: namespace attributes for action bar/window related flags, nor separate themes for v14, v20, etc. The full list of top level attributes which AppCompat provides across all API levels can be found in the AppCompat R.styleable Theme.
Much of the code on the internet (including parts of the developer.android.com site) are still written for the pre-v21 AppCompat, which did require both the android: and non-prefixed versions.
I recently started Android programming, all is good and dandy, but I've came across a problem that I couldn't find an answer to, and I really diged hard for 4 days so far.
My app uses support action bar, and to be specific "android.support.v7.app.ActionBarActivity". Long story short, I couldn't handle most of the stuff applicable to an Action Bar to this support one.
My app uses this support action bar by default due to setting up my mini sdk version to 14.
I want to be able to build an action bar from scratch, and customize it, since the default action bar is not responsive to my customization in styles.xml, etc.
I don't mind using Holo theme library instead of AppCompat.
So the question here, how can use Action Bar instead of Support Action Bar?
How can I extend my java class to use that instead of the support one?
Because none of the online customizing solutions are applicable to the support action bar.
A bit foggy description so I apologize for that.
Create a new project, and select the minimum API level as 15. When you do this, the appcompat-v7 library will not be required for this project as it is for projects with minSdkversion < 15. In this project, the classes android.app.ActionBarActivity and android.app.ActionBar will be used by default, i.e. the native AOSP classes and not the ones from the support library.
The following will let you have an ActionBar with custom background color as you want it, on API level 8 and above.
STEP 1. In your res/values folder, define an XML file theme.xml and add the following to it:
<resources
xmlns:tools="http://schemas.android.com/tools">
<style name="DefaultActionBarTheme" parent="#style/Theme.AppCompat.Light">
<item name="android:actionBarStyle" tools:targetApi="11">#style/MyActionBar</item>
<item name="actionBarStyle">#style/MyActionBar</item>
<item name="android:actionBarSize" tools:targetApi="11">#dimen/action_bar_wrap_content</item>
<item name="actionBarSize">#dimen/action_bar_wrap_content</item>
</style>
<style name="MyActionBar" parent="#style/Widget.AppCompat.Light.ActionBar">
<item name="android:background" tools:targetApi="11">#color/actionbarbgcolor</item>
<item name="background">#color/actionbarbgcolor</item>
<item name="android:height" tools:targetApi="11">#dimen/action_bar_wrap_content</item>
<item name="height">#dimen/action_bar_wrap_content</item>
</style>
</resources>
In the same folder make another XML file colors.xml and add the following to it:
<resources>
<color
name="actionbarbgcolor">#00FF00
</color>
</resources>
and to the existing file dimens.xml, add the last line:
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<!-- Optional, in case you wish to increase the default width of the Action Bar. -->
<dimen name="action_bar_wrap_content">55dp</dimen>
</resources>
In place of #00FF00 above, use the hex color code for the background color you wish to use in your ActionBar.
NOTE: The above will work assuming you are using the appcompat-v7 library. If not, then you'll have to use one of the Holo.Light themes instead of AppCompat.Light, and there will be other changes as well.
STEP 2. In your manifest file, you must add:
android:theme="#style/DefaultActionBarTheme"
to every <activity declaration if that Activity has the ActionBar.
Try this. It will work.
Zygotelnit answer works but you have to omit the ["tools:targetApi="11"] from item declaration otherwise it will give you an error for some reason.
On the other hand, I've found a much shorter and easier but not so optimized solution.While searching through the actionBar class and playing around here is the answer:
In your activity.java, go down to
protected void onCreate(Bundle savedInstanceState)
Anywhere appropriate in that method, write the following code:
ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new
ColorDrawable(Color.parseColor("#D62D20")));
You replace the color code by any color code of your choosing. It's obviously a hex color code.
I have used this below code to set the status bar color. placed this code under values-v21 folder
`
<style name="AppTheme" parent="android:Theme.Material">
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:colorPrimaryDark">#263143</item>
<item name="android:colorAccent">#color/accent</item>
</style>
`
But it makes following
error: Error: No resource found that matches the given name: attr 'colorPrimaryDark'.
How to solve this?
You should drop android prefix from android:colorPrimaryDark
<item name="colorPrimaryDark">#263143</item>
We now use the support implementation of Toolbar/ActionBar on all platforms meaning that we no longer read any android: attributes related to the action bar.
Source: https://chris.banes.me/2014/10/17/appcompat-v21/#theming
Also, Theme.Material is only available from API 21. If you want it work on lower APIs, use AppCompat themes instead.
The material theme is only available in Android 5.0 (API level 21) and above. The v7 Support Libraries provide themes with material design styles for some widgets and support for customizing the color palette.
Source: https://developer.android.com/training/material/theme.html
StatusBar doesn't work for devices with API level less than 21. If your min API level is less than that then you have to do some change in your java source file. Go to the source file to which you want to add the status bar color. Then wrap the OnCreate() method with annotation #TargetApi(Build.VERSION_CODES.LOLLIPOP).
Then inside the method you have to check whether the device is capable or not as shown below.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(getResources().getColor(R.color.Statusbar));
}
I am facing this annoying problem with the Action Bar Sherlock (ABS) in a 'theme.xml' file of my Android Library Project that I am creating and I cannot really find a solution to it.
I have referenced the ABS library correctly into my Library Project and the build target of my Library project is currently set to 19 with the minSdkVersion being 8. I have used the ABS before but this is kinda strange.
Here is my theme file:
<style name="Theme.MyTheme" parent="#style/Theme.Sherlock.Light">
<!-- Set style for Action Bar (affects tab bar too) -->
<item name="actionBarStyle">#style/Widget.MyTheme.ActionBar</item>
</style>
I also tried it without the '#style' tag:
parent="Theme.Sherlock.Light"
I have checked out various posts and tried the answers mentioned but it does not seem to work.
Is this because I am creating a Library project? Or Is there a silly issue that I am overlooking?
Any help will be appreciated.
So i have been following the tutorials on the android developer website. i have created an android project with min SDK of 8, and included the compatibility for the android 2.1 for the menu bar. i am able to get the menu bar to work properly for android 4.0 and 2.2, no issues.
now i am trying to set the overlay for the menubar, and i ran into an issue. while setting up a custom theme with the parent being an AppCompact theme, as stated here http://developer.android.com/training/basics/actionbar/overlaying.html .
the code with the issue is :
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#android:style/Theme.AppCompat">
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="windowActionBarOverlay">true</item>
</style>
2 issues arise:
error: Error retrieving parent for item: No resource found that
matches the given name '#android:style/ Theme.AppCompat'.
and
android:windowActionBarOverlay requires API level 11 (current min is
8)
the first issue, i have no idea why it is being thrown. i use Theme.AppCompact in my manifest and it works. the 2nd issue is confusing me, in the google tutorials it states to include both definitions, as one is for android devices with the new API and the other is for older API's.
i have tried to clean/build my project, it did not help.
Solved by replacing
parent="#android:style/Theme.AppCompat"
with
parent="#style/Theme.AppCompat"