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
Point your browser at
https://github.com/android/platform_development/tree/master/samples/ApiDemos
app/java/com.example.android.apis/ApiDemos.java
defines a subclass of class
ListActivity
named
ApiDemos
.
The first screenshot shows its
ListView
.
app/manifests/AndroidManifest.xml
:
lines 61–67
declare an
Activity
whose name is
ApiDemos
.
app/res/values/strings.xml
:
line 18
contains the
string
resource
named
activity_sample_code
,
specified in
line 52
of
AndroidManifest.xml
.
app/res/drawable-hdpi/app_sample_code.png
:
the app’s icon,
specified in
line 53
of
AndroidManifest.xml
.
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
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
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 15to 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
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"/>
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 ▶ srcNote that the
app
folder does not contain a
libs
folder.
We will create one.
Select the
app
folder and pull down
libs
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 = trueAppend 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
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
Launch ApiDemos and select
App →
Activity →
Hello World
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
.
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
.
app/res/values/strings.xml
contains the following
string
resource:
hello_world
.
The string contains the pairs of HTML tags
<B>
(bold)
and
<I>
(italics).app/manifests/AndroidManifest.xml
declares an
Activity
whose name is
HelloWorld
.
app/java/com.examples.android.apis/view/Buttons1.java
defines a subclass of class
Activity
named
Buttons1
.
app/res/layout/buttons1.xml
contains three
Button
s
in a
vertical
LinearLayout
.
app/res/values/strings.xml
contains the following
string
resources:
buttons_1_normal
buttons_1_small
buttons_1_toggle
.
This string is not displayed by the
ToggleButton
.app/manifests/AndroidManifest.xml
declares an
Activity
whose name is
Buttons1
.
app/java/com.examples.android.apis/app/Controls1.java
defines a subclass of class
Activity
named
Controls1
.
app/res/layout/controls_1.xml
contains many controls, including a
Button
whose
id
is
button_disabled
.
app/res/values/strings.xml
contains the following
string
resources:
app/manifests/AndroidManifest.xml
declares an
Activity
whose name is
Controls1
.
Adapter
and an
AdapterView
)
app/java/com.examples.android.apis/view/List1.java
defines a subclass of class
ListActivity
named
List1
.
app/java/com.examples.android.apis/view/Cheeses.java
defines class
Cheeses
,
which contains an array of 500
String
s.
simple_list_item_1.xml
is on your Mac or PC in the directory
~/Library/Android/sdk/platforms/android-23/data/res/layout
.
app/manifests/AndroidManifest.xml
declares an
Activity
whose name is
List1
.
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
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
onResume
and
onPause
that we saw in the
flow
chart.