2

I have upgraded my android studio before encountering this.

I Need Blank Activity with Fragment. I am creating new activity as "DetailActivity" with fragment. It Creates two source files

1)DetailActivity.java 
2)DetailActivityFragment.java

Also there are 3 other source files created ie.

1)Activity_Detail.xml
2)Fragment_Detail.xml
3)Content_Detail.xml

I understood the significance of two source files but didn't really understood the significance of these 3 source files.

My questions is : There is a Tag in Activity_Detail.xml ie:

 <include layout="@layout/content_detail" />

That means we can go ahead and add our ui elements in Content_Detail.xml (My Understanding)

So Why do we have Fragment_Detail.xml

what is the purpose of Fragment_Detail.xml. If UI elements should go in Fragment_Detail.xml then what is the purpose of Content_Detail.xml

Where the UI elements should really go: Fragment_Detail.xml or Content_Detail.xml?

Android Studio version 2.2

api version: 22

Android M.

Mayur Gupta
  • 762
  • 3
  • 8
  • 23

2 Answers2

0

So Why do we have Fragment_Detail.xml?

Because you probably forgot to un-tick this option when you were creating the Activity via wizard:

Use a Fragment

That means we can go ahead and add our ui elements in Content_Detail.xml (My Understanding)

Yes, that's correct. But you can also add UI elements to activity_detail as well, this question explains which of these to use for which elements.

Community
  • 1
  • 1
Vucko
  • 7,371
  • 2
  • 27
  • 45
  • I did that intentionally ,..... I want the blank activity with fragment. – Mayur Gupta Jul 28 '16 at 19:28
  • What are you asking then? Why are fragments used? – Vucko Jul 28 '16 at 19:29
  • I am new to android ...i am asking ... what is the purpose of fragmentxml and content.xml i got both these to files along with activity.xml. where should i add ui element.?? – Mayur Gupta Jul 28 '16 at 19:31
  • Fragments are reusable and multiple fragments can be held by multiple activities. For example using a viewPager - you cannot add views to it, you can add fragments. and content.xml is a view. If you add a fragment into your activity's container (replace its contents with a fragment), then your `content.xml` will no longer be visible. – Vucko Jul 28 '16 at 19:34
  • Ok that clears a few things to me ... I am actually following udacity tutorial. they have created the blank activity with fragment and then it created 2 resource files 1)Activity_Detail.xml and 2)Fragment_Detail.xml and not the content_detail.xml. Do you know why this happened. – Mayur Gupta Jul 28 '16 at 19:40
  • Because they did not select "create a fragment" **and** they chose `empty activity` instead of `blank activity` as the template. Don't ask me why those two are different, don't know myself :D Probably cause `blank` is newer, to support the new guideline added in API 23 with `content.xml.` Consider upvoting if I helped you in any way. Thanks. – Vucko Jul 28 '16 at 19:41
  • Actually i have the same question in my mind. and i think its all the same as in new android studio there is no blank activity its just empty activity. and i have not chosen the empty activity. i have chosen "Basic activity" where we get an option to include fragment. and in the course video they have "Blank activity with fragment" thing. i would suggest you to upgrade your android studio and then you will understand why i am confused. – Mayur Gupta Jul 28 '16 at 19:57
  • You're right, I do have AS version 1.5.1 .. Honestly, I'm scared to upgrade :D – Vucko Jul 28 '16 at 19:57
  • ohhh i already got 2 downvotes ... and content.xml is still a mystery.. – Mayur Gupta Jul 28 '16 at 19:59
0

If you generate a new project w/ a fragment then it generates those three files to separate out the different components of the DetailActivity you are creating.

The first is the activity_main.xml, which has all the information related to the AppBar layout, potentially the Floating Action Button, and if you look at the code will have the following line:

<include layout="@layout/content_main" />

This line of code embeds the content_main.xml file in your activity_main.xml file. The content_main.xml is just the definition of a fragment:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment"
android:name="csci567.examples.myapplication.MainActivityFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:layout="@layout/fragment_main" />

If you'll notice the last line defines the layout of the fragment, to be your fragment_main.xml file. This really breaks out where the fragment is defined and where the activity is defined. It also separates out the content from the rest of the body of the activity into the content_main.xml file.

Hope that helps.

  • Also usually more helpful if you look at the "Text" vs the "Design" view of the xml files as will let you see the actual script definition of the UI elements. – Dr. Coffee Jul 28 '16 at 19:34