Creating Activity | Android Studio ? [Some concerns] - android

Till Eclipse, creating a second activity and class had a brevity . However, with the new official Android studio , I feel more of an abstraction . Earlier, while creating an activity it specified many things, nevertheless asked the layout of an XML file. And here are my doubts:
1) Is there an option to create Java file in Android Studio just like we use to do with Eclipse ?
2) Which is the best way to create an activity in Android Studio with our specified layout?

1) You can go into the project directory and right click the folder where you want the activity to be (typically in src/java/com.xxx.xxx/) and select New -> JavaClass and create YourActivity and make sure that YourActivity extends Activity. Be sure to add the activity to your manifest!
2) There is no best way as it is a matter of preference. Personally I do what I said in step 1 and create the xml layout file myself in the res folder. I find that Android Studios auto activity creation to be a bit annoying as they add menu xml files that I do not need.

Related

Where is the "content_my.xml" file in Android Studio?

In the training docs, it says that the content_my.xml should be in the layout directory. But as you can see in the picture below, it is missing from there. What can I do?
Probably You have selected EmptyActivity instead of BasicActivity during creation of the project.
Go to File -> New -> Activity -> Basic Activity to create it in the same project or just start another project.

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

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...

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

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!

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