Reader small image

You're reading from  Hands-On Android UI Development

Product typeBook
Published inNov 2017
Reading LevelExpert
PublisherPackt
ISBN-139781788475051
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Jason Morris
Jason Morris
author image
Jason Morris

Jason Morris has been developing software for as long as he can remember. He's written software for the desktop, the server, for feature phones and for smart phones. He's written in many languages, and deployed in a variety of countries. Jason loves a good programming challenge, and when he's not writing code, or spending time with his family, taking photo's or camping: he's probably thinking about programming. In 2010 / 2011 he wrote Android User Interface Development: A Beginners Guide, which helped many beginner Android developers take their first steps into the realm of User Interface design and development for mobile devices.
Read more about Jason Morris

Right arrow

Organizing project files


Android Studio gives you a fairly standard Java project structure, that is, you have your main source sets, tests, a resources directory, and so on, but that doesn't really cover all of your organizational needs. If you check the project structure we created, you might note some patterns:

  1. You'll first note that only a single Activity was created--MainActivity, but this Activity template has generated four layout files.

  2. Only activity_main.xml is actually referenced by MainActivity; all the other files are included via the resource system.

  3. The next thing to note is that the layout file referenced by MainActivity is named as actvitity_main.xml; this is a standard naming pattern that Android Studio will actually suggest when creating new Activity classes. It's a good idea, because it helps separate layouts used for Activity classes from those used elsewhere.

  4. Next, take a look at the names of the other layout files. Each of them is also prefixed with nav, app_bar, and content. These prefixes help group the layout files logically in a file manager and in the IDE.

  5. Finally, you'll note that the values directory has several XML files in it. The entire values directory is actually treated as one big XML file by the resource compiler, but it helps keep it organized by the type of resources being declared.

Note

Use filename prefixes in the resources directories (especially layouts) to keep things organized. You cannot break things down into subdirectories, so a prefix is the only way to group files together logically. Common prefixes are "activity", "fragment", "content", and "item", which are commonly used to prefix layouts that are used to render list items and so on.

  1. If you open the MainActivity class now, you'll see how the layout is loaded and bound. The first thing MainActivity does when it's created is to call onCreate to its parent class (which is a mandatory step, and failure to do so will result in an exception). Then, it loads its layout file using the setContentView method. This method call does two things at once: it loads the layout XML file, and adds its root widget as the root of the Activity (replacing any widgets that were already there). The R class is defined by the resource compiler, and kept in sync for you by Android Studio. Every file and value resource will have its own unique identifier, which allows you to keep things tightly bound together. Rename a resource file, and its corresponding field will change:
setContentView(R.layout.activity_main);
  1. You'll then note that MainActivity retrieves various widgets that were included in the layout files by their own IDs (also defined in the R class). The findViewById method searches through the Activity layout for a widget with the corresponding id, and then returns it:

// MainActivity.java
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Note

The findviewById method works by traversing all of the widgets in an Activity in a series of loops. There is no lookup table or optimize this process. As such, you should call the findViewById method in onCreate and keep a class-field reference to each of the View objects you'll need.

  1. The preceding code snippet will return the Toolbar object declared in the app_bar_main.xml layout resource file:

<!-- app_bar_main.xml -->
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay" />

Note

findViewById can also be found on the View class, but it's a relatively expensive operation, so when you have widgets that will be used again in an Activity, they should be assigned to fields in the class.

Previous PageNext Page
You have been reading a chapter from
Hands-On Android UI Development
Published in: Nov 2017Publisher: PacktISBN-13: 9781788475051
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Author (1)

author image
Jason Morris

Jason Morris has been developing software for as long as he can remember. He's written software for the desktop, the server, for feature phones and for smart phones. He's written in many languages, and deployed in a variety of countries. Jason loves a good programming challenge, and when he's not writing code, or spending time with his family, taking photo's or camping: he's probably thinking about programming. In 2010 / 2011 he wrote Android User Interface Development: A Beginners Guide, which helped many beginner Android developers take their first steps into the realm of User Interface design and development for mobile devices.
Read more about Jason Morris