AppCompat - Error while styling the actionbar - android

Im using the appcompat library to implement an action bar. While using a custom style it gives an error android:actionBarStyle requires API level 11 (current min is 10). I want the app to be compatible with android 2.3. Im using the google sample code.
themes.xml
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/actionbar_background</item>
<!-- Support library compatibility -->
<item name="background">#drawable/actionbar_background</item>
</style>

AppCompat is compatible with 2.3, but you need to define 2 styles (as you have above): 1 for the native actionbar implementation (android:actionBarStyle) and one for the compatibility implementation (actionBarStyle).
If you define both those styles in your values/styles.xml, you will get compile errors - because values/styles.xml is compiled for all api versions, and 2.3 doesn't know about android:actionBarStyle.
So you can duplicate your styles.
Have a values/styles.xml and a values-v14/styles.xml
The styles in values-v14/styles.xml will override any styles in values/styles.xml when run on a api 14 and above device.
Or you can keep all your styles in the one values/styles.xml, but on the attributes you are getting the error on, add a tools:targetApi="14" to the tag, which tells the compiler to ignore this on non api 14 devices.
e.g.
<item name="android:actionBarStyle" tools:targetApi="14">#style/MyActionBar</item>

Related

Change Xamarin.Android Api Version

I already set all Api versions to Api 23 in the Android project properties, but it doesn't seems to help. This is the errorcode I always get:
Could not find android.jar for API Level 25. This means the Android
SDK platform for API Level 25 is not installed. Either install it in
the Android SDK Manager (Tools > Open Android SDK Manager...), or
change your Xamarin.Android project to target an API version that is
installed. (C:\Program Files
(x86)\Android\android-sdk\platforms\android-25\android.jar missing.)
I already tried to switch to Api Version 25 but I don't get an update for Xamarin.Forms and therefore I can't update the other packages.
Edit:
If I update the SDK to Level 25 and try to deploy I receive the following Errors:
The error occurs in the file style.xml:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MainTheme" parent="MainTheme.Base">
</style>
<!--Base theme applied no matter what API-->
<style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
<item name="windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
<item name="windowActionBar">false</item>
<!--Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette
colorPrimary is used for the default action bar background-->
<item name="colorPrimary">#2196F3</item>
<!--colorPrimaryDark is used for the status bar-->
<item name="colorPrimaryDark">#1976D2</item>
<!--colorAccent is used as the default value for colorControlActivated
which is used to tint widgets-->
<item name="colorAccent">#FF4081</item>
<!--You can also set colorControlNormal, colorControlActivated
colorControlHighlight and colorSwitchThumbNormal.-->
<item name="windowActionModeOverlay">true</item>
<item name="android:datePickerDialogTheme">#style/AppCompatDialogStyle</item>
</style>
<style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#FF4081</item>
</style>
</resources>
If you already changed the API Version, try to install the Nougat 7.1 SDK (Api Level 25). If that is already done, clean %USER%\AppData\Local\Xamarin, clean your solution and install what you need.

Android Styles.xml Top Level Element Not Completed

I cannot explain nor fix this nagging error of "Top level element not completed" This is a brand new project as in just created.
Here the styles.xml screenshot
And here is part of the Gradle file
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.android.support:support-v4:22.1.1'
}
So its not like its missing a reference. The only other thing in the app so far is ToolBar, what else am I missing to make this error go away, I am still able to build and run the app,but the red squiggly line remains, any ideas would be greatly appreciated.
first of all add this
compile 'com.android.support:appcompat-v7:22.0.0' // 21 if your target is 21.
in your style.xml file create base theme..
This means that you have one base theme which will applicable in all version
from 11 to 21 in your case. you can set theme as you want.
<style name="AppTheme" parent="AppTheme.Base">
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/primaryColor</item> <!--do not use android:colorPrimary : older version doesn't recognize it.-->
<item name="colorPrimaryDark">#color/primaryColorDark</item>
<item name="colorAccent">#color/accentColor</item> <!-- in base theme don't use android prefix. it is not supported -->
</style>
above all code in style.xml
Now, you have style.xml(v21)
in that file you need to inherit that base theme.
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:colorPrimary">#color/primaryColor</item> <!--In version 21 you need to provide it as android:colorPrimary : older version only doesn't recognize it.-->
<item name="android:colorPrimaryDark">#color/primaryColorDark</item>
<item name="android:colorAccent">#color/accentColor</item>
<item name="android:windowNoTitle">true</item>
<item name="android:actionBarSize">60dp</item>
<item name="android:textColor">#FFF</item>
</style>
This way, i hope you understand, AppTheme.Base will be applicable to all API.
and plus in 21 it will use specifically with v21. in this way you can set your min sdk version lower as you wanted it to be 11.
Try this.! let me know if it works.!
It might be related to your style inheritance. Maybe try deleting the first AppTheme style, and changing your custom style like this:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">...</item>
</style>
Just make sure you reference this AppTheme name for you activities.
After toiling with this for a few hours, I started to suspect that my Android Studio installation or atleast my Android Studio editor is corrupt and my assumptions turned out correct.
Following instructions here I reset my Android Studio installation to defaults and voila the error went away. For the actual styling one does not have to create a seperate folder -v21 for lolipop devices. Per this post something like this will work.
If you are using AppCompat, then all of the material color palette attributes (such as colorPrimary) are available to all API levels, so you can write a single theme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
</style>

how to use material design features in api lower than 21 in eclipse?

there are many questions regarding this but not even single helped me.
So please tell me how to enable material design features in eclipse.
i have also imported appcompat_v7 library in project but it does not create a folder with name values_v21 which helps in using those feature i think.
So please help me out in this
here is my styles.xml file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/AppTheme_ActionBar</item>
<item name="android:actionBarSize">#dimen/action_bar_height</item>
<item name="android:windowContentOverlay">#drawable/actionbar_shadow</item>
<item name="android:homeAsUpIndicator">#drawable/ic_ab_up_compat</item>
<item name="android:actionBarTabTextStyle">#style/tabtextcolor</item>
<item name="android:actionOverflowButtonStyle">#style/AppTheme_ActionBar_Overflow</item>
</style>
<style name="AppTheme_ActionBar" parent="android:Widget.Holo.ActionBar.Solid">
<item name="android:background">#color/apptheme_primary</item>
<item name="android:actionBarTabTextStyle">#style/tabtextcolor</item>
<item name="android:icon">#drawable/actionbar_icon_placeholder_compat</item>
<item name="android:titleTextStyle">#style/AppTheme_ActionBar_Title</item>
</style>
<style name="AppTheme_ActionBar_Title" parent="android:TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#android:color/white</item>
<item name="android:textSize">20sp</item>
</style>
<style name="AppTheme_ActionBar_Overflow" parent="android:Widget.ActionButton.Overflow">
<item name="android:src">#drawable/ic_ab_overflow_compat</item>
</style>
<style name="tabtextcolor" parent="#android:style/Widget.Holo.Light.ActionBar.TabText">
<item name="android:textColor">#android:color/white</item>
</style>
</resources>
There are some points you should check:
Import into eclipse, last version of android-support-v7-appcompat
project. You should update via SDK Manager, and import project from {your_path_to_android_sdk}/extras/android/support/v7/appcompat
Add this project as library dependency to your project
In styles.xml use Theme.AppCompat.Light or Theme.AppCompat:
style name="AppTheme" parent="#style/Theme.AppCompat.Light"
A simple guideline:
import the appcompat-v7 project and add it as a library dependency for your project
let your theme inherit some of the appcompat themes. For example <style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
Note that not all widgets (ui elements) are material-styled by appcompat
Follow the examples and explanations in the android-developers blogspot.
The values-vXY folders are used to add specific resources for a specific platform. For example you might add a style (in a values-v21 folder) that inherits some of the material styles that are built-in and available only one api level 21+. These folders are created manually and are only used if you want an alternative resource for a specific platform level. They are used when you want your app to fit better in all/many android versions. More info - at the android developers portal

Android API 7 - AppCompat Theme Custom Font

Trying to Add App Support Android 2.0 or API Level 7 Support to my application (Current Support is only for the versions of 3.0 & 4.+). as android suggests, i added AppCompat Lib and changed minSDK to 7.
but the problem is i used following style for the buttons
<style name="buttonText" parent="#android:style/TextAppearance">
<item name="android:textSize">16sp</item>
<item name="android:textColor">#color/linecolor</item>
<item name="android:fontFamily">sans-serif</item> <!-- This one not accepting -->
</style>
fontFamily is not available and says "FontFamily requires API Level 16(Current 7)"
I would like to know what is the alternative way to solve this issue?
You mustnt use android: prefix just use it like
<item name="fontFamily">sans-serif</item>
if you added the support library truely.

R Cannot be resolved to a variable.

I am trying to run an simple android project from a tutorial, However I am receiving the popular "R cannot be resolved to a variable" error. I have checked my gen folder and the R.id file is not there. This is the list of things I have tried so far:
have done a clean build several billion times.
fixed the project properties
There are no errors in my .xml files and they do not contain uppercase letters etc.
The package name in the source and manifest are exactly the same.
Have installed the latest SDK updates + restarted eclipse.
have deleted the gen file and done another clean build.
There is only an error in the source files, according to eclipse.
Here is a themes.xml file that may be causing a problem:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="android:actionBarTabTextStyle">#style/MyActionBarTabText</item>
<item name="android:actionMenuTextColor">#+color/actionbar_text</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#android:style/Widget.Holo.ActionBar">
<item name="android:titleTextStyle">#style/MyActionBarTitleText</item>
</style>
<!-- ActionBar title text -->
<style name="MyActionBarTitleText"
parent="#android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#color/actionbar_text</item>
</style>
<!-- ActionBar tabs text styles -->
<style name="MyActionBarTabText"
parent="#android:style/Widget.Holo.ActionBar.TabText">
<item name="android:textColor">#color/actionbar_text</item>
</style>
</resources>
Apologies in advance if this sounds familiar, but I have tried everything I have seen from previous posts.
Happy to provide more details if needed. Thanks in advance.
Theme.Holo and Widget.Holo.ActionBar requires API Level 11
TextAppearance.Holo.Widget.ActionBar.Title and Widget.Holo.ActionBar.TabText requires API Level 13
Check your android:minSdkVersion. If it is less than 13 than you can't build your project(or you have to duplicate your styles for different API Levels, using different api level folders under res). eclipse shows this kind of errors.
And also your Android Project Build Target must be at least API Level 13.

Categories

Resources