Android Adventures - Activity And UI With Android Studio 2
Written by Mike James   
Saturday, 23 July 2016
Article Index
Android Adventures - Activity And UI With Android Studio 2
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 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:
    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.

     text3

    androidJavaSmallAndroid Programming In Java:
    Starting With an App
    Third Edition

    Is now available in paperback and ebook.

    Available from Amazon.

     

     

    1. Getting Started With Android Studio 3
    2. The Activity And The UI
    3. Building The UI and a Calculator App
    4. Android Events
           Extract: Using Lambdas
    5. Basic Controls
    6. Layout Containers
    7. The ConstraintLayout
          Extract: Guidelines and Barriers
    8. UI Graphics A Deep Dive
          Extract: Programming the UI ***NEW
    9. Menus & The Action Bar
    10. Menus, Context & Popup
    11. Resources
    12. Beginning Bitmap Graphics
          Extract: Simple Animation
    13. Staying Alive! Lifecycle & State
    14. Spinners
    15. Pickers
    16. ListView And Adapters

    If you are interested in creating custom template also see:

    Custom Projects In Android Studio

    Androidgears

     

     

    Coming Next

    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,FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

     

     

    raspberry pi books

     

    Comments




    or email your comment to: comments@i-programmer.info



    Last Updated ( Monday, 27 March 2017 )