Eclipse creates FrameLayout when creating new project - android

I've just updated some things on eclipse ADT and I created a new project.
I've seen that in the place where you can set the Main Activity's and main layout's names, now there is another place to set a fragment's name.
When finishing creating the project, in the layout folder now appears the main layout and a framgent layout.
Also, the mainActivity extends ActionBarActivity, makes a fragment transaction in the onCreate() method, and implements the PlaceholderFragment class.
Is there a place where these changes are explained or could someone explain how these modifications affect to coding?
Sorry If this is stupid question, but I'm learning about this and I would like to know how theses changes affect.

Those files are template file for an android application using fragments. Like mentioned before you can delete them or use them as you see fit. Fragments are a little different than regular activities and I would recommend reading the android developer guide on fragments if you want to work with these files:
http://developer.android.com/guide/components/fragments.html
http://developer.android.com/training/basics/fragments/creating.html
These will give you an idea of how fragments differ from regular activities.

Related

MainActivity is too big. How do I split it into multiple files

I'm building an Android App which has a complex screen with lots of logic.
It currently contains a listview, tabs, search box, and a panel for updating user stuff.
The probem is that the mainactivity code file became very big, although I'm seperating things to different layers, because there's a lot of UI components which affect things in the screen.
I'm trying to seperate it to several files but I don't seem to do it right.
As much as I understood Fragments is not what I need here. Are there any other ways?
Just need some directions please.
I'm asking mostly about the code, not the layout (Although I don't care changing the layout too).
Currently it's 616 lines and the biggest problem is that we are a team and the maintenance became hell...
Create utility class and put your listeners and adapters there. Use main activity only to initialise view instances and setting listeners and adapters.
Although 616 is not a particularly big file you could use Fragments as they just add another layer of abstraction. They also have a similar lifecycle to activities.
This tutorial shows how to add fragments to tabs
http://developer.android.com/training/implementing-navigation/lateral.html
You can create a base class put all the initializations and listerners there.
Likewise for function to be implemented later create a template in base class and override it in main activity. It works !

When i create New android project using eclipse it create two xml layout

My old eclipse version worked perfectly but after updating eclipse, when I create a new Android project, it creates two XML layouts. 1st is activity_main.xml and second is fragment.xml. So when i add some item in activity_main.xml it wont display anything.
So how to use fragment layout?
This is the new Project Structure for Android by developers.
If you want to view any UI widgets put them inside fragment.xml file.
And any java code should put inside PlacementHolder class that is inner class of Main.java.
As others mentioned that's just the recent new project style from Android Team. But I guess you were also looking for an introduction into Fragments when you asked:
So how to use fragment layout?
Basically:
Android team has completely covered Fragments in their documentation. Start from here and then refer here. Then download this official source code and examine it closely.
Now I'm gonna share a personal experience:
I suggest you first study this tutorial on fragments from Vogella's website.
Why? Because the Android documentation is slow and rather boring there fore hard to understand, whereas the Vogella version is to-the-point and sharp.
You can then go back and study Android documentation.
at least read this: http://developer.android.com/guide/components/fragments.html it will give you an idea how the fragments work. And better switch now to Android Studio, the sooner the better.

ListActivity with ActionBar support v7

My main Activity extends ListActivity, and now I have to implement an ActionBar with support for older versions.
I have readed about the ways to do this, and I decided to try this way:
I downloaded the source code of ListActivity, I modified few things, and now I need to implement here the ActionBar, so I'm trying to extend this Activity to ActionbarActivity. This way, I would have a custom ActionBarListActivity, and I could extend my Activity to this custom class, have then the functionality of the ListActivity and the ActionBar.
These are the steps I've done:
Add the support v7 library to my project
Set as aplications theme the #Style/Theme.AppCompat
But when I'm trying to extend the activity to ActionBarActivity, doesn't let me do this, it says "ActionbarActivity cannot be resolved to a type" and suggest to change it for 'ActionBar'(android.app). I know I have to import the "import android.support.v7.app.ActionBarActivity" but it doesn't let me do this.
So, what is going wrong here?
A simpler approach (to downloading source, etc.) is to break out the list into a Fragment and just include the Fragment in the Activity. I'll outline the steps to get there:-
Split out the list into it's own layout XML. So you will end up with 2 XML's - one for the Activity 'container' which will be pretty simple - (see step 2), and one for the new Fragment layout which will be your overall layout for the list, but most importantly will include a <ListView> element).
Include a <FrameLayout> placeholder in your activity's layout XML (to be the container for the fragment). It will be nested inside the top level layout (a <RelativeLayout> would do as the top level). Give the placeholder an id like myFragmentHolder.
Create a new Fragment class (extend ListFragment) called say, MyListFragment.
Move all of your list code (such as ArrayAdapter code etc.) into the new fragment class
Your Activity code is now really just a shell to contain the fragment and to manage the ActionBar. To support the list fragment all it needs in onCreate() is the call to setContentView() and to replace the fragment:- getSupportFragmentManager().beginTransaction().setTransition(FragmentTransaction.TRANSIT_ENTER_MASK).replace(R.id.myFragmentHolder, new MyListFragment(), "MY_LIST").commit();. ("MY_LIST" is an arbitary tag name you use to identify the fragment. It can be anything -read the docs for more info).
Your Activity should still extend ActionBarActivity.
Now you have a nice separation of concerns. The Activity manages the ActionBar and the Fragment manages the List.
It does not sound like you imported the library right especially.
in eclipse you need to right click on the project, go to Properties, select Android in the list then Add to add the library
follow this tutorial in the docs
http://developer.android.com/tools/support-library/setup.html
Update:
Try this:
Import support library as a project from "sdk/extras/android/support/v7/appcompat".
Reference library in your project (for Eclipse, "Properties - Android - Add").
Build projects (for Eclipse,"Projects - Build All"). Make sure, you have "android.support.v7.appcompat"in your main project gen folder.
If it still doesn't solve your problem, restart eclipse.
then clean and rebuild project
If the problem persists, remove the support library from you computer and redownload it and follow above mentioned steps.
Thank you Nebu for your great explanation.
Theo, for simplicity, you might check out this tutorial on how to change a ListActivity to an ActionBarActivity by using a ListView object. (http://www.opensourcealternative.org/tutorials/android-tutorials/simple-android-listview-without-listactivity/)
However, Theo, I'll try to clarify Nebu's instructions with a little more details in order to try and answer your questions about his instructions.
Theo, as far as I understand, this is what Nebu is saying needs to be done.
In your original activity's XML for the listactivity, you had a FrameLayout.
You're going to copy the FrameLayout element to your new XML for your ListFragment, and the FrameLayout will be the root element in that new XML file.
In the original activity's XML file, replace the original FrameLayout element with a simple declaration of a FrameLayout element and an ID, just the start and end tags of the XML element without any child elements. This will be used to populate the FrameLayout with the one in your ListFragment at runtime.
The R.id.myFragmentHolder is the ID of the FragmentLayout placeholder in your original activity's XML file.
The "new MyListFragment()" call is calling the constructor of your new ListFragment class.
"MyList" can be any string, I would personally make it more descriptive of the list I was using, but it doesn't matter. Search Google for android fragment Tags to learn more about Tags.
When you extend ActionBarActivity, you will get errors unless you import android.support.v7.app.actionbaractivity in your actionbaractivity class that is replacing your original ListActivity class.
if I missed anything or if anything is not clear, please let me know.

Extending Activity with FrameLayout over multiple activities with code reuse

I am not sure if this is the right way to do it. I have a ChatActivity using FrameLayout in frame.xml. This activity needs to be reused across 5 activities. Is there anyway to do a code reuse? This acitivty runs independently of other activities.
For example, in activity A, which uses main.xml, I want ChatActivity and frame.xml to be included. What is the best way to achieve this besides merging the ChatActivity and frame.xml into activity A and main.xml? Merging the activities would mean that I have to copy and paste codes 5 times into different activities. I am not sure if this is the right way...
You cannot "include" an Activity to another Activity. Since your Activity has a basic functionality that all your other Activities use, you could have all your Activities extend this basic Activity.
The best way though for you would be to use Fragments and the compatibility library.
Regarding layouts, you can have reusable ones and import them to your current layout using include.
Hope this helps!
Use Fragments. you can create one Fragment and use the same in all activities. Fragments have their own view. Look at the following link for fragment.
http://developer.android.com/guide/topics/fundamentals/fragments.html
you can use fragments before 3.0 by including compatibility library.
http://android-developers.blogspot.com/2011/03/fragments-for-all.html
I android you can reuse xml files by using the include tag like
<include layout="#layout/okcancelbar_button" android:id="#+id/okcancelbar_ok"/>
To share functionalites of an activity with other activities, create a base activity with common functionalites and make the other activities extend from it.

My first ever Android code with Fragment

As an exercise, I am trying to rewrite the following google tutorial with Fragment class. The original tutorial implements tabs by using the old TabActivity class and TabHost/TabWidget annotation.
Tab Layout Google Tutorial
I have converted all Activity class with Fragment. I couldn't make my new code to work. I think I am stuck.I could not find any 'complete' Tab sample code using Fragment class.
Here are my questions
1. Should I define in the res/layout/main.xml or calling Actionbar.addTab(...) in my entry class, or both?
2. What would be complete res/layout/main.xml looks like? What would be the root element (i.e. LinearLayout, FrameLayout...etc)?
3. Any additional info would be greatly appreciated.
Check out this example from the compatibility library demos: FragmentTabs.java
and the corresponding layout: fragment_tabs.xml
Really, though, I wouldn't start with Tabs if you're trying out Fragments for the first time. Tabs in Android are a little bit of a mess. The above example (from Google itself) uses a hack just to get things working. Tabs just add a layer of unnecessary confusion when you're just learning.
Here's a more straightforward starting-out Fragments example/tutorial: http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html
(Just make sure to replace things like getFragmentManager() with getSupportFragmentManager() if you're using the compatibility library.)

Categories

Resources