Android Adventures - Activity And UI
Written by Mike James   
Monday, 27 May 2013
Article Index
Android Adventures - Activity And UI
Creating a UI
Connecting the Activity to the UI
Summary

Summary 

  • 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 you 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 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 object that correspond to each of the View objects placed using the designer. 

  • 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 myEventHandler(View v)

  • The View object parameter is set 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:
    1. Find the integer id via the string id, set in the designer, of the widget using resource object R.id.stringId.
    2. Use the integer id to retrieve the Java object corresponding to the widget using findViewById(intId)
    3. Work with the object's methods/properties to modify it.

    Androidgears

    Android Adventures With Android Studio

      

     

    Contents

    1. Getting Started With Android Studio
    2. The Activity And The UI
    3. Building The UI and a Calculator App
    4. Lifecycle and State
    5. Basic Controls And Events
    6. Spinners
    7. Pickers
    8. UI Graphics A Deep Dive
    9. Introducing Fragments
    10. Fragments and XML
    11. Fragment And Activity Working Together
    12. Managing Fragments
    13. Dialog Fragments

    See Also:

    Custom Projects In Android Studio

    Coming Next

    In the next installment 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 and for the simple blank activity template 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,FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

     

    blog comments powered by Disqus



    Last Updated ( Wednesday, 09 April 2014 )
     
     

       
    Copyright © 2014 i-programmer.info. All Rights Reserved.
    Joomla! is Free Software released under the GNU/GPL License.