Android ADT: No fragment_main.xml, only activity_main.xml - android

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!

Related

Android Studio Activity Java File does not recognize XML

I have a big problem. Last I copied a CardView with RecyclerView from a video. After that I created a new empty activity, but the Javafile didn't want to accept the corresponding XML file. And if I go with ALT + ENTER he suggests to me to create your xml file, but afterwards he tells me it already exists. So I can open the XML file, the program recognizes it but it is not recognized in the Java data and I cannot select it there either. There a screen shot, There a picture from solution research, And There a photo of the xml File.
Can someone help me there? Thank you in advance!
I was facing the same problem but you just need to close your project and open it again, the error related to activity file will bee removed!!Just try it.....
maybe you need to rebuild your app
1- Build > clean project
2 - Build > Rebuild project
I hope it will work with you .
Delete those two files. Copy the code first.
Go to File ->new activity then create that activity and paste the code there, besides those lines.

Two activities are created when I try to create a new Blank Activity in Android Studio

Whenever I try to create a new blank activity in the layout folder two layouts are created.For eg:When I try to create a Blank Activity by name lola following two activities are created i.e. activity_lolla.xml and content_lolla.xml:
Not two activities, but two layout files... U can use "Empty Activity" template to avoid that. In this case you also would add android:label="#string/title_of_your_new_activity" in the manifest file ander activity node
I am going to make an assumption that you are either:
A)Just beginning to learn Android from tutorials that uses older version of Android Studio
B)Recently upgraded to new Android Studio 1.4
If you look at activity_lola.xml, you should see in middle, there should be a line of code like this:
<include layout="#layout/content_lola" />
Both of these are layout files but only activity_lola is considered an activity. The content_lola is layout that is included inside the activity_lola.
So basically if you belong to assumption A, you can get the "Blank Activity" that books and other tutorials are referring to by selecting "Empty Activity".
Hope this helps and happy coding! =P

Hello world android - creation with errors

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

Android Studio XML Layout file exist but I can't find it

I wanted to create a Fragment in Android Studio, so I did the following steps:
Right click on package
New -> Android (Other) -> Fragment (List)
Checked both include fragment factory methods? and switch to grid view on large screens
The description said "Creates a new fragment containing a list that can optionally change to grid when on large screens."
Here's the funny part: As I was reading the code to understand how this switching is done, I could not find any code that switched XML files or anything. The only odd thing that I found is that the XML used for the Adapter was called R.layout.fragment_item, but there was no XML called like that in my layout folder.
There were 2 XML called R.layout.fragment_item_list and R.layout.fragment_item_grid. So I though "hey, maybe internally the framework auto decide if going for _grid or _list and that's why it doesn't mark R.layout.fragment_item it as non-existent"
When I was done reading the code, I deleted both R.layout.fragment_item_list and R.layout.fragment_item_gridfrom my layout folder and guess what? R.layout.fragment_item is NOT marked as non-existent, and there is no such file inside my Layout folder.
I already went Build > Clean and Build > Rebuild Project and it still marks it as existent. Am I missing something here? sounds like there is some basic stuff I haven't taken into account.
Just found what was going on. Inside values/refs.xml there was <item name="fragment_item" type="layout">#layout/fragment_item_grid</item> which should be the cause of it.
I had the same issue, I didn't understand your solution as I couldn't see refs.xml in the values folder.
For anybody out there, this is what worked for me:
In Android Studio go to File -> Invalidate Caches / Restart...

Using map created in res folder

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.

Categories

Resources