When ever i create a new project, the Fragment_main.xml file is added to my Layout folder and unlike in Eclipse it is this file that contains what is normally in the Activity_Main.xml file.Why is the Fragment_main.xml file always added to my projects in Android Studio and how is it different from the "regular" Activity_main.xml file?
-
Presumably, it contains the layout for a fragment. I would recommend reading the generated Java source code to see where this layout file is referenced. – CommonsWare Nov 09 '13 at 14:08
3 Answers
The Activity_main.xml contains the Layout for the FragmentActivity and the fragment_main.xml is the Layout for the fragment.
For more information to fragments and how you can use it.
Visit: http://developer.android.com/training/basics/fragments/index.html

- 271
- 4
- 11
If you are creating a new Project and it adds fragment_main.xml by default you must be selecting a layout by default. Maybe a pager/spinner layout?
Fragment_main is the same as activity_main. The names are just string labels and mean nothing in and of itself and are just changed for clarity by the IDE.
Have a read of this.
http://developer.android.com/guide/topics/ui/declaring-layout.html

- 554
- 1
- 7
- 18
-
1**If you are creating a new Project and it adds fragment_main.xml by default you must be selecting a layout by default. Maybe a pager/spinner layout?** No, i am not selecting a layout by default, i just follow the basic default steps in creating a new project and end up with it. – Ojonugwa Jude Ochalifu Nov 09 '13 at 14:50
-
1Since they are the same, i'll just work with the Fragment_main.xml file then.But what's the point of having both? (That's a rhetorical question :)) – Ojonugwa Jude Ochalifu Nov 09 '13 at 14:52
-
1If you have different layouts for different parts of an app. If you don't know what fragments are just ignore it for now. You'd have to understand what are to Fragments to get this. If this helped at al give us an upvote thanks. :) – bungleofsketches Nov 09 '13 at 16:01
just as Bytehawks said above.
activity_main.xml describes Layout for the FragmentActivity and the fragment_main.xml is the Layout for the fragment.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //get the activity_main.xml for layout
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//code for describing layout more details, get fragment_main.xml
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}

- 56
- 3