You can paste a chunk of Java code into the
onCreate
method of the
ActionBarActivity
object in
MainActivity.java
.
The code pasted into this app
lets you type in two number of type
double
,
miles and gallons,
and then gives them to
your chunk of Java code.
The code then displays the miles per gallon.
When you dismiss the display,
the chunk of code repeats itself with an infinite
for (;;)
loop.
The first and second
dialogs
have a title, message, and an
EditText
whose hint is “Type answer here and press Done.”
The third dialog has a title, message,
and a
positive button
whose text is “OK”.
The file
Bed.zip
contains the source code of the project,
plus files that are not source code.
You can create your own
Bed.zip
file with
these directions.
The only files you have to look at are
activity_main.xml
,
dialog.xml
,
strings.xml
,
and
MainActivity.java
,
in that order.
res/layout/activity_main.xml
consists of a
RelativeLayout
containing one
TextView
.
The
TextView
is not used.
res/layout/dialog.xml
is inserted into each of the first two dialogs.
res/menu/menu_main.xml
is an
options
menu.
res/values/strings.xml
contains a
string
resource
named
hint
.
res/values/styles.xml
res/values/dimens/dimens.xml
gives the dimensions of the margins in
density-independentpixels.
res/values-w820dp/dimens.xml
for wider screens.
res/mipmap-mdpi/ic_launcher.png
48 × 48
res/mipmap-hdpi/ic_launcher.png
72 × 72
res/mipmap-xhdpi/ic_launcher.png
96 × 96
res/mipmap-xxhdpi/ic_launcher.png
144 × 144
app/build/intermediates/res/merged/debug/mipmap-mdpi/ic_launcher.png
48 × 48
app/build/intermediates/res/merged/debug/mipmap-hdpi/ic_launcher.png
72 × 72
app/build/intermediates/res/merged/debug/mipmap-xhdpi/ic_launcher.png
96 × 96
app/build/intermediates/res/merged/debug/mipmap-xxhdpi/ic_launcher.png
144 × 144
AndroidManifest.xml
specifies the subclass of class
Activity
to be instantiated when the app is launched.
It could also give the app
permission to make telephone calls or Internet connections,
etc.build.gradle
(Module: app)
To create the file
dialog.xml
,
select the folder app/res/layout
in the Android Studio
project
view.
File → New… → Layout resource file
New Resource File
File name: dialog
Root element: LinearLayout
OK
In
MainActivity.java
,
import the classes
android.app.AlertDialog
and
android.os.Handler
.
Stop the Genymotion emulator.
Select and press its wrench.
Configuration →
Android system options
Check the checkbox for Use virtual keyboard for text input.
If you don’t do this,
no keyboard will slide up onto the screen,
and you will have to use the Mac or PC keyboard to type into the
EditText
.