Android Adventures - Activity And UI
Android Adventures - Activity And UI
Written by Mike James   
Tuesday, 10 February 2015
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 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 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:
    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(int Id)
    3. Work with the object's methods/properties to modify it.

     ui2

    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. Basic Controls And Events
    5. Layout Containers
    6. UI Graphics A Deep Dive
    7. Menus & The Action Bar
    8. Menus, Context & Popup
    9. Resources
    10. Graphics - Coming Soon
    11. Lifecycle and State
    12. Spinners
    13. Pickers
    14. ListView And Adapters

      Fragments
    15. Introducing Fragments
    16. Fragments and XML
    17. Fragment And Activity Working Together
    18. Managing Fragments
    19. Custom dialogs using DialogFragment
    20. Dialog Classes In DialogFragment
    21. A NumberPicker DialogFragment Project
    22. ViewPager

    If you are interested in creating custom template also see:

    Custom Projects In Android Studio

    Androidgears

     


    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 ( Tuesday, 10 February 2015 )
     
     
    Banner

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