I'm developing an android project, and I want to use a <selector> in an XML file. I've put the <selector> in a file under res/drawable/default_item_background_selector.xml, and I'm referencing it with XML attributes like
<TextView
android:background="#drawable/default_item_background_selector"/>
The XML attribute content I get from Eclipse's content assist, so that can see it just fine. However, when I compile everything (and it compiles just fine) and debug it on either simulator or on a device, the app crashes, with a root exception of :
09-24 23:55:14.771: E/AndroidRuntime(22478): Caused by:
android.content.res.Resources$NotFoundException: File
res/drawable/default_item_background_selector.xml from drawable
resource ID #0x7f020000
The R.drawable.default_item_background_selector gets generated just fine, but at runtime, it appears there's no physical file generated in the output directory. Has anybody experienced this before ? Yes, I've cleaned and recompiled (so many times).
Created default_bg.xml in Drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true"
android:drawable="#drawable/red" />
<item android:state_pressed="true" android:state_selected="false"
android:drawable="#drawable/blue" />
<item android:state_selected="false"
android:drawable="#drawable/yellow" />
</selector>
Create drawables in **strings.xml** is
<drawable name="blue">#0000FF</drawable>
<drawable name="red">#FF0000</drawable>
<drawable name="yellow">#FFFF55</drawable>
And set background to textview
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:background="#drawable/default_bg"
/>
Related
xamarin.forms has a tutorial for creating splash screens in android
This code is my result from following the tutorial and it works
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#color/splash_background"/>
</item>
<item>
<bitmap
android:src="#drawable/monstersplash"
android:tileMode="disabled"
android:gravity="fill_vertical|fill_horizontal"/>
</item>
</layer-list>
But now I also want my image to aspect-fit on the page.
I found a number of posts suggesting the following code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/splashlinearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/monstersplash"
android:adjustViewBounds="true"
android:scaleType="fitCenter" ></ImageView>
</LinearLayout>
But this code results in a ResourcesNotFoundException: android.content.res.Resources$NotFoundException: Drawable com.companyname.BB.App:drawable/splash_screen with resource ID #0x7f070141
I tried different configurations with layer-list /linear layout but somehow the Imageview does not seem to be able to load my monstersplash.png image while the source parameter is the same as with the bitmap.
Any idea's?
Full context:
The above mentioned code lives in drawable/splash_screen.xml
in my styles.xml I have a reference to the drawable like so:
<style name="SplashTheme" parent ="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/splash_screen</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowActionBar">true</item>
</style>
And I created a splashactivity which references the theme
[Activity(Theme = "#style/SplashTheme", MainLauncher = true, NoHistory = true)]
public class SplashActivity : AppCompatActivity
{
Since it works within Bitmap. The issue maybe caused by the IDE or project cache.
Delete the folder bin and obj in your project ,then clean and
rebuild it.
Delete all content in the file Resource.design.cs , then rebuild the project .
If it still doesn't work , you could create a new project and check if the issue still exists .
Im creating an android app which uses a menu.xml file in the res folder. But I'm getting the above error. What does it mean? How can I fix it?
menu.xml:
<?xml version="1.0" encoding= "utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item id="#+id/my_location"
android:icon="#drawable/my_location"
android:title:="Current location" />
<item id="#+id/mapview_satellite"
android:icon="#drawable/satelliteview"
android:title="Satellite View" />
<item id="#+id/mapview_normal"
android:icon="#drawable/normalview"
android:title="Normal view" />
</menu>
Change <item id="#+id/my_location" to <item android:id="#+id/my_location". This in all three places.
Also, in here: android:title:="Current location" remove the colon after title.
You also need to make sure the following two lines are in your attributes list:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
If you omit those, Eclipse will just give you the "missing android prefix" error.
Believe it or not, for anyone who tries to work through examples in downloaded books, you may need to make sure you have true quotation marks as the compiler understands them. I'd been getting the same error as the OP, and many similar parse errors as I tried to modify an XML file in small ways. I noticed then that quote marks from the pasted code snippets were different from the ones on my keyboard (in the snippets they leaned to the right, and if I typed them myself they were straight up and down).
I know this sounds nuts and I could hardly believe it myself, but when I retyped my quotes it worked fine.
Adding android: before the attribute solved my problem.
create an XML file inside your project's res/menu/ directory Not in in res folder
&
dont forget to prefix the id attributes.
Try like this:
<?xml version="1.0" encoding= "utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/my_location"
android:icon="#drawable/my_location"
android:title="Current location" />
<item android:id="#+id/mapview_satellite"
android:icon="#drawable/satelliteview"
android:title="Satellite View" />
<item android:id="#+id/mapview_normal"
android:icon="#drawable/normalview"
android:title="Normal view" />
</menu>
Some times the XML code might be written correctly so you need to simply cut what you currently have and then delete it from the line numbers then re-paste it and the error should be fixed. If it doesn't then you know your code is wrong. The xml default is to recognize what you have put at first and needs to be removed in order to begin again. Trust me I just spent 3 days on that very problem and did exactly what is said above, good luck.
If you have something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="person" type="com.abc.PersonEntity"/>
</data>
Move your data inside layout tag:
<layout xmlns:android="http://schemas.android.com/apk/res/android" >
<data>
<variable name="person" type="com.abc.PersonEntity"/>
</data>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
...
You've forgotten to prefix the id attributes. Try to do it like this:
<?xml version="1.0" encoding= "utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/my_location"
android:icon="#drawable/my_location"
android:title="Current location" />
<item android:id="#+id/mapview_satellite"
android:icon="#drawable/satelliteview"
android:title="Satellite View" />
<item android:id="#+id/mapview_normal"
android:icon="#drawable/normalview"
android:title="Normal view" />
</menu>
there was two error silly error
- id isn't it attribute is android:id
- First itme android:title:= you have written extra colon after title
here is implemented code
<?xml version="1.0" encoding= "utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/my_location"
android:icon="#drawable/my_location"
android:title="Current location"/>
<item
android:id="#+id/mapview_satellite"
android:icon="#drawable/satelliteview"
android:title="Satellite View"/>
<item
android:id="#+id/mapview_normal"
android:icon="#drawable/normalview"
android:title="Normal view"/>
</menu>
I want to make a rollover with android.
I saw that it is necesarry to write a XML like :
<?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/verde" />
<item android:drawable="#drawable/rojo" />
</selector>
I saved it with the name button.xml.
I don't know in which folder save it.
Then I tried to used the reference in my layout.xml
I change the background attribute for my button in my layout.xml from
android:background="#drawable/red"
and use the
android:background="#drawable/button"
Save button.xml in the folder res/drawable if it's applicable for all screen sizes/devices, otherwise put it in the folder with the appropriate qualifier suffix (e.g. drawable-hdpi, drawable-ldpi etc).
See here (http://developer.android.com) for a full example, and explanation of other states you could include.
I have a question about using references in a layer-list drawable.
I want to use a custom button in my app, made of a layer-list.
That is the final drawable btn.xml for the button, made of a selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/btn_normal"/>
</selector>
The pressed-state-drawable btn_pressed looks like that:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="?custom_color" />
</shape>
</item>
<item android:drawable="#drawable/btn_normal"/>
</layer-list>
The essential part is the line, where i declare the color to be a reference to custom_color, that i define as follows.
attrs.xml:
<resources>
<attr name="custom_color" format="reference|color" />
</resources>
themes.xml:
<resources>
<style name="MyTheme" parent="android:Theme.Light.NoTitleBar">
<item name="custom_color">#ff33b5e5</item>
</style>
</resources>
Eclipse gives me no errors at all, and it compiles just fine.
But when i start the application on my ICS Nexus S, or the Emulator (no matter what version), it crashes. Logcat points the following out:
03-23 14:33:38.832: E/AndroidRuntime(636): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/btn.xml from drawable resource ID #0x7f020006
[...]
03-23 14:33:38.832: E/AndroidRuntime(636): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/btn_pressed.xml from drawable resource ID #0x7f020009
If I comment out, the shape-element in the layer-list, or set the color hard-coded, everything works. So there seems to be a problem referencing attributes in a layer-list.
So, does anyone know a solution to this problem? I want to change colors in that layer-list, depending on the theme, that my application is using.
Ok, seems like this is a bug that wasn't looked at until Android L.
More details on the Android Issue Tracker: Issue 26251
It should be fixed in Android L, but at least testing with the Android L Preview in the Emulator it doesn't seem to work completely, yet. At least it won't crash the app, but instead of showing the correct color, it just gives me transparency (#00ffffff). Maybe this isn't the case on the devices and/or once Android L stable is released.
Try android:color="#color/custom_color"
I'm struggling with the Android ToggleButton because I try to change the green indicator light. In the Android SDK folder I've found the file drawable\btn_toggle_bg.xml where it says
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+android:id/background" android:drawable="#android:drawable/btn_default_small" />
<item android:id="#+android:id/toggle" android:drawable="#android:drawable/btn_toggle" />
</layer-list>
I copied this file to the drawable folder of my project and changed the last item to
<item android:id="#+android:id/toggle" android:drawable="#drawable/btn_toggle" />
and then took the original btn_toggle.xml from the Android SDK, copied into the drawable folder, too. btn_toggle.xml is this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#drawable/btn_toggle_off" />
<item android:state_checked="true" android:drawable="#drawable/btn_toggle_on" />
</selector>
So then I asumed that it would be necessary to create btn_toggle_off.png and btn_toggle_on.png. At last I added the line
<ToggleButton
...
android:background="#drawable/btn_toggle_bg" />
but finally, the ToggleButton looks completely strange.
What I noticed are files called btn_toggle_off.9.png and btn_toggle_on.9.png in the SDK but I couldn't find a reference to these files although they look exactely like the original indicator.
Can you help me? :)
Without completely re-styling the toggle button widget, the resources you are trying to replace should probably be nine patch PNGs like the originals are.
Copy btn_toggle_off.9.png and btn_toggle_on.9.png into your project and modify them, or use the draw9patch tool to properly add the nine patch metadata to your own images.
Refer to the following documentation for an explaination of Android's nine patch drawables:
http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
http://developer.android.com/guide/developing/tools/draw9patch.html
The ".9" part of the filename must be retained, but is not referenced in XML resources. So btn_toggle_on.9.png is referred to as simply "#drawable/btn_toggle_on".