This is probably realy noobish question but, i am unable to solve it. I dont know what i am doing wrong or what should i do, i checked some tuts but i wasnt able to solve it. So i created new folder in res called menu and created new file there called xyz.xml. Now i want to call it in activity with following R.menu.item ... But when i just write R. it doesnt show menu as option. I can call any map which are default here like layout etc, but maps which i created i cant call them. What should i do to solve this?
If the menu file you create is called xyz.xml, then in your code you should use it as:
R.menu.xyz
Also, are you referring to the correct R? There's one R class used for the Android framework resources (android.R) and then there's one specific R class for your projects resources. To ensure you're using the right one you can use the fully qualified namespace, e.g. yourprojectsnamespace.R.
See http://developer.android.com/guide/topics/ui/menus.html for more information about Android menus.
Related
I'm trying to create my first app with Android but I've encountered some problems.
Following a tutorial and making:
new android application -> blank activity
The default result should be:
With MainActivity.java and R.Java inside it.
But instead I've got this:
There are a lot of errors and the R.Java file is missing.
I can obtain a result similar to the tutorial one (with no errors), creating a new android application and unchecking create activity, but this time MainActivity.java under src is totally missing.
Maybe I could copy the MainActivity.java of the first project and put into the second one, but I prefer understand why it doesn't work.
Thanks for the help.
Best regards.
its import library problem
Right click on you application----->Android
under library view ,
check if you have this red error then remove the library and add it again
I'm trying to learn how to program android applications, so I downloaded the ADT bundle that google supplied, and I tried following the tutorial that allowed me to create a simple application. However, during the procedures, there are several instructions telling me to open up the fragment_main.xml file, but my layout/res/ directory did not have this file, only the activity_main.xml file. Furthermore, when creating new Android activities, there was never an option to name my fragment layout, indicating that eclipse just doesn't create it for some reason. I didn't think this would be an issue at first (I just edited activity_main instead), until I realized that the tutorial wanted us to use the some information from the fragment class or xml file.
Does anyone know why my Eclipse IDE is not creating a fragment_main.xml? I will try to supply more details if necessary.
While creating the new Android Project in one of the panels select "Blank activity with Fragment" instead of default selection "Blank Activity". The Android Developer tutorial does not say anything about it. Doing so will create the file fragment_main.xml in res/layout/ which is needed to continue subsequent steps.
Based on the versions you indicated in the comment response, I think updating to the later versions (22.6+) would help, as discussed in https://code.google.com/p/android/issues/detail?id=67421
Try creating a new project with "Blank Activity with Fragment". I hope this is helpful.
Open MainActivity.java from the src/(package name file)/ directory. Then inside the java file there is a method called OnCreate() that has setContentView(R.layout.activity_main) as default.
Change that to R.layout.activity_main to R.layout.fragment_main
so instead of having
setContentView(R.layout.activity_main)
you'll have
setContentView(R.layout.fragment.main)
Once that's done change the name of the activity_main.xml file under the /res/layout/ directory to fragment_main.xml
Thanks for that Onik!
I am writing a simple application to get familiar with android programming. Today I want to play with menus. I follow this page!. I first create "menu" folder under "res" and then add "game_menu.xml" in it. However this "R" thing can't get that folder. I tried some refresh to get it 'auto-generated' but it didn't work. I clean for a rebuild, but this time R.java completely dissappeared. How can I make eclipse to recreate R or what is the problem?
Are you sure the R is not creating the appropriate link for the menu?
To find your xml menu, you must type: "R.menu." & hit ctrl+space and all your menu's will pop up.
Try this way.
And if you want to check your R file, if its creating the menu link, search for:
"public static final class menu" in R file
In that class you will find your xml menu's..
are you able to compile ? Check which R you have imported in your code. It should not be the android.R but the one from your project
You have most likely misspelled something in your xml file, or have some other error. If the xml files are not entirely correct (or other resource files), the R. file cannot be built. Go through the file and make sure that everything is error free.
A common mistake is to name the resources with capitals, although the names can only contain small letters.
another common mistake is to use unsupported folder names.
I'm new... just finished my first eclipse/adt tutorial.
I don't see see anything in the manifest that points to res.layout.main.xml or res.values.strings.xml.
QUESTION: how does android find these xml's?
thanks,
Shannon
All the xml files get sent to the R.java class. They're assigned specific integer IDs that can be referenced in your java code.
when you say setContentView(R.layout.main) you're getting the integer ID and passing it to the content view. This R class then redirects it to your xml.
You can get to these files using the R class. For example when you want to set your contentview to main.xml you do it like this setContentView(R.layout.main);
if you want to reach a String from a XML you can do it like this getResources().getString(R.string.appname);
Also take a look at the Application Resources section of the Dev Guide http://developer.android.com/guide/topics/resources/index.html
my app has a styles.xml file with various visual constants defined.
I'd like my users to be able to switch the entire app to an alternative visual theme. I'd like to provide an alternative styles2.xml file and switch at runtime (via the Settings).
Is this possible, and how? I suspect the style names' appearance in the generated R class does not bode well.
If it's not possible, what's my next best option?
Not sure if anyone is still interested but I have found a possible solution. A bit hackish but gets the desired result.
Basically I set up my 2 style files in separate country code directories:
res/values-mcc199/style.xml
res/values-mcc198/style.xml
Then in my activity I use the following to change which is referenced:
Configuration config = new Configuration();
config.mcc = 199;
getBaseContext().getResources().updateConfiguration(config,null);
I've only done some basic testing so far but it appears to work. Obviously if you are already using country code to decide your layouts then this will interfere. I think there may be problems as well if the phone gets an event about a country change.
Actually, after some reading of the doc, it seems that this can be done. Look here.
As it is mentionned :
To create a set of styles, save an XML
file in the res/values/ directory of
your project. The name of the XML file
is arbitrary, but it must use the .xml
extension and be saved in the
res/values/ folder.
Now, if this is logical and I didn't read the doc wrongly, you can create as many styles as you want, reference them in themes.xml with #style/... (if you want to apply it to a whole activity or application) and then, just call
setTheme(R.id.yourtheme)
I think this should work. Have a go at it and tell us?
It's not an exact answer; in my blog post here:
http://blog.blundell-apps.com/switching-android-configurations-using-constants-and-ant/
I switch out a java class at build time using Ant, there is nothing to stop you switching out an XML file, as it compiles after the switch. To amend the tutorial you'd just have to change the path's of the file your templating.
Also mirrored on GitHub: https://github.com/blundell/BuildChoiceTut