Overcome resource/.axml difficulties in HelloWorld Xamarin Android app - android

I am starting out in Xamarin to create an Android app for the first time, so I am following the most basic Xamarin Android tutorial. I get to the point where I am editing my Strings.xml file for my UI controls. I do so, and compile, and get:
C:\...\main.xml(0,0): Error: No resource found that matches the given name (at 'text' with value '#string/hello'). (AndroidHello)
I don't have a main.xml, but I do have a Main.axml, which was apparently generated with the solution. Am I expected to edit this file to match my .cs files? That doesn't seem right at all. Or should it be re-generated from the .cs somehow?
I searched around to try to understand more about .axml files, so I understand that they can be used to specify a layout, instead of doing so in C# code. So I thought I could just delete the .axml file and rely on C# only--but then my Resource.String.whatever resources that I specified in Strings.xml won't work when referenced from my C# code.
Strings.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="helloButtonText">Say Hello</string>
<string name="helloLabelText">Hello Xamarin.Android</string>
</resources>
As mentioned, the .axml file references strings that no longer exist. How should I resolve that?

You probably have the following Button defined in the main.axml file:
<Button
android:id="#+id/MyButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Hello" />
Replace the #string/Hello and you should be good to go. If you changed the string.xml then "#string/helloButtonText" might be the replacement you need, or simply "Say Hello".

Related

could not resolve resource android eclipse

I am new in andorid development.
I have created my android first project according to http://developer.android.com/training/basics/firstapp/creating-project.html
And here is the code that I have as the project was created(no changes are done in code by me)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
Here is the screenshot of the Graphical layout
http://imageshack.us/a/img35/5284/android1s.png
So, it looks fine for now, but if I want to change the text Hello world to , say, hello, so the code's part looks like android:text="#string/hello", I get the error
NOTE: This project contains resource errors, so aapt did not succeed, which can cause rendering failures. Fix resource problems first.
and
Couldn't resolve resource #string/hello, and the text in graphical layout becomes
#string/hello, as it is in this image
http://imageshack.us/a/img826/7735/android2p.png
But I noticed one thing:
If after this error I go to res/values/strings.xml and add String with name and value hello, like
http://img842.imageshack.us/img842/8108/android3s.png
The error disappears and I can see the word hello in the graphical layout.
I tried these so far
Using ctrl+shift+s like it is described here https://stackoverflow.com/a/14462434/932473
Closing and reopening Eclipse.
cleaning, rebulding
removing the entire project and creating again.
The values folder does exist in res.
None of these questions solved my problem
Eclipse could not resolve string resource error
Can't Resolve Android Resource Strings
UPDATED
If I write like this android:text="hello"
I get this warning in eclipse
Hardcoded string "hello", should use #string resource,
I do not get it every time I want to use some string like android:text="#string/hello2", I have to first manually specify hello2 it in strings.xml ?
In res/values/strings.xml
<string name="hello">Hello</string>
Then refer the string in your textview as
android:text="#string/hello"
I guess you do not have something similar to above. Hence you get resource not found exception coz the resource is not defined in strings.xml
or
You can do as below
<string name="hello_world">hello</string> // change the string in strings.xml
android:text="#string/hello_world"
For the question in the comment
You can just use
android:text="hello"
the #string bit means its a resource called hello in your xml.
So you can also add the resource to your xml if you wish. Its known to be better practice, for internationalization.

Android - XML Accessbility Service won't build

I have written a very simple accessibility service, the code works fine without an xml file defined for metadata but I want to add the xml file for compatibility purposes.
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeNotificationStateChanged"
android:notificationTimeout="100"
android:description=""
/>
However the error I'm getting is none of the 'android:' resources are defined and also I'm not allowed that description. Although the description happens regardless of what I type or in what string asset I prove.
Anyone have any ideas..
Here it says android 4.0 has the option to configure the accessibility service with xml. Are you using 4.0+?
Also it mentions certain configuration options like canRetrieveWindowContent are only available when using the XML configuration. Are you using such options? If not, I don't think you really need the XML configuration.
It needs the xmlns portion....
Thanks for the suggested solutions. Turns out that you need to have your target build at 14 for eclipse to accept that xml file.

renamed xml file isn't declared in R.java

i am customizing an action bar, and i am using inflater and styles.
i added a menu.xml to a menu folder in project which i inflated into the onOptionsMenu,
the menu.xml contains the action bar item's.
Everything worked fine, until i renamed menu.xml to actionBar_menu.xmlusing refractor.
Now i can't even add it to the Inflater. it's not even declared in R.java
How do i solve the problem any hint or solution?
Thank you
Edit:
Thank you guys for your answers.
i did what you asked so i changed my action bar to actionb_menu.xml, but it still not working.
otherwise it's now declared in R.java, but eclipse editor doesn't recognize it
(ctrl+space), and even if i ignore this and continue typing the name of the file in the inflater it it still underlined as an error.
any solution for that??
Your original problem was: You can only use lowercase letters, numbers and underscores in your layout XML file name. As stated by #midoalageb
This is the answer to your edit.
Because your id wasn't initially in your R.java when you tried to do
R.id.....
eclipse has imported android.R; into your class file.
You need to go up to the top of your file delete this import:
import android.R;
then attempt to do:
R.id.whatever
again. When you press ctrl+space after R. ensure you select com.your.package.R and not android.R
hey hosni habeby look do like this,
open your new xml file the one you re named it and go to
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.something. app.OldJava" />
and replace the context value to your new java file the one you renamed also.. like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.something. app.MyNewActivityJava" />
in eclipse when you changed java file's name and it xml.. eclipse will not touch the values in the xml so we have to change it our selves also eclipse will add an import line may not refer to your R file.. if so remove it and type one that refers to your R file of your project not of Android or any library
You can only use lowercase letters, numbers and underscores in your layout XML file name. So rename your file to actionbar_menu.xml
Right click on Project. Refresh it first--> then clean it --> then rebuild it -->if there are necessary code changes then do it--> it should work.
also rename it in all small case chars
Try cleaning the project. If it still says R cannot be resolved then there should be something wrong with the xml file. Check your xml file for some extra characters that you mistyped or something of that kind. Your R file will not be built if even one xml file has an error!

Android custom attributes on "normal" views?

After taking a look at theming for Fede's UberMusic I came across the file album_screen.xml. Below is the source of that file. Basically I noticed that his themes have the ability to use custom views that are a part of his application, and that they have this XML namespace at the top theme. I am missing the connection as to how he is able to apply his attributes to views that he does not control, and how Eclipse will compile the cod below without fail. I placed the URL http://schemas.uberdroidstudio.com/theme into my browser's address bar but nothing came up, and I cannot figure out where/ how Eclipse knows the attributes that the namespace supports. Thank you ahead of time for your help.
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:theme="http://schemas.uberdroidstudio.com/theme">
<TextView
android:id="#id/artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.0"
theme:textFilter="uppercase" /> <!-- This is the interesting line -->
I suspect that the theme:textFilter="uppercase" line isn't actually having an effect on the (apparently vanilla) TextView.
As for the URL of the namespace, it is interesting that you can't access it, since it does not appear to be a local styleable (Android would have you refer to a local styleable namespace as http://schemas.android.com/apk/res/your.package). +1 for novelty.
The solution is actually not as complicated as I originally thought. XML namespaces are arbitrary strings that just need to be unique. If your namespace does not start with http://schemas.android.com/apk/res then it is not validated and the APK package is not checked for declare-styleable or the like.
Now a custom namespace if very simple to implement, as illustrated by this code snippet in GitHub. Finally, applying custom XML attributes to "normal" widgets can be accomplished by using the LayoutInflater.Factory and calling setFactory before you inflate your views. Throw it all together and you have a highly theme-able, XML driven application like Fede's UberMusic.

Can't get theme attribute references to work in android

I'm very new (second day!) to android and working my way through the dev guide. There, it says that you can reference attributes inside the current theme from your XML by using the form "?...", for example
<?xml version="1.0" encoding="utf-8"?>
<EditText id="text"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:textColor="?android:textDisabledColor"
android:text="#string/hello_world" />
If I try to build this, I just get "No resource found that matches the given name (at 'textColor' with value '?android:textDisabledColor')
Given that this seems pretty basic stuff, and yet I can't find any references to similar problems elsewhere, I must be missing something obvious or misunderstanding something simple. Can anyone tell me what it is?
I don't think textDisabledColor exists anymore - at least not in the newer SDKs which is probably why it can't be found. Try textColorSecondary instead - e.g.
android:textColor="?android:attr/android:textColorSecondary"
Unfortunately I cant figure out if theres a specific disabled color. I came across the site below which seems to define the default theme: See this link for definition of default theme
try android:textColor="?android:attr/textDisabledColor"

Categories

Resources