I find myself again trying to use resources set in the current theme inside my Android application and running into difficulties.
Following the rough guidelines found here for theme attribute value reuse
I was originally trying to create a selector like so
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="?android:attr/selectableItemBackground" />
<item android:drawable="#color/transparent" />
</selector>
where the pressed state drawable is declared like so
<item name="selectableItemBackground">#android:drawable/item_background</item>
in the current ICS themes xml doc themes xml doc . This compiles fine in eclispse (whereas if i spell the attr wrong it will give an error or reffng #android:drawable/item_background directly will inform me the drawable itself is private).
When i run the app i get a
FATAL EXCEPTION: main
E / AndroidRuntime (18815): android.view.InflateException: Binary XML file line #14: Error inflating class <unknown>
without any other useful information apart from the xml file that is using this selector for an ImageViews android:background attribute value. I know that there is a bug where colorStateLists cant be used as backgrounds as mentioned here and maybe this also applys to drawableState lists referencing drawables from the theme. I know however it does work as the post mentioned describes.
Am i missing something here?? I always run into something i dont understand when trying to do stuff like this so would be glad of any pointers.
Thanks for any help
EDIT
as another experiement I tried setting an alias to the referenced drawable in the theme like
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="?android:attr/selectableItemBackground" ></bitmap>
and using this as the background directly but this does not work either. Looking at aliases they are onlyment to ref actual images so this makes sense
EDIT 2
another test an I have found using it directly
android:background="?android:attr/selectableItemBackground"
does actually work. So it seems that using it inside a alias or selector is where it gets unhappy. Strange as i would assume that both background and the same arg
EDIT 3
It seems the theme attr i was pointing at was actually a selector instelf, which im sure didnt help the issue! Thought i wouldve guessed looking at the name of it
I guess i answered the question myself in my edits - main issue i imagine was that i was trying to ref a selector inside a selector.
Related
I am trying to follow a simple tutorial online about making a toggle button in android with images (so it shows images instad of ON/OFF).
It doesn't work. The error I get is
- Binary XML file line #1: <item> tag requires a 'drawable' attribute or child tag defining a drawable
I am using Visual Studio 2019 and coding with Xamarin.
I have a toggle button defined like this in my activity_main.xml:
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/toggleButton1"
android:background="#drawable/toggleButton_emp"
android:id="#+id/toggleButtonEmp"/>
Then I have a "toggleButton_emp.xml" file in "Resources\drawable" which contains:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/faction4" android:state_checked="true"/>
<item android:drawable="#drawable/faction3" android:state_checked="false" />
</selector>
faction4 and faction3 are valid drawables I have added to "Resources\drawable"
I have found many answers for this issue that are most 10 years old, and none help. They often say to make "drawable" first tag but it's already first. Or removing spaces but I don't have any.
Okay so in this case it turned out the problem was that apparently either Android or Visual Studio does not accept .svg format (I assume the former). But there is no error message (format not supported?), no notice, nothing. So I assumed at this day an age, it should either accept it or barf in some way.
The error message I got was also not indicative of real problem in any way. It said that there is no drawable tag, while there clearly was one. So there was no way to guess drawable itself is the culprit.
Anyways, replacing ".svg"s with ".png"s fixed the issue.
I've been trying to follow this answer for adding an animation to a dialog box. However I'm getting an error on the first style addition.
I've added the following to res/values/styles.xml (not completely sure if this is correct)
<style name="DialogAnimation">
<item name="android:windowEnterAnimation">#anim/slide_down_dialog.xml</item>
<item name="android:windowExitAnimation">#anim/slide_out_up</item>
</style>
The first item, android:windowEnterAnimation, does not seem to exist when I tab complete android:. This also applies to android:windowExitAnimation. The error I receive in the XML file is:
error: Error: No resource found that matches the given name (at 'android:windowEnterAnimation' with value '#anim/slide_down_dialog.xml').
All the questions I've found regarding animation like this uses windowEnter/ExitAnimation. I looked at the android docs and it supposedly has this attribute, but I cannot for the life of me get it to appear.
Thanks in advance for any help/advice. If this is a duplicate please point me in the right direction, I couldn't find any related questions.
you sholud put # letter before android and remove the .xml like:
<item name="#android:windowEnterAnimation">#anim/slide_down_dialog</item>
I think it is:
remove (.xml) to be like (#anim/slide_down_dialog)
To start with some context, I'm trying to style the background color of a SearchView widget. A really insightful so answer to this problem has already been posted, and I learned immensely from it.
There is one gap in my understanding though, and I'm hoping someone can explain it to me. When I create a theme, such as the following:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyCustomTheme" parent="android:Theme">
<item name="android:searchViewTextField">#android:color/white</item>
</style>
</resources>
Eclipse compiles with an error saying it doesn't know about the attribute:
error: Error: No resource found that matches the given name: attr 'android:searchViewTextField'.
However, if I re-declare the attribute:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<declare-styleable name="CustomSearchView">
<attr name="android:searchViewTextField" format="reference" />
</declare-styleable>
</resources>
Eclipse responds with an error saying:
error: Attribute "android:searchViewTextField" has already been defined
Eclipse seems to be aware of the attribute, but conveniently forgets about that attribute when I want to use it. (I wonder if there is some context switching going on in the background)
At any rate, if I delete the problem code then I can see my custom theme inheriting searchViewTextField from its parent. I just don't understand why I can't supplant it with my own.
(The other answer mentions android.R.stylable, but that file is obsolete in api 16)
Thanks in advance.
Relevant Android sources:
themes.xml, attrs.xml, and search_view.xml (sorry, two link limitation).
The answer in the SO answer you linked explicitly says that you cannot specify searchViewTextField in your own theme because it is not a stylable resource. You need to modify it's value in code as per the other answer.
The question was, why can't I use the theme attribute "android:searchViewTextField"?
Out of suspicion that the compiled Android.jar file was not in tune with the published Android source, I ripped open the jar and started decompiling (a first for me). After poking around, it does appear that someone went into R$style.class with a machete and hacked out a massive chunk of the resources. As far as I can ascertain, it must have been done deliberately and perhaps even manually.
This kind of compiled source modification is not without precedent in other similar frameworks, but it sure as hell is confusing to anyone trying to debug the framework.
Ironically, auto-generated R files all have a header that says,
/* AUTO-GENERATED FILE. DO NOT MODIFY.
Kinda pointless when you make it routine procedure to modify the files.
I've got the following selector defined in button_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:state_enabled="true"
android:drawable="#drawable/button_settlement_background_pressed" />
<item android:state_enabled="true"
android:drawable="#drawable/button_settlement_background_normal" />
<item android:state_enabled="false"
android:drawable="#drawable/button_settlement_background_disabled" />
</selector>
When I run lint I get the following warning: Unexpected text found in layout file: "". It says it's happening at line 4 in "drawable". All of the referenced drawables exist in /res/drawable.
Does anyone know what could be causing this? I can ignore the warning but I'd rather fix it if possible.
Also, I get warnings for unused strings and icons when they're only referenced in AndroidManifest.xml. Is there a way to fix those instead of ignoring them?
Using Project>Clean.
Fixed that problem in my case.
Perhaps there's an invisible character somewhere in the white space. I'd try recreating the xml from scratch (not using copy-and-paste, which would just copy the problem, if that's what's happening). I'd also clean and rebuild the project.
As far as the unused resource warnings, I don't think there's a way to control this. The problem of false positives is hugely worse for library projects. Android lint is a fairly new tool and (in my opinion) still has a lot of rough edges.
You can also Format the xml file (CTRL + SHIFT + F). The extra characters would be displayed.
typically, this would happen when you inserting something like "android:id=" without a newline and using eclipse's auto completion, like this:
<LinearLayout
android:id= >
^
here type enter, the code would appears to be like this:
<LinearLayout
android:id="#+id/
the character '>' has been erased and '"' become invisible.
use ctrl+shift+f to format the file to find the hidden characters.
I found that if I closed the file I had the hidden character on, then reopened the same file. The character was no longer hidden (and typically was just after the />, for example />" ).
Analyze -> Inspect code
It worked for me!
In XML file, tags contain attributes. consider the line: (last ">" should not be there) ...
<selector xmlns:android="http://schemas.android.com/apk/res/android">
Make sure that the tag on first line does not contain">".
Replace above code with:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
...
</selector>
Now other elements are under selector tag. I hope this helps.
I am attempting to run the HelloTabWidget example from here:
http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
I am able to build, but once it runs it force closes. I ran the debugger in Eclipse and it looks like the error I am getting is in the res/drawable/ic_tab_artists.xml file.
The error I am getting is:
"org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: tag requires a 'drawable' attribute or child tag defining a drawable"
This is the xml I have for it, pretty straightforward:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http//schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="#drawable/ic_tab_artists_grey" android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="#drawable/ic_tab_artists_white" />
</selector>
I'm just confused because the drawable attribute is there...any ideas? The code/XML I have in my implementation is verbatim what they have in the examples, but it just won't run in the emulator.
<selector xmlns:android="http//schemas.android.com/apk/res/android">
You are missing a colon after http. Perhaps that is the source of the error?
I'm just confused because the drawable attribute is there...any ideas
The only thing I came come up with on why you would get that error is that there's a typo in xmlns:android="http//schemas.android.com/apk/res/android" or a typo in android:drawable, but I don't see a typo in either.
If there were a typo in the xmlns:android= decl, then the android:drawable attribute in item wouldn't be the right drawable attribute, so the run-time would say it isn't there.
If there were a typo in android:drawable, then the run-time error is right: there literally is no drawable attribute in item.