To Examples

BeerAdvisor Example

Goal

Directions

  1. Create an Android Studio project named BeerAdvisor.
  2. Replace the ConstraintLayout with LinearLayout:
    Using the Design Editor in the Component Tree,
    right click on Constraint Layout >>
    Right click on Convert View... >>
    Select LinearLayout >> Click Apply

    Set the orientation attribute of the linear layout to vertical.
  3. Delete the existing textview with text "Hello, World"
  4. From the Palette in the Text Group, drag a TextView widget onto the layout.
    This textview will display the recommended brands of beer for a given color.
  5. Set these properties of the textview widget:
    Attribute Value
    id txt_brands
    layout_height wrap_content
    layout_width wrap_content
    layout_margin 16dp
    text  
    textSize 24sp

  6. From the Palette in the Buttons Group, drag a button onto the layout below the textview.
    When this button is clicked, the recommended brands for a given beer color will appear in the textview.
  7. Set these properties of the button:
    Attribute Value
    id txt_brands
    layout_height wrap_content
    layout_width wrap_content
    text Find Beer
    padding 16dp
    textSize 24sp
    textAllCaps false

  8. From the Palette in the Containers group, drag a Spinner widget on the layout below the textview and button.
  9. Set these properties of the spinner:
    Attribute Value
    id spnr_beer_color
    layout_height wrap_content
    layout_width match_parent
    layout_margin 16dp

  10. Add an array of strings with name beer_colors and items light, amber, brown, and dark to the strings resource file strings.xml like this:
    <string-array name="beer_colors">
        <item>blond</item>
        <item>amber</item>
        <item>brown</item>
        <item>dark</item>
    </string-array>
    
    The resulting strings resource file:
         strings.xml
  11. Set the entries attribute of the spinner to reference the beer_colors strings array:
    android:entries="@array/beer_colors"
    
  12. Set up an onClick event handler for the button:
    1. In MainActivity.java, enter a header for the onClick event handler:
      public void findBeer(View view) {
         // body to be added later.
      }
      
    2. Add an onClick attribute to the button:
      android:onClick="findBeer"
      
    Here is the resulting layout page:
        activity_main.xml
  13. Add a class named BeerFinder.
    1. To the class, add the instance method getBrands that inputs a color and outputs an ArrayList collection containing the brands that correspond to that color. Here are the inputs and return values:

      Input Return Value
      blond [Glacier Eisbock, Dogfish Head Piercing Pilsner,
      Maui Bikini Blonde, Free State Oktoberfest]
      amber [Fat Tire Amber Ale, Hop Head Red,
      Bell\'s Amber Ale, Flipside Red IPA]
      brown [Sweetwater Georgia Brown, Brooklyn Brown Ale,
      Newcastle Brown Ale,
      Founders Sumatra Mountain Brown]
      dark [Sumi Zest,  Beer for Breakfast,
      Sweet Baby Jesus!, Speedway Stout]
    The BeerAdvisor class:
        BeerAdvisor.java
  14. Now use the BeerAdvisor class in the onClickFindBeer event handler:
    1. Declare and instantiate a BeerAdvisor method named advisor.
    2. Declare a List<String> collection named brands. List is an interface implemented by ArrayList. We use List for the type of the collection class for flexibility in case we later decide to replace the ArrayList collection with a different collection class.
    3. Obtain references for the textbox and spinner from the layout:
      TextView txtBrands = findViewById(R.id.brands);
      Spinner spnrColor = findViewById(R.id.beer_color);
      
    4. Get the selected item from the spinner:
      String beerType = spnrColor.getSelectedItem( ).toString( );
      
    5. Write Java code in the event handler to do the following:
      • Get the beer brands list from the BeerAdvisor object using the getBrands method.
      • Use a StringBuilder object to format the list of brands in a single string with \n characters between the brands.
      • Display the formatted list brands in the text box.
    The resulting activity file:
         MainActivity.java