Page 4 of 4
- An Activity is the unit of the Android app and it roughly corresponds to one screen full of user interface plus the code to make it work.
- In most cases you will create an Activity for each UI screen you want to present to your user.
- Only one Activity from your app is running at any given time.
- An Activity is single threaded and runs on the UI thread.
- You can set which Activity starts the app in the Manifest - Android Studio sets this to MainActivity by default.
- The Activity has events corresponding to different stages in it lifecycle. The onCreate event is called when the app first starts and this is where you perform all initialization.
- You can also restore the apps state from previous runs at this point.
- The Activity then loads a View or ViewGroup object to create its user interface.
- You can create View/ViewGroup objects in three possible ways: in code, using XML or using the designer to generate the XML.
- The designer is far the easiest way to create a UI.
- By opening the XML file you can use the designer to place widgets corresponding to View objects on the design surface.
- You can use the property window to set the properties of each widget.
- The XML file that the designer creates is used by the Activity to set its UI by creating Java objects that correspond to each of the View objects placed using the designer.
- When you reference a class that isn't defined in the file, i.e. most of them, then you need to add an import statement to the start of the code.
- If you use Alt+Enter when the cursor is positioned within any word that is displayed in red then Android Studio will help you fix the problem.
- You can hook up onClick event handlers defined within the current Activity to the widgets using the Properties window.
- An onClick event handler is just a public function with the signature void myEventHandler(View v)
- The View object parameter is sent to the View object that raised the event. This can be used to access the properties/methods of the View object that the user interacted with.
- To access other View objects you follow the standard pattern of:
- Find the integer id via the string id, set in the designer, of the widget using resource object R.id.stringId.
- Use the integer id to retrieve the Java object corresponding to the widget using findViewById(int Id)
- Work with the object's methods/properties to modify it.
Android Programming In Java:
Starting With an App
Is now available in paperback and ebook.
Available from Amazon.
- Getting Started With Android Studio 3
- The Activity And The UI
- Building The UI and a Calculator App
- Android Events
Extract: Using Lambdas
- Basic Controls
- Layout Containers
- The ConstraintLayout
Extract: Guidelines and Barriers
- UI Graphics A Deep Dive
Extract: Programming the UI ***NEW
- Menus & The Action Bar
- Menus, Context & Popup
- Beginning Bitmap Graphics
Extract: Simple Animation
- Staying Alive! Lifecycle & State
- ListView And Adapters
If you are interested in creating custom template also see:
Custom Projects In Android Studio
In the next chapter we'll discover how to use layouts and some of the details of the basic widgets.
Meanwhile if you have any questions on what we've covered so far please let me know using the comments.
You can download the code for this program from the CodeBin (note you have to register first).
To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter,Facebook, Google+ or Linkedin, or sign up for our weekly newsletter.
Summer SALE Kindle 9.99 Paperback $10 off!!
or email your comment to: firstname.lastname@example.org