I'm trying to create custom themes for my app and therefore, would like the selector to pick up the right drawable based on the current theme. Unfortunately, doesn't look like any image gets picked up at all.
This is my selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<bitmap android:src="?attr/test_a"
android:gravity="center" />
</item>
<item android:state_focused="true">
<bitmap android:src="?attr/test_a"
android:gravity="center" />
</item>
<item android:state_activated="true">
<bitmap android:src="?attr/test_a"
android:gravity="center" />
</item>
<item>
<bitmap android:src="?attr/test_b"
android:gravity="center" />
</item>
</selector>
My attributes are defined in attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="test_a" format="reference"/>
<attr name="test_b" format="reference"/>
</resources>
And my theme has the following:
<style name="MyTheme" parent="Theme.AppCompat.NoActionBar">
<item name="test_a">#drawable/test_a_dark</item>
<item name="test_b">#drawable/test_b_dark</item>
</style>
<style name="MyTheme.Light">
<item name="test_a">#drawable/test_a_light</item>
<item name="test_b">#drawable/test_b_light</item>
</style>
The theme is applied in the manifest as required. I have seen some questions on SO around this saying that there's a problem Pre-Lolipop, however, I'm testing on Marshmellow.
Still not sure why this happens but in case this is useful for someone, as a workaround I did the following:
1) Created two selectors - one for the dark theme, pointing directly at the dark drawables, and one for the light theme
2) Created one attribute with type reference
3) Modified the theme to use that attribute point at the selector instead of the drawable
Related
I want to create a splash screen containing the app name. How can I add text to a layer-list?
This is my splash screen file at the moment:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#android:color/white"
android:gravity="fill_horizontal|fill_vertical" />
<item> <!--APP NAME--></item>
</layer-list>
I already tryed to create a png file with the app name but this caused some issues on different screen sizes.
I already tryed to create a png file with the text but it does not work for all screen sizes.
You can use a vector drawable (app_logo.xml) with your text as window background:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
<item android:drawable="#android:color/white"/>
<item
android:drawable="#drawable/app_logo"
android:gravity="center" />
</layer-list>
Setting theme:
<style name="NewSplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/background_splash</item>
</style>
This will work correctly on API level >= 23, you should provide an alternative version of background_splash with raster graphics for previous API levels (see #Venky's answer).
You can add an image in layer list
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/gray"/>
<item>
<bitmap
android:gravity="center"
android:src="#mipmap/ic_launcher"/>
</item>
</layer-list>
In theme.xml
<style name="NewSplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/background_splash</item>
</style>
Don't forget to create an image with App Name
I'm trying to define a custom color for my app's theme.
Here is how I do:
define custom attribute:
<declare-styleable name="ApplicationStyle">
<attr name="colorWeekdaysBg" format="color"/>
</declare-styleable>
Define an application style:
<style name="ApplicationStyle" parent="Theme.AppCompat.NoActionBar">
<item name="android:editTextStyle">#style/EditTextStyle</item>
<item name="colorPrimaryDark">#color/dark_blue</item>
<item name="colorPrimary">#color/dark_blue</item>
<item name="colorAccent">#color/dark_blue</item>
<item name="colorWeekdaysBg">#color/access_weekdays</item>
</style>
Set style in the manifest:
<application
android:name=".App"
android:icon="#drawable/icon_launcher"
android:label="#string/app_name"
android:theme="#style/ApplicationStyle">
Use this attribute in the drawable xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="-1dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="?colorWeekdaysBg" />
<corners
android:bottomLeftRadius="100dp"
android:topLeftRadius="100dp" />
</shape>
</item>
</layer-list>
But for some reason it does not apply my color to the drawable. It applies a transparent color instead.
One more strange thing is if I replace my ?colorWeekdaysBg with ?colorAccent, which is defined in the Theme.AppCompat, then it applies correct color.
So finally the question: Do you have any idea why it doesn't work, and how to fix it?
As per answer give by mudit here it is a bug in the pre-lollipop OS.
It is reported in Google's Issue Tracker.
In the devices which have Android Lollipop and above OS version it will work fine.
Check below code:
attr.xml
<declare-styleable name="AppTheme">
<attr name="colorWeekdaysBg" format="color|reference" /> <!-- Or you can keep color only. It will be ok -->
</declare-styleable>
You have to set the attribute in the v21 style file. Might be you missed this one.
v21\styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:splitMotionEvents">false</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:navigationBarColor">#color/black</item>
<item name="colorWeekdaysBg">#color/access_weekdays</item>
</style>
Create two drawable files to get it work on all devices. One for normal i.e. pre-lollipop version and another for Lollipop and above i.e. v21 drawable.
your_drawable.xml In this you have to set color from the color file.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="-1dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#color/access_weekdays" />
<corners
android:bottomLeftRadius="100dp"
android:topLeftRadius="100dp" />
</shape>
</item>
</layer-list>
v21\your_drawable.xml In this file you can set custom attribute like you did.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="-1dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="?colorWeekdaysBg" />
<corners
android:bottomLeftRadius="100dp"
android:topLeftRadius="100dp" />
</shape>
</item>
</layer-list>
your_layout.xml In your layout you can give that drawable as below
<Button
android:id="#+id/btnTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/your_drawable"
android:text="Test Text" />
Now check the answers of your questions.
But for some reason it does not apply my color to the drawable. It applies a transparent color instead.
Because you may not have defined your attribute in v21 style.xml file
One more strange thing is if I replace my ?colorWeekdaysBg with ?colorAccent, which is defined in the Theme.AppCompat, then it applies the correct color.
Android Studio displays color in preview but when you will run the application and reached on that screen your application will be crash with the Error inflating class error
android.view.InflateException: Binary XML file line #68: Error inflating class Button
So finally the question: Do you have any idea why it doesn't work, and how to fix it?
I think now you have got the idea that why it doesn't work and how to fix it.
I am trying to implement splash screen without any extra activities, using theme call in the manifest file's <activity/> tag.
In my styles.xml
<style name="splashTheme" parent="AppTheme">
<item name="android:windowBackground">#drawable/splash</item>
</style>
here the drawable file splash.xml uses layer-list
How do I add text to this layer list?
One way to add Texts in your drawable layer-list is by creating a png file of the text and adding it using bitmap. Here is one example of it.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/background"/>
<item android:bottom="150dp">
<bitmap
android:gravity="center"
android:src="#drawable/logo"/>
</item>
<item android:top="60dp">
<bitmap
android:gravity="center"
android:tint="#android:color/white"
android:src="#drawable/text"/>
</item>
</layer-list>
I have a style for button like : http://ace-subido.github.io/css3-microsoft-metro-buttons/buttons.html (See Stripped button).
But I want to be able to use this style for different colors without recreating 4 xml files (a selector drawable + 3 button states drawables with layer list).
In a btn_stripped_orange.xml (and 2 other xml for different button states) I have a color
With this syntax, evrything is ok :-)
But instead of creating 4 new xml files for every different colors, I want to "pass a color" to btn_stripped_orange.xml (so I should rename it to btn_stripped.xml).
In style.xml I have to call #drawable/btn_stripped_orange_selector
after this btn_stripped_orange_selector call
I think I have to pass the border_color variable to the style in styles.xml with call #drawable/btn_stripped_orange_selector
I want to just create new style with the #drawable/btn_stripped_orange_selector as android:background (and rename it) and the color varaible
This is the code that I'm trying to do (no error in eclipse, but app crash on run), If I replace ?attr/border_color with a color #color/orange for exemple, it works perfectly. I think it's just a syntax problem.
My themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="btn_orange" parent="#style/btStripped1">
<item name="border_color">#attr/border_color</item>
</style>
<attr name="border_color" format="color" />
</resources>
My btn_stripped_orange_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/btn_stripped_orange_press" />
<item android:state_focused="true" android:drawable="#drawable/btn_stripped_orange_focus" />
<item android:drawable="#drawable/btn_stripped_orange" />
</selector>
My btn_stripped_orange.xml (nearly the same than _focus and _press) :
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="?attr/border_color" />
</shape>
</item>
<item android:left="5dp">
<shape android:shape="rectangle">
<solid android:color="#EEEEEE" />
</shape>
</item>
<item android:left="5dp">
<shape android:shape="rectangle">
<solid android:color="#EEEEEE" />
</shape>
</item>
</layer-list>
Finally in my styles.xml
<resources>
.....
<style name="btStripped1">
<!-- I want to pass a color variable here -->
<item name="android:background">#drawable/btn_stripped_orange_selector</item>
<item name="android:padding">6dp</item>
<item name="android:textColor">#android:color/black</item>
<item name="android:shadowColor">#android:color/white</item>
<item name="android:shadowDx">1</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">1</item>
</style>
....
</resources>
I want a pure xml solution but if it's impossible and you have a solution with a bit of java code, your're welcome !
Thank you very very much for your help and sorry for average english !
I'm trying to use a color defined in a stlyle in a selector but it is causing a Resources$NotFoundException.
First I added a new attribute to attr.xml:
<resources>
<attr name="unread_background" format="color" />
</resources>
Then I defined that attr value in styles.xml:
<style name="ThemeNoTitleBar" parent="android:Theme.NoTitleBar">
<item name="unread_background">#000000</item>
</style>
Then I tried to use that attr in my selector definition:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- other states snipped -->
<item android:state_selected="false"
android:drawable="?unread_background" />
</selector>
Lastly, the activity uses the ThemeNoTitleBar style theme in the manifest.
I've also tried creating a color in colors.xml and having it use the new attr but that also fails.
I'm obviously missing something but am not sure what to do to fix it. My intent is to create multiple themes and have the selector use the color in the currently selected theme.
<item android:state_selected="false"
android:drawable="?unread_background" />
this above section is wrong.
the drawable only take a reference to a drawable resource.
Please see this link. http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Here is something, that works by me.
attrs.xml:
<attr name="color_selection" format="reference"/>
styles.xml, as child of main theme:
<item name="color_selection">#color/selection_background_inverse</item>
shape_background_selected.xml in drawable folder:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="?attr/color_selection"/>
</shape>
your selector file, in my case: selector_background_recyclerview:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/shape_background_selected" android:state_activated="true" />
<item android:drawable="#drawable/shape_background_selected" android:state_pressed="true" /> <!-- pressed -->
<item android:drawable="#color/transparent" /> <!-- default -->
</selector>
finally, in your view's xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/selector_recyclerview_item_background"../>
Android button with different background colors Take a look onto the example. It looks like you need that.