ApiDemos:
Application Programming Interface Demonstrations

The ApiDemos app contains hundreds of stand-alone activities (i.e., subclasses of class Activity) that demonstrate things you’ll want your apps to do. The second screenshot shows the simplest demonstration:
App → Activity → Hello World

Source code on GitHub

Point your browser at
https://github.com/android/platform_development/tree/master/samples/ApiDemos

  1. app/java/com.example.android.apis/ApiDemos.java defines a subclass of class ListActivity named ApiDemos. The first screenshot shows its ListView.
  2. app/manifests/AndroidManifest.xml: lines 61–67 declare an Activity whose name is ApiDemos.
  3. app/res/values/strings.xml: line 18 contains the string resource named activity_sample_code, specified in line 52 of AndroidManifest.xml.
  4. app/res/drawable-hdpi/app_sample_code.png: the app’s icon, specified in line 53 of AndroidManifest.xml.

Create the project

Make sure you have the samples directory on your Mac or PC. In Android Studio, pull down
Tools → Android → SDK Manager
and click on Launch Standalone SDK Manager. Under Android 6.0 (API 23), install the Samples for SDK if they are not already installed.

In Android Studio, pull down
File → New → Import Project…
Select Eclipse or Gradle Project to Import
Select your Eclipse project folder, build.gradle or settings.gradle
/Users/myname/Library/Android/sdk/samples/android-23/legacy/ApiDemos
OK
Import Destination Directory: /Users/myname/AndroidStudioProjects/ApiDemos
Next
Finish

Rename a file

Andtoid Studio will display the error message
/Users/myname/AndroidStudioProjects/ApiDemos/app/src/main/res/xml/preference_switch
Error: the file name must end with .xml
In the Android Studio project view, control-click on the file app/res/xml/preference_switch and select
Refactor → Rename…
Rename
Rename file preference_switch and its usages to: preference_switch.xml
Refactor

Add a library dependency

The file app/java/com.example.android.apis/app/PrintBitmap.java tries to import the class android.support.v4.print.PrintHelper in line 23. This class belongs to the V4 Support Library. To use this library, add

        minSdkVersion 15
to the defaultConfig section of the file build.gradle (Module: app) immediately below the applicationId line. And while you’re at it, change the compileSdkversion from 21 to 23 because ApiDemos uses features that were introduced in API 23. Then pull down
Tools → Android → Sync Project with Gradle Files

In Android Studio, pull down
File → Project Structure…
Select app in the left panel, click on the Dependencies tab, and press the plus sign in the lower left corner. Select Library dependency.
Choose Library Dependency.
Select support-v4 (com.android.support:support-v4:23.0.0)
OK
OK
Your build.gradle (Module: app) file should now end with the following:

dependencies {
    compile 'com.android.support:support-v4:23.0.0'
}

In the AndroidManifest.xml file, add the attribute xmlns:tools="http://schemas.android.com/tools" to the <manifest> element, and add the following element to the <manifest> element.

    <uses-sdk tools:overrideLibrary="android.support.v4"/>

Add a jar file to the project

The file app/java/com.example.android.apis/os/MmsMessagingDemo.java tries to import the Java class com.google.android.mms.ContentType in line 19. We will have to download a .jar file (“Java Archive”) that contains this class and add the .jar file to the project.

Point your browser at search.maven.org and search for com.google.android.mms.ContentType. If that doesn’t find anything (“Too few results?”), follow its suggestion to search for fc:com.google.android.mms.ContentType. Click on the version number of the first item it finds, 4.1.2-r1-rc. In the table at the bottom of the page, click on android-base-4.1.2_r1_rc-real.jar.
This type of file can harm your computer. Do you want to keep android-base-4.1.2_r1_rc-real.jar anyway?
Keep

To verify that this file arrived safely, ask for a table of contents:

cd ~/Downloads
pwd
/Users/myname/Downloads

jar -tf android-base-4.1.2_r1_rc-real.jar | grep ContentType
android/widget/Editor$InputContentType.class
com/google/android/mms/ContentType.class
com/google/android/mms/pdu/PduContentTypes.class

In the dropdown menu at the top of the Android Studio project view, select Project Files instead of Android. Open up the folders as far as

▼ ApiDemos
   ▶ .gradle
   ▶ .idea
   ▼ app
      ▶ build
      ▶ src
Note that the app folder does not contain a libs folder. We will create one. Select the app folder and pull down
File → New → Directory
Enter new directory name: libs
OK

Copy the file android-base-4.1.2_r1_rc-real.jar into the new libs folder. In the Macintosh finder, control-click on android-base-4.1.2_r1_rc-real.jar and select Copy “android-base-4.1.2_r1_rc-real.jar”. In the Android Studio project view, control-click on libs, select Paste, and press OK.

Control-click on the android-base-4.1.2_r1_rc-real.jar in the libs folder and select Add As Library….
Create Library
Add to module: app
OK
In the dropdown menu at the top of the Android Studio project view, select the customary Android. Notice that the dependencies section of the file build.gradle (Module: app) now contains

    compile files('lib/android-base-4.1.2_r1_rc-real.jar')

Add the following to the defaultConfig section of the android section of build.gradle (Module: app).

        multiDexEnabled = true
Append the following section to the android section of the build.gradle (Module: app) file.
    dexOptions {
        incremental true
        javaMaxHeapSize "2048M"
    }
After all these changes, your build.gradle (Module: app) file should now contain the following.
apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"

    defaultConfig {
        applicationId "com.example.android.apis"
        minSdkVersion 15
        testApplicationId "com.example.android.apis.tests"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
        multiDexEnabled = true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    dexOptions {
        incremental true
        javaMaxHeapSize "2048M"
    }
}

dependencies {
    compile 'com.android.support:support-v4:23.0.0'
    compile files('libs/android-base-4.1.2_r1_rc-real.jar')
}
Pull down
Tools → Android → Sync Project with Gradle Files

Run the project

Press the Android Studio run button.

Application Installation Failed
Installation failed with message null. It is possible that this issue is resolved by uninstalling an existing version of the apk if it is present, and then re-installing.
WARNING: Uninstalling will remove the application data!
Do you want to uninstall the existing application?
OK

Interesting, modest demos

Launch ApiDemos and select
App → Activity → Hello World

  1. App → Activity → Hello World
    1. app/java/com.examples.android.apis/app/HelloWorld.java defines a subclass of class Activity named HelloWorld. Its full name is com.example.android.apis.app.HelloWorld.
    2. app/res/layout/hello_world.xml contains a TextView that fills the screen (match_parent, match_parent). The text is centered in the TextView because of the android:gravity.
    3. app/res/values/strings.xml contains the following string resource:
      1. hello_world. The string contains the pairs of HTML tags <B> (bold) and <I> (italics).
    4. app/manifests/AndroidManifest.xml declares an Activity whose name is HelloWorld.

  2. Views → Buttons
    1. app/java/com.examples.android.apis/view/Buttons1.java defines a subclass of class Activity named Buttons1.
    2. app/res/layout/buttons1.xml contains three Buttons in a vertical LinearLayout.
    3. app/res/values/strings.xml contains the following string resources:
      1. buttons_1_normal
      2. buttons_1_small
      3. buttons_1_toggle. This string is not displayed by the ToggleButton.
    4. app/manifests/AndroidManifest.xml declares an Activity whose name is Buttons1.

  3. Views → Controls → 1. Light Theme
    1. app/java/com.examples.android.apis/app/Controls1.java defines a subclass of class Activity named Controls1.
    2. app/res/layout/controls_1.xml contains many controls, including a Button whose id is button_disabled.
    3. app/res/values/strings.xml contains the following string resources:
      1. controls_1_save
      2. controls_1_checkbox_1
      3. controls_1_checkbox_2
      4. controls_1_radiobutton_1
      5. controls_1_radiobutton_2
      6. controls_1_star
      7. textColorPrimary
      8. textColorSecondary
      9. textColorTertiary
      10. listSeparatorTextViewStyle
    4. app/manifests/AndroidManifest.xml declares an Activity whose name is Controls1.

  4. Views → Lists → 01. Array (500 types of cheese with an Adapter and an AdapterView)
    1. app/java/com.examples.android.apis/view/List1.java defines a subclass of class ListActivity named List1.
    2. app/java/com.examples.android.apis/view/Cheeses.java defines class Cheeses, which contains an array of 500 Strings.
    3. simple_list_item_1.xml is on your Mac or PC in the directory ~/Library/Android/sdk/platforms/android-23/data/res/layout.
    4. app/manifests/AndroidManifest.xml declares an Activity whose name is List1.

Things to try

  1. In ApiDemos, run the spinning triangle demonstration in
    Graphics → OpenGL ES → OpenGL ES 2.0
    which purports to demonstrate version 2.0 of OpenGL for Embedded Systems. Does it really? Go to the file app/java/com.example.android.apis/graphics/GLES20Activity.java in the Android Studio project view and look at the method detectOpenGLES20. It returns true if your device or emulator has version 2.0, false otherwise. (Note that it uses the getSystemService method we saw in Text.) Use a piece of Toast to find out what detectOpenGLES20 really returns. If it returns false, then
    Graphics → OpenGL ES → OpenGL ES 2.0
    is really doing the demonstration on your device or emulator with OpenGL ES 1.0.
    import android.widget.Toast;
    
    	//easy way to convert boolean to string
    	Toast toast = Toast.makeText(this, "any string " + (info.reqGlEsVersion >= 0x20000), Toast.LENGTH_LONG);
            toast.show();
    
    	//harder way, for purists
    	Toast toast = Toast.makeText(this, Boolean.toString(info.reqGlEsVersion >= 0x20000), Toast.LENGTH_LONG);
            toast.show();
    

    By the way, the Activity in GLES20Activity.java has the methods onResume and onPause that we saw in the flow chart.


  2. Follow the directions in MediaPlayer to make ApiDemos play an audio file in the Music folder.