Java testbed using a dialog

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”.

Source code in Bed.zip

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.

  1. Java files
    1. MainActivity.java. The onCreate method of the Activity object is called automatically when the app is launched.
    2. R.java. It creates the integer variables R.layout.activity_main, R.layout.dialog, and R.id.editText. Do not edit this file.

  2. XML layout files
    1. res/layout/activity_main.xml consists of a RelativeLayout containing one TextView. The TextView is not used.
    2. res/layout/dialog.xml is inserted into each of the first two dialogs.

  3. Other XML resource files
    1. res/menu/menu_main.xml is an options menu.
    2. res/values/strings.xml contains a string resource named hint.
    3. res/values/styles.xml
    4. res/values/dimens/dimens.xml gives the dimensions of the margins in density-independentpixels.
    5. res/values-w820dp/dimens.xml for wider screens.

  4. Launcher icons, with dimensions in pixels.
    1. res/mipmap-mdpi/ic_launcher.png 48 × 48
    2. res/mipmap-hdpi/ic_launcher.png 72 × 72
    3. res/mipmap-xhdpi/ic_launcher.png 96 × 96
    4. res/mipmap-xxhdpi/ic_launcher.png 144 × 144
    5. app/build/intermediates/res/merged/debug/mipmap-mdpi/ic_launcher.png 48 × 48
    6. app/build/intermediates/res/merged/debug/mipmap-hdpi/ic_launcher.png 72 × 72
    7. app/build/intermediates/res/merged/debug/mipmap-xhdpi/ic_launcher.png 96 × 96
    8. app/build/intermediates/res/merged/debug/mipmap-xxhdpi/ic_launcher.png 144 × 144

  5. 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.

  6. build.gradle (Module: app)

Create the project

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.

Run the project

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.