11

After the latest update of the Android Developer Tools there is a problem I faced whenever I create a new project. It forces me to create a fragment_layout.xml as well. So in the hello world program there is too much of code.

But in previous one only MainActivity.java and activity_main.xml appeared.

Is there any way to remove only the fragment_main.xml and the extra code in the MainActivity.java.

One way is to un-mark the create activity part in the beginning but it will not create the MainActivity.java too.

Rajan Maurya
  • 624
  • 1
  • 12
  • 24

7 Answers7

5

To remove fragments from your new wizard-created app:

  • Change the type of MainActivity from ActionBarActivity to just Activity.
  • Delete the whole if (savedInstanceState) statement from MainActivity.
  • Delete the PlaceholderFragment class from MainActivity.
  • Replace the contents of activty_main.xml file with the contents of fragment_main.mxl or your own layout.
  • Organize imports and clean your project.
scottt
  • 8,301
  • 1
  • 31
  • 41
  • How does one change the type of a file? –  Dec 31 '14 at 16:36
  • I'm not sure what you're asking. If you're questioning what the first bullet means, then it's saying to change the line that looks like "public class MainActivity extends ActionBarActivity {" to "public class MainActivity extends Activity {". – scottt Dec 31 '14 at 16:53
  • Exactly that, i misread it thank you :) I ended up recreating file without fragment. –  Dec 31 '14 at 17:11
3

Yeah, that's annoying. Just delete the fragment XML and recreate the activity XML with the layout you prefer. Change the activity so it extends Activity instead of ActionBarActivity or whatever the ADT makes it. Remove the skeleton Fragment class declared in the actvity and the parts of onCreate that refer to these things.

I also usually delete the menu methods in the activity and the menu XML in res. And of course the bogus strings like "Hello World" from strings.xml

Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82
1

On your machine navigate to your SDK folder then \tools\templates\activities

Copy the BlankActivities folder and paste it into the same directory.

Rename BlankActivities_copy to EmptyActivities

Go to https://android-review.googlesource.com/#/c/88890/4

Under the Files section there is a list of links. Each link directs you to the xml that needs to be added to the new EmptyActivities folder you created.

Copy the xml from the right pane to the path\file at the top of the left pane(the link was also the path). If the file already exists delete the contents and replace. If the file doesn’t exist, create it and copy the xml into the file.

Repeat for each of the links.

Close and reopen Eclipse. If you have any of the files open in an editor Eclipse will crash.

and then: on the create activity screen "Empty Activity" should be an option. After picking it and clicking next the Empty Activity form has a default Layout Name of "activity_main" that will show up as res/layout/activity_main.xml in Eclipse.

Jigar
  • 791
  • 11
  • 21
1

As far as I know there's currently no way to keep Android from creating a fragment when you create an Activity. To do that in ADT my suggestion is that you can uncheck the "Create Activity" check box and create a new Activity, but that's really all you can do.

Lal
  • 14,726
  • 4
  • 45
  • 70
0

On your machine navigate to your SDK folder then \tools\templates\activities

Copy the BlankActivities folder and paste it into the same directory.

Rename BlankActivities_copy to EmptyActivities

Go to https://android-review.googlesource.com/#/c/88890/4

Under the Files section there is a list of links. Each link directs you to the xml that needs to be added to the new EmptyActivities folder you created.

Copy the xml from the right pane to the path\file at the top of the left pane(the link was also the path). If the file already exists delete the contents and replace. If the file doesn’t exist, create it and copy the xml into the file.

Repeat for each of the links.

Close and reopen Eclipse. If you have any of the files open in an editor Eclipse will crash.

hope this will solve your problem

Ajit Kumar
  • 534
  • 3
  • 8
  • Did you really just copy the answer @Jigar posted? – SvenT23 Apr 23 '14 at 13:58
  • no dear i didn't just copy pasted his code i think we both read this from same source and pasted it here. i was not aware that somebody answered same thing. – Ajit Kumar Apr 24 '14 at 07:09
0

Very simple way. Copy the name of your main activity (activity_main) and paste ( replace ) it in fragment layout column. It will remove fragment layout. I tried it many times. It works perfectly for me.

Nouman Ghaffar
  • 223
  • 1
  • 2
  • 6
0

The answer is simple. When creating a new project, the Empty Activity option should be selected.

Welcome to Android Studio

New Project

and so only activity_main.xml and MainActivity.java file as you want.

done

Droidbane
  • 91
  • 2
  • 10