Timbo
Timbo

Reputation: 173

New Android Studio Activity Design Pattern content_main.xml

I started learning Android, but in new Android Studio 1.4 using blank activity creates two xml files - activity_main and content_main - from what I read this is the new design pattern, but no relatively new tutorial (<1yr) mentions this and operates with blank activity creating only activity_main.

Is there any way around it? Is it possible to create your own activity template or just create activity_main without content_main?

Learning Android for beginner is already enough of a hassle without manually creating java and xml from empty activity every time or trying to "translate" files from tutorials into new design pattern while trying to learn.

Upvotes: 7

Views: 10691

Answers (4)

Martin
Martin

Reputation: 727

If you look in activity_main.xml (or main_activity.xml - whatever it's called), you should see the line <include layout="@layout/content_main"/>. As you might expect, this includes the content of content_main.xml in this position within the activity_main.xml layout. To make things easier to understand in conjunction with your book, you can just cut and paste everything in content_main.xml to replace that line.
There are a couple of reasons why <include /> is useful. First, it allows you to reuse your layout xml across multiple files. Second, it means that you can specify different layouts depending on the configuration of the device. For an example of this, take a look at what happens with activity_item_list.xml and item_list.xml when you make a new Master/Detail Flow.

Upvotes: 1

N Suhaib
N Suhaib

Reputation: 181

There IS a way around it. When you create a new project, on the "Add an activity page", instead of "Blank Activity", choose "Empty Activity". That will generate two files; a java file and an xml file, just like the previous versions. So file > New > New Project

Upvotes: 3

user1941659
user1941659

Reputation:

you can use an empty template and add your own fragments for the old way, but i suggest using the content_main.xml as there has to be a reason whey android is implementing it with a blank activity. as long as you leave the 'include' tag in the activity_main and but your views(textview, button, etc) in the content_main.xml. i havent found suffectient reasoning for this extra layout but, as a new android developer, i will be using it in an assumption that it is, will be, best practice. as far as tutorials not being current with this method... i just add my views and/or fragments to the content_main. youll also come across tutoriald with depricated code. omg, this was so frustating for me at first, but chances are youre not the first to realize this and a quick google search will usually show you the alternative. also i suggest learning with the free udacity/google course. simply put... its great.

heres a link to the udacity/google course. dont click the 'start free trial' click the button below that which says 'course materials'. these people are very thorough and provide updated code for each step of each lesson. some classes used are deprecated but they try to let you know if theres a better way OR if not, you can usually find help in the comments for the lesson's code.

https://www.udacity.com/course/developing-android-apps--ud853

Upvotes: 0

young_08
young_08

Reputation: 1216

Just delete the extra xml's and files . And it would be like old Template . For example :

Delete content_main.xml and all the error comes after doing this . And it would back to old Template . Also , you can choose Empty Activity , it would be like old one more .

see this and u would get your answer : What is the role of content_main.xml in android studio 1.4?

Upvotes: 0

Related Questions