Sans Pareil Technologies, Inc.

Key To Your Business

Lab 1: Grid View Application

We will build a simple data driven (AdapterView) application that we will enhance throughout the course with features as we cover them in the course.  The application will use a GridView to display a set number of cells

  • Create a new project in Android Studio
  • Name it GridViewSample or similar
  • Assign mis142 as Company Domain.  This is used as part of the package name for the Java classes.
  • Use Empty Activity in the Activity screen
  • Edit activity_main.xml to make it use a GridView layout.  Note that GridView layouts cannot include any other widgets (other that those we supply to it via its Adapter class).
    • Change the RelativeLayout XML tags to GridView
    • Delete the TextView which displays "Hello World!"
    • Assign a custom identifier to the GridView (android:id="@+id/gridView")
    • Add an attribute that will automatically fit columns as screen/device width allows (android:numColumns="auto_fit")
  • Create a new Java class called NumberAdapter in your project.  This is our Adapter implementation which will specify the number of items the GridView should display, as well as the type of View to be rendered in each cell in the GridView.
    • Make the class inherit from BaseAdapter
    • Let the IDE generate the required methods from parent class.
    • Introduce a final field for the Android Context (type/class name) named context.
    • Let IDE generate a constructor for the context field.
    • Implement the getCount and getView methods as appropriate. 
      • Return a fixed number from the getCount method (eg. 100).  Android invokes this method at first to determine the number of cells to be rendered.  If this returns 0, no cells will be rendered.
      • Make the getView method return a TextView with text "Cell i" where i is the cell number in the grid.  
      • When creating a new TextView set the layout parameters (egtextView.setLayoutParams( new GridView.LayoutParams( 150, 120 ) ))
      • Also set a padding around the text view (egtextView.setPadding( 8, 8, 8, 8 );)
  • Edit the MainActivity.java file
    • In the onCreate method retrieve a reference to the GridView (eg. final GridView gridview = (GridView) findViewById( R.id.gridView );)
    • Set the adapter for the grid view (gridview.setAdapter( new NumberAdapter( this ) );)
  • Run the application in the emulator
  • Add logging to the getView method to see when views are reused.
  • Set a break point in the getView method and step through code.
  • Remove v7 appcompact support as documented on page 58 in the book.
  • Optimize the NumberAdapter.getView implementation.  Android will try to re-use the View's that you have already created - when you scroll, Android will attempt to use the same View's that were originally placed on screen, and give you the option of just updating the information displayed in the View as appropriate.
    • Use the convertView parameter passed in to determine whether you need to create a new TextView instance or not.
    • If convertView is not null, it can be type cast to a TextView, and only its text needs to be set.
    • If convertView is null, you will need to create and initialize the TextView as you had done earlier.