Can't access android/drawable Icon-Resource - android

I'm creating a Optionsmenu, for that I use the Android Icon-Resource. But I can't access them, what am I doing wrong. What do I need to change?
Here's the Code of my menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/Videos"
android:icon="#android/drawable/ic_menu_view"
android:title="Videos" />
<item android:id="#+id/Bio"
android:icon="#android/drawable/ic_menu_friendlist"
android:title="Biographie" />
<item android:id="#+id/Support"
android:icon="#android/drawable/ic_menu_star"
android:title="Support" />
</menu>
thank you for the help in advance!

You are almost correct, when accessing framework resources you have to prefix the resource type with android: since you are using the android package here. Which means it should not be
android:icon="#android/drawable/ic_menu_view"
but
android:icon="#android:drawable/ic_menu_view"
instead for example (note the colon between android and drawable instead of a forward slash).
Also note that you can't access some resources, since they are not public, such as the ic_menu_star you are using here. You have to copy them to your projects drawable folders, which means you have to access them via a normal #drawable/ic_menu_star after doing that. See this question for reference.

try :
android:icon="#android:drawable/ic_menu_view"
instead of :
android:icon="#drawable/ic_menu_view"

Try this:
<item android:id="#+id/Videos"
android:icon="#drawable/ic_menu_view"
android:title="Videos" />
<item android:id="#+id/Bio"
android:icon="#drawable/ic_menu_friendlist"
android:title="Biographie" />
<item android:id="#+id/Support"
android:icon="#drawable/ic_menu_star"
android:title="Support" />
I think #android/drawable/ is incorrect. You should use instead #drawable/
You can see it: http://developer.android.com/guide/topics/ui/menus.html
Hope this helps...

Related

attribute is missing the android namespace prefix

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>

How to reference style attributes from a drawable?

I want to have 2 selectable themes for my application. In order to do that, I defined some attributes, like this:
<attr format="color" name="item_background" />
Then, I created both themes, like this:
<style name="ThemeA">
<item name="item_background">#123456</item>
</style>
<style name="ThemeB">
<item name="item_background">#ABCDEF</item>
</style>
This method works great, allowing me to create and modify several themes easily. The problem is that it seems that it can be used only in Views, and not in Drawables.
For example, referencing a value from a View inside a layout works:
<TextView android:background="?item_background" />
But doing the same in a Drawable doesn't:
<shape android:shape="rectangle">
<solid android:color="?item_background" />
</shape>
I get this error when running the application:
java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
If instead of ?item_background I use a hardcoded color, it works, but that doesn't allow me to use my themes. I also tried ?attr:item_background, but the same happens.
How could I do this? And why does it work in Views but not in Drawables? I can't find this limitation anywhere in the documentation...
In my experience it is not possible to reference an attribute in an XML drawable.
In order to make your theme you need to:
Create one XML drawable per theme.
Include the needed color into you drawable directly with the #color tag or #RGB format.
Make an attribute for your drawable in attrs.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Attributes must be lowercase as we want to use them for drawables -->
<attr name="my_drawable" format="reference" />
</resources>
Add your drawable to your theme.xml.
<style name="MyTheme" parent="#android:style/Theme.NoTitleBar">
<item name="my_drawable">#drawable/my_drawable</item>
</style>
Reference your drawable in your layout using your attribute.
<TextView android:background="?my_drawable" />
Starting with lollipop (API 21) this feature is supported, see
https://code.google.com/p/android/issues/detail?id=26251
However, if you're targeting devices without lollipop, don't use it, as it will crash, use the workaround in the accepted answer instead.
Although it's not possible to reference style attributes from drawables on pre-Lollipop devices, but it's possible for color state lists. You can use AppCompatResources.getColorStateList(Context context, int resId) method from Android Support Library. The downside is that you will have to set those color state lists programmatically.
Here is a very basic example.
color/my_color_state.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="?colorControlActivated" />
<item android:color="?colorControlNormal" />
</selector>
A widget that needs a color state list:
<RadioButton
android:id="#+id/radio_button"
android:text="My Radio" />
And the most important:
ColorStateList csl = AppCompatResources.getColorStateList(context, R.color.my_color_state);
RadioButton r = (RadioButton) findViewById(R.id.radio_button);
r.setTextColor(csl);
Well, not the most elegant or shortest way, but this is what Android Support Library does to make it work on older versions (pre-Lollipop) of Android.
Unfortunately, the similar method for drawables doesn't work with style attributes.
I answered the same question in https://stackoverflow.com/a/59467269/3841352 but i will post it here as well:
I encountered the same problem and as of 2019 it hasn't been resolved so you can't have an attribute referenced in a selector as a drawable. I will share the solution I got for the problem as I don't see it posted in here. I found it in the last comment of the bug report.
The workaround is basically create a drawable resource that will be the one referring the attribute value.
To illustrate your case the solution would be instead of:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="?attr/colorPrimary" android:state_enabled="true" android:state_window_focused="false"/>
<item android:drawable="?attr/colorPrimaryDark" android:state_pressed="true"/>
<item android:drawable="#android:color/darker_gray" android:state_enabled="false"/>
<item android:drawable="?attr/colorPrimary"/>
</selector>
you would replace the ?attr/* for a drawable resource:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/colorPrimaryDrawable" android:state_enabled="true" android:state_window_focused="false"/>
<item android:drawable="#drawable/colorPrimaryDarkDrawable" android:state_pressed="true"/>
<item android:drawable="#android:color/darker_gray" android:state_enabled="false"/>
<item android:drawable="#drawable/colorPrimaryDrawable"/>
</selector>
The drawables would be defined as:
drawable/colorPrimaryDrawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle">
<solid android:color="?attr/colorPrimary" />
</shape>
drawable/colorPrimaryDarkDrawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle">
<solid android:color="?attr/colorPrimaryDark" />
</shape>
Hope it helps!!
As #marmor stated this is now supported on API 21. But for those us who need to support legacy versions of Android, you can use this feature. Using the v7 support library you can still use it on apps with minimum SDK level all the way down to 7.
The AppCompatImageView in the v7 Android Support Library has a bug free implementation of this feature. Simply replace your usages of ImageView with AppCompatImageView.

Android: Custom spinner help

I am following this tutorial on how to create custom spinners. Near the begining it instructs you to navigate to: *android.jar\res\drawable-finger* But for some reason I can't find the "drawable-finger" folder in my android.jar. I thought it may have to do with the API level so I changed it from 1.6 to 2.1 to no avail. I went around this and put them in the drawable folder thinking I could just work around it but then the spinner has the image stretched and malformed instead of repeating the image in the x direction. Any ideas?
Thanks!
EDIT: Here is the code
Spinner
<Spinner android:id="#+id/catspinner" android:layout_marginLeft="15dip" android:layout_marginRight="15dip"
android:layout_width="fill_parent" android:layout_centerHorizontal="true"
android:layout_height="wrap_content" android:layout_centerVertical="true"
android:drawSelectorOnTop="true" android:background="#drawable/spinnerbackground"/>
code for the spinner background:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/myspinner_select" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/myspinner_press" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/myspinner_press" />
<item android:drawable="#drawable/myspinner_normal" />
</selector>
it looks like this
Sherif is correct. As far as following the tutorial, it doesn't (and shouldn't) suggest that you put the images back in android.jar/res/drawable-finger. You should put your versions of those images in your drawable folders (i.e. drawable-ldpi, drawable-mdpi, etc).
I suspect that the issue with the image being stretched and not repeated is a result of your code or image, not the location you put the images. You could post your code and we might be able to help with that.
I also noticed that the tutorial looks like it is based off of a pre-release Android SDK version. I'm guessing that the drawable-finger folder no longer exists. It's also possible that other elements of the tutorial are outdated.

Customize android spinner

I am trying to customize spinner. I found this tutorial, http://www.gersic.com/blog.php?id=57
It looks great but I am finding some errors all of which is like the following
error: No resource identifier found for attribute 'state_dropdown_showing' in package 'android'
and the xml which leads to this error is
<!-- DROPDOWN SHOWING-->
<item android:state_first="true"
android:state_dropdown_showing="true"
android:drawable="#drawable/btn_dropdown_down"
/>
<item android:state_middle="true"
android:state_dropdown_showing="true"
android:drawable="#drawable/btn_dropdown_up_down"
/>
<item android:state_last="true"
android:state_dropdown_showing="true"
android:drawable="#drawable/btn_dropdown_up"
/>
<item android:state_single="true"
android:state_dropdown_showing="true"
android:drawable="#drawable/btn_dropdown_neither_up_down"
/>
<!-- DROPDOWN NOT SHOWING-->
<item android:state_first="true"
android:state_dropdown_showing="false"
android:drawable="#drawable/btn_dropdown_right_only"
/>
<item android:state_middle="true"
android:state_dropdown_showing="false"
android:drawable="#drawable/btn_dropdown_left_right_collapsed"
/>
<item android:state_last="true"
android:state_dropdown_showing="false"
android:drawable="#drawable/btn_dropdown_left_only"
/>
<item android:state_single="true"
android:state_dropdown_showing="false"
android:drawable="#drawable/btn_dropdown_neither"
/>
And I am not finding anything in the net relevant to this. I am at a fix now. I want to change the look and feel of the regular spinner with nice color background.
Anybody please guide me about customization of spinner.
android:state_dropdown_showing it is worked on after r15 version only..so update the android sdk tools version.it is hopefully helpful to all

android custom button shape

I am a beginner. So I request you to be patient with me.
I am trying to achieve a custom shape for the button instead of the usual rectangular one.
Is there any other way to do this than setting it as background for a button?
Also i am trying to use different colours for different states for the button. Towards this I have created this file:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="#android:drawable/my_button_background_focus_blue" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#android:drawable/my_button_background_pressed_blue" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#android:drawable/my_button_background_pressed_blue" />
<item android:drawable="#android:drawable/my_button_background_normal" />
</selector>
Do I include this inside the main.xml or create another xml file? If so, where do I create it?
Thank you.
Is there any other way to do this than setting it as background for a button?
There are more ways, but this is the easier one. You could, for instance, create your own buttons implementing a custom View, but it does not worth the effort.
Do I include this inside the main.xml or create another xml file? If so, where do I create it?
You have to create a new XML file. You call it as you want, and place it in the res/drawable folder. Once you have done so, you can reference that file from XML or programatically:
<Button
android:background="#drawable/the_name_of_the_xml"/>
Or...
button.setBackgroundResource(R.drawable.the_name_of_the_xml);

Categories

Resources