Build Simple UI Lesson Sample - android

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.

Related

Reference an Image in another project

I have an project that I put all of my images for the system in the workspace.
I have this at the top of my layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:images="http://schemas.android.com/apk/res/com.utas.android.imagelibrary"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/dark_blue"
android:padding="8dip"
>
The namespace "images" gives a warning in Eclipse stating "unused names space images", which leads me to believe I am atleast doing that part right but the next part is wrong.
With in my ImageLibrary (a project that has the "Is Library" check boxed checked in the same workspace) I have the following pathing
res/drawable/01_core_accept/drawable-hdpi/ic_action_acept.png
I have a button within the layout as follows.
<Button
android:id="#+id/btn_finish_a"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/btn_finish_a_label"
android:layout_toRightOf="#id/btn_start_c"
android:icon="#images:res/drawable/01_core_accept/drawable-hdpi/ic_action_acept"
android:onClick="finishActivityA"
/>
However I am getting an error on the image reference stating "error:Error:No resource found that matches the given name (at 'icon' with value '#images:res/drawable/01_core_accept/drawable-hdpi/ic_action_acept')."
How do I properly reference an image in an external?
You do not need this line:
xmlns:images="http://schemas.android.com/apk/res/com.utas.android.imagelibrary"
The line should read:
android:icon="#drawable/ic_action_acept"
If you've linked an Android Library project correctly, resources defined in that library should be compiled into your app and you reference the same way as resources in your own res folders.

R doesn't generate button

I've just finished to do the layout I want, and even although I saved everything, when I try to find by id the button, the button I'm looking for is missing.. Both the button's I have.. I've checked in the R class, under the ID, and it seems it doesn't sees' the buttons. Doesn't generate them? I dunno.. Any way, it happens every time I use relative layout!! Is there any connection? It works with TableLayout and with linear layout.. Only with relative layout it make's problems.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/Background" >
<TextView
android:id="#+id/corpTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/corpTag"
android:textColor="#color/White" />
<Button
android:id="#+id/addBusinessButton"
style="#android:style/ButtonBar"
android:layout_width="200dp"
android:layout_height="25dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="208dp"
android:text="#string/addYourBusiness" />
<Button
android:id="#+id/searchBusinessButton"
style="#android:style/ButtonBar"
android:layout_width="200dp"
android:layout_height="25dp"
android:layout_alignBottom="#+id/addBusinessButton"
android:layout_alignLeft="#+id/addBusinessButton"
android:layout_marginBottom="36dp"
android:text="#string/searchBusiness" />
I found this happening to me with a broken layout. No need to be worry. I am trying my best to giving you the solution
Solution
Make sure that anything the R. links to is not broken. Fix all errors in your XML files. If anything in the ADKs are broken, R will not regenerate.
If you somehow hit something and created import android.R in your activity, remove it.
Run Project -> Clean. This will delete and regenerate R and BuildConfig.
Make sure Project -> Build Automatically is ticked. If not, build it manually via Menu -> Project -> Build Project .
Wait a few seconds for the errors to disappear.
This problem isn't related to relative layout, but is actually set on the background because Android drwable is case-sensitive and now allows you any capital letters for image or layout. Rename your image name or drwable name to "background" instead of Background."
Here is the problem with set background name:
android:background="#drawable/Background"
It might be the Android Style ButtonBar. It isn't available for every API level. So if you have a low minSDK, it's not going to build and Eclipse/IntelliJ should give you an error message in its error pane.
The rule given by Google, is that you should always define your own resources. If you want to use a resource that is in the Android name space, that's fine, but you should copy it first and package it with your apk, because that's the only way you can be assured that it will always be bundled with your application.
And yes, I realize many people and many tutorials on the web don't do it that way, but that's because most people are lazy.

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.

DrawableLeft fails to compile

I currently have a code for a button in XML as such:
<Button
android:id="#+id/button1"
android:layout_width="250dp"
android:layout_height="62dp"
android:layout_gravity="center_horizontal"
android:drawableLeft="#drawable/ic_menu_slideshow"
android:text="Start Slideshow" />
However, I get an error for Error: No resource found that matches the given name (at 'drawableLeft' with value '#drawable/ic_menu_slideshow'). However, when I check on the graphical layout of the XML file, the stock drawable at ic_menu_slideshow is present. Why do I get this compilation error, and how would I fix it?
Try to replace #drawable/ic_menu_slideshow with #android:drawable/ic_menu_slideshow
Have you tried cleaning your project? A similar question was asked moments ago so keep an eye on that as well: Can't Resolve Android Resource Strings

Android R not generating, even after following every single step on the internet

The android R has been troubling me for quite some time now. I have seen it in a lot of projects of mine, sometimes they just disappear, sometimes I have had to recreate a project, etc. Still this does not solve anything for my current project.
Logically I suspect the XML files to contain an error but I am unable to find it.
I am using Eclipse as my development environment and this is my main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imgLogo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/title"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/title"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/btnNewTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/overview" />
<Button
android:id="#+id/btnOverview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/overview" />
<Button
android:id="#+id/btnValues"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/values" />
<Button
android:id="#+id/btnExit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/exit" />
</LinearLayout>
This is my strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Werkregistratie</string>
<string name="title">Werkregistratie</string>
<string name="overview">Overzicht</string>
<string name="values">Salaris/reiskosten aanpassen</string>
<string name="exit">Afsluiten</string>
</resources>
I am unable to find the problem so I really hope you can help me out.
Ok this seems to be just a bug in eclipse, quite some answers are partially correct. Make sure the names validate(you can use Lint in some cases). Make sure the xmls validate. Then check your buildpath for errors. After that do a project clean and all should be fine.
Also, this problem doesn't appear as much in the new android sdk's so they fixed something it seems.
I tried your xml and its work well for me , Hopefully your java class code might have some problem. Try to find out problem inside your and surely clean the project.
All the best :)
If your package name is com.foo.bar.package, then you need to import it like this:
import com.foo.bar.package.R;
Then the findViewById should work using this notation:
Button btnValue = (Button)findViewById(R.id.btnValue);
Sometimes, Eclipse can be quite obnoxious when it comes to that "Resources missing" a lá cannot find the R file which does not get generated.
I just faced this issue today when importing an app src from workspace. all line of code using R was showing error.
I read somewhere that I should actually set tagetSDK as 4.0 from properties/android view. and minsdk version as 8 in manifest.
It worked like charm.
One more thing it could be the issue with any res file/layouts/drawable have the names with capital letter. Some time Thumbs.db got generated in drawable folder and cause the same issue.
Thanks!!!
I think I've ran into this problem before, the only way I could get around it is to try again with a fresh project and import my code. Inefficient and clunky I know but this was how I managed to get the R file to generate after a frustrating few hours.
Failing that you might be able to find something to help you here
If your package name is "com.example.test"
then try something like this-
findViewById(com.example.test.R.id.textview1);
not like this-
findViewById(R.id.textview1);
(* Only use when your R file is not generated)

Categories

Resources