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.
Related
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".
I installed packages from the Android SDK manager, but when I open an xml file to edit a layout, I get unresolved errors When I drag and drop items from the sidebar, I get a log saying:
Couldn't resolve resource #dimen/activity_horizontal_margin
Couldn't resolve resource #dimen/activity_vertical_margin
"#dimen/activity_horizontal_margin" in attribute "paddingLeft" is not a valid format.
"#dimen/activity_vertical_margin" in attribute "paddingTop" is not a valid format.
"#dimen/activity_horizontal_margin" in attribute "paddingRight" is not a valid format.
"#dimen/activity_vertical_margin" in attribute "paddingBottom" is not a valid format.
Also, no properties show up when I view the menu. When I right click on the layout to set a different layout manager, I see the following. I installed the correct packages from the SDK Manager, including all tools, the Android 19 API, Android 18 API, and the Android Support library.
Here's my xml file
<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" >
</RelativeLayout>
Where can I start by fixing these errors?
This is probably caused by a mix-and-match of some large, medium and small resolutions in your project but render the layout in a view not considered large enough. In this case, just change the render parameter in the graphical layout tool. Hope it helps.
Such layout errors usually appears after you've created new Activities/Layouts using IDE build-in tools/wizards (like New->Activity->Blank Activity with Fragment in Android Studio). At this moment Java and XML code is already created, but auto-generated code (like R.java file) isn't generated yet. What's why layout editing feature is not working - it requires valid R.java file with all your resources (in your case it's #dimen/*).
To fix this errors simply compile your project (which will force auto-generated code to be generated) and refresh layout editor window.
I am having issues viewing text in the graphical layout section of my android applications. This is consistently happening across many projects, new and old. See the link for visual example: http://i.imgur.com/OwENudf.jpg
The button in the image should say 'test' but instead its two boxes. Sometimes it appears as an open bracket, [
Background- I was having issues with eclipse/android sdk not creating the R.java file when a new project was created so I uninstalled the sdk and then reinstalled it. The R.java file builds fine now but the text in the graphic layouts is all strange characters.
Any idea what I can do to fix this?
The XML has one warning, which is 'Hardcoded string "test", should use #string resource"
I don't think this is the issue because I have had this error before on working applications.
EDIT:
XML code-
<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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:text="test" />
</RelativeLayout>
I believe you are having font issues with your emulator. It can't find appropriate characters to display. You may need to reinstall that.
Ok so I searched and I think it will fix your problem (and it was too long to post it as a comment).
Go to the platforms folder where android-sdk is installed (C:\Program Files (x86)\Android\android-sdk\platforms).
Then go to the folders you're currenlty building your projects with then go to data/fonts (i.e C:\Program Files (x86)\Android\android-sdk\platforms\android-8\data\fonts) and take a look at the file fonts.xml. Also, may a ttf file disappeared when you updated your ADT (which seems odd).
You can find here an example. Read this carefully before trying to modify these files.
I hope this will solve your problem. If not, I have really no idea about what to do.
Ok, I have been following a lesson sample at the Android Developer site and the code was thus:
<LinearLayout 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:orientation="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
</LinearLayout>
Apparently, the code should be fine, after worrying I typed it in wrong, I ended up copying it directly but the Console is telling me:
Description
error: Error: No resource found that matches the given name (at 'hint' with value '#string/edit_message').
Description
error: Error: No resource found that matches the given name (at 'text' with value '#string/button_send').
Now these values are defined in the strings.xml file. So I have no idea why I would be getting these errors.
To anyone who can help here is a big thank you in advance.
Try in eclipse project>clean . Sometimes eclipse give you errors like these. Or restart eclipse. Did you also make sure at your strings.xml you have something like this ?<string name="edit_message">Your intresting string.</string> sometimes if you use the 'gui resource adder' It gives mistakes. Check inside your xml.
make sure there is no capital characters or underscores in any file inside res.
Also , there must no any error inside the files of res and src ..
after that you have to clean the project as mentioned in previous answer.
Actually these errors prevent the R file to be updated which is necessary to access resources inside res file.
hi have problem when i view graphical.layout of this file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this is main text" />
</LinearLayout>
when i view graphical layout of this view this xml file then this warning show
[I18N] Hardcoded string "this is main text", should use #string resource
but i dont want to declare string valiable i want to show this "this is main text" without declaring string
The warning about 'should use #string resource' is just a reminder that locale-specific information should be placed in 'resource bundles' (eg /res/values/strings.xml) so that labels and other text can be provided in different languages without having to re-code and recompile the entire application for each language.
If you're just testing things out, you can safely ignore that warning, but it's a good idea to get in the habit of referencing strings as resources in external files rather than hardcoding them directly in the application itself.
It is not good practice to hard code strings into your layout files. You should add them to a string resource file and then reference them from your layout.
refer this answer and this one also.
You must declare your text using `#string` resource that
is a good programming format.You should prefer using
`string.xml` in value folder in res folder if you want
to use texts.
If you want to suppress the specific warning use
tools:ignore="HardcodedText"
more details
http://tools.android.com/recent/ignoringlintwarnings