I am writing an xml for a drawable. My question is: is there a way to give the src property a reference to a attribute defined in the attr.xml file ? I've tried:
android:src="?image"
and
android:src="?attr/image"
None of them seem to work. Here's my attr.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="image" format="reference"></attr>
</resources>
and where i define the values, which are related to theme:
<resources>
<style name="HOLO" parent="#android:style/Theme.Black">
<item name="android:textColor">#00FFFF</item>
<item name="image">#drawable/holo_vcard_settings_icon</item>
</style>
<style name="HOLO_LIGHT" parent="#android:style/Theme.Light">
<item name="android:textColor">#FF00FF</item>
<item name="image">#drawable/holo_light_vcard_settings_icon</item>
</style>
</resources>
A few suggestions:
Try clean your project. I've experience a few times where the XML changes are not updated in an Eclipse build.
Are you sure that your application or activity is either using HOLO or HOLO_LIGHT theme? Check the 'android:theme' key is set in your activity.
Try using the 'android:background' property instead of 'android:src', meaning don't use ImageView and just use a View or LinearLayout's background.
BTW, the correct syntax to reference your attribute is:
<... android:src="?attr/image" .../>
4th option: If all else fails, you can try using the attribute as a style. (just a hunch, it may not work) e.g:
<resources>
<attr name="imageStyle" format="reference"></attr>
</resources>
then in your styles.xml
<style name="LightImageStyle">
<item name="android:src">#drawable/light_png</item>
</style>
<style name="DarkImageStyle">
<item name="android:src">#drawable/dark_png</item>
</style>
then in your themes.xml
<style name="HOLO" ...>
<item name="imageStyle">#style/DarkImageStyle</item>
</style>
<style name="HOLO_LIGHT" ....>
<item name="imageStyle">#style/LightImageStyle</item>
</style>
finally in your layout:
<ImageView style="?attr/imageStyle" ... />
Related
I'm having a hard time compiling my Android App in Xamarin Studio. The error that comes up is as follows:
No resource found that matches the given name attr "colorPrimary"
Which refers to my styles.xml:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="#android:style/Theme.Material.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<!--item name="colorPrimaryDark">#color/colorPrimaryDark</item-->
<!--item name="colorAccent">#color/colorAccent</item-->
</style>
</resources>
The usual advice found online is to set the SDK version to 21 or higher. But I already tried that:
Set minSdkVersion to 21 in manifest
Set targetSdkVersion to 21 in manifest
Set taget framework to Android 5.0 in project settings
Cleaned and rebuilt project
The error is still there :-(
Are there other settings required to make this work?
This is what worked for my project.
In res/values-v21/styles.xml,
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="AppTheme" parent="#android:style/Theme.Material.Light.DarkActionBar">
<item name="android:colorPrimary">#color/primaryColor</item>
</style>
</resources>
In res/values/styles.xml,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/primaryColor</item>
</style>
</resources>
You should follow the convention directly from the Material Design documentation(https://developer.android.com/training/material/theme.html#ColorPalette):
<resources>
<!-- inherit from the material theme -->
<style name="AppTheme" parent="android:Theme.Material">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="android:colorPrimary">#color/primary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="android:colorPrimaryDark">#color/primary_dark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="android:colorAccent">#color/accent</item>
</style>
</resources>
What you are missing here is the android: namespace prefix to the colorPrimary item. Because of this, it cannot find the respective attribute as it's not defined in the scope.
Otherwise you would need to remove the android: prefix from the theme
parent="#android:style/Theme.Material.Light.DarkActionBar"
In my case, the colors used in styles.xml where not defined.
So,make sure u have defined all the colors as well as names properly.
In my case it were wrong entries in the SDK from an external partner i use:
they had style information in the SDK value.xml file
they also hat an android:theme part in their AndroidManifest.xml
which both are obviously not nessesary when you build an SDK (and not an app, where you need config about the themes and colors...)
The major issue people will face this because they forgot to add android before defining the colors
And
The defined colors not exists in your colors xml
<item name="android:colorPrimary">#fff</item>
<item name="android:colorPrimaryDark">#fff</item>
<item name="android:colorAccent">#fff</item>
Here I have defined colors directly if you use #color/color_primary like this the mentioned color_primary must exist in your color file.
What you are doing is declare that the colorPrimary of your style is the colorPrimary of your style... What does not have sense.
You should declare wich color is:
<style name="_AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#6c0e54</item>
If you want to define another colors and the call it on the style create a xml file like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="apptheme_color1">#9D3F86</color>
<color name="apptheme_color2">#84226C</color>
</resources>
and then you can do:
<style name="_AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/apptheme_color1</item>
Add this code in res\values\colors.xml
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
Now sync your project.
Is there a way how to generate style from XML attributes so I don't have to manually copy paste 1 by 1? When I prototype, I use the "inline" styles then I want to export styles into styles.xml but manually it is time consuming task. It is just different format, should be easy to automate.
<ImageView
android:layout_width="80dp"
android:layout_height="47dp"/>
into
<ImageView
style="#style/h2_image"/>
<style name="h2_image">
<item name="android:layout_width">80dp</item>
<item name="android:layout_height">47dp</item>
</style>
Very Simple.!
Just keep the cursor in the file from where you want to extract the style resource. Just a right click then click on refactor.
Refactor -> Extract -> style/layout.
Then give style a name and there you go..!
For more you can refer the documentation.
Try something like that:
<resources>
<style name="CustomImgView">
<item name="android:layout_width">80dp</item>
<item name="android:layout_height">47dp</item>
</style>
</resources>
<ImageView style="#style/CustomImgView" />
more information: https://developer.android.com/guide/topics/resources/style-resource.html
Try to Declare your Style like this :
<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="YourCustomStyle">
<item name="android:layout_width">"80dp"</item>
<item name="android:layout_height">"47dp"</item>
<!-- another attributes -->
<item name="customAttr">value</item>
</style>
</resources>
And in your xml add this android:theme="#style/YourCustomStyle"
I am trying to set the default background color of all my Activities to white, but it doesn't seem to work properly on Jelly Bean (comes out cyan). I'm thinking it might be something with the V4 appcompat FragmentActivity (as standard Activities seem to be fine).
What I have done in styles.xml is this:
<style name="AppTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:windowBackground">#android:color/white</item>
<item name="android:colorBackgroundCacheHint">#android:color/white</item>
<item name="android:colorBackground">#android:color/white</item>
</style>
In your resource folder under values create a new XML file called color.xml if it does not already exist. Then add a new colour to the colour xml file.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#FFFFFF</color>
</resources>
remove "android:" in your xml and it should turn to white.
<style name="AppTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:windowBackground">#color/white</item>
<item name="android:colorBackgroundCacheHint">#color/white</item>
<item name="android:colorBackground">#color/white</item>
</style>
Let me clarify the question.
I have theme like this
<style name="DefaultTheme" parent="android:Theme.Holo.Light">
<item name="android:textViewStyle">#style/DefaultTextViewStyle</item>
</style>
where
<style name="DefaultTextViewStyle" parent="android:Widget.TextView">
<item name="android:textColor">?attrTextColor</item>
</style>
for application I use the DefaultTheme theme, and I want to change value of the attrTextColor dynamically from code (to change text color for all TextView). Can I do it? How Can I do it?
For simple explication, you can do it with theme.
You have to define your attributes
<attr name="color_menu" format="color"/>
<attr name="color_text_main" format="color"/>
Define your theme color (here, blue and green)
<style name="AppTheme_green" parent="DefaultTheme">
<item name="color_menu">#color/very_dark_green</item>
<item name="color_text_main">#color/very_dark_gray3</item>
</style>
<style name="AppTheme_blue" parent="DefaultTheme">
<item name="color_menu">#color/very_dark_blue</item>
<item name="color_text_main">#color/very_dark_gray1</item>
</style>
Now your current activity can have juste one theme in your AndroidManifest.xml
<activity
android:theme="#style/AppTheme_green"
>
...
</activity>
But you can change it dynamically in your activity
activity.setTheme(R.style.AppTheme_Blue);
I hope this can help you
I have a quick question.
Is there a way, using eclipse, to define a default theme for my app, or do I have to configure each activity and asset to match my desired theme?
If there is a way, how do I go about doing that?
Define your android:theme in application element of your manifest.
Create an xml file in res\values. In it create a default layout style like this (you can define other stuff also this is just an example):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="#android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
Use one style for each default theme you want to create. Then in the Widget you want to use this style you enter like this (again just an example):
<TextView
style="#style/CodeFont"
android:text="#string/hello" />
This TextView will now inherit the settings declared in the style.