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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With