Sans Pareil Technologies, Inc.

Key To Your Business

Lab 4: Implicit Intent

Modify the simple app built in Lab3b and make it a bit more user friendly.  Instead of a hard-coded Item index, we will use a seek bar to let the user select the item they wish to view.  Note that the seek bar should be set to maximum value that corresponds to what we set in the Database singleton.  We will also use a text view to display the user selected value.  This will also help exercise seek bar change listener.

  • Copy the Lab3b project directory to Lab4
  • Edit the activity layout file
    • Drag and drop a SeekBar widget on to the layout
      • Set the id for the widget to "range".
      • Set the max property to 100 (or whatever your Database instance in Lab3a returns for getCount)
    • Edit the TextView widget and set the id for the widget to "value".
  • Edit the MainActivity class
    • Introduce a utility method to retrieve the value TextView widget.
      private TextView getValueField()
        return (TextView) findViewById( );
    • Create a OnSeekBarChangeListener inner class  This will update the text view with the current value of the seek bar.
      private class RangeListener implements OnSeekBarChangeListener
        public void onProgressChanged( SeekBar seekBar, int progress, boolean fromUser )
          getValueField().setText( valueOf( progress ) );

        public void onStartTrackingTouch( SeekBar seekBar ) {}

        public void onStopTrackingTouch( SeekBar seekBar )
          getValueField().setText( valueOf( seekBar.getProgress() ) );
    • Initialize the SeekBar widget from the onCreate method
      private void initRange()
        final SeekBar seekBar = (SeekBar) findViewById( );
        seekBar.setProgress( 0 );
        seekBar.setOnSeekBarChangeListener( new RangeListener() );
        getValueField().setText( "0" );
    • Modify the button click listener
      • Change the intent.putExtra to use the value displayed in the TextView widget instead of the hard-coded value.
      • Display a Toast message instead of logging if the intent cannot be resolved.
      • Note: You may need to run the application from Lab3a on the emulator/device prior to running this app to ensure that Android is able to resolve the intent to the Lab3a app.
    • Run the app and verify the user interaction.
    • Modify the test suite to test the intent.  Note that Espresso does not support cross-application testing (you will need to use UI Automator for that).  We will however use the espresso-intents library to verify that the intent being generated matches our rules.
      • Add a dependency to the gradle file
        androidTestCompile ''
      • Change ActivityTestRule to IntentsTestRule
      • Add a setProgress method that will create a ViewAction for use with perform.  There are no pre-built view actions that will set/update the value of a seek bar.
        public static ViewAction setProgress( final int progress )
          return new ViewAction() {
            public Matcher<View> getConstraints() { return isAssignableFrom( SeekBar.class ); }

            public String getDescription() { return "Set SeekBar progress value"; }

            public void perform( UiController uiController, View view )
              ( (SeekBar) view ).setProgress( progress );
      • Modify the onClick test method
        public void onClick()
          final int index = 5;
          onView( withId( ) ).perform( setProgress( index ) );
          onView( withId( ) ).perform( click() );
          sleep( 500 );
          intended( hasExtra( "index", index ), times( 1 ) );
      • Run the test.  Change the parameters to the intended invocation and observe the results.