Advertisement
Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

Turning Standard Oracle Java into Android Java

Part of the Java Programming for Android Developers For Dummies Cheat Sheet

Here's the scenario: You have a killer app, written in Java. The app runs on the Windows and Macintosh operating systems. The app doesn't run on a mobile device (because Java SE programs don't run on mobile devices).

You want to know how the app looks when it runs on an Android device. And here's one more thing: You don't want to do any serious thinking. That would involve too much stress!

A Java program for a desktop or laptop computer

Can you quickly and mechanically turn the app into an Android app? You can. Start with the simple program in this listing.

package com.allmycode.tickets;
import javax.swing.JOptionPane;
public class TicketPrice {
  public static void main(String[] args) {
    String ageString;
    int age;
    String price;
    
    ageString = JOptionPane.showInputDialog("Age?");
    age = Integer.parseInt(ageString);
    
    if (age < 18 || 65 <= age) { 
      price = "$7.00";
    } else {
      price = "$10.00";
    }
    
    JOptionPane.showMessageDialog(null, 
        price, "Ticket price", 
        JOptionPane.INFORMATION_MESSAGE);
  }
}

The code in this listing prompts the user for a number (the user's age). If the user is a child or a senior, the ticket price is only $7.00. Otherwise, the ticket price is $10.00.

To convert this listing into an Android app, do the following:

  1. Using Eclipse, create an Android project.

  2. In the main activity's layout file, create at least three widgets: an EditText widget, a Button widget, and a TextView widget.

A res/layout/activity_main.xml file

Here's a sample layout file.

<RelativeLayout xmlns:android=
    "http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight=
    "@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity" >
  <EditText
      android:id="@+id/editText1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="16dp"
      android:ems="10"
      android:hint="@string/enter_age"
      android:inputType="number" >
      <requestFocus />
  </EditText>
  <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="18dp"
    android:onClick="whenButtonClicked"
    android:text="@string/submit" />
  <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="29dp"
    android:text="@string/ticket_price"
    android:textAppearance=
      "?android:attr/textAppearanceMedium" />
</RelativeLayout>

In this listing, the editText1 field is where the user enters his or her age. The user clicks the button1 widget to tell the app to calculate a ticket price, and then the ticket price appears in the textView1 field.

In the next step, you give meaning to the @string things from this second listing.    

A res/values/strings.xml file

Add string elements to the app's strings.xml file, as shown in this listing.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Ticket Price</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="enter_age">Enter your age here</string>
    <string name="submit">Submit</string>
    <string name="ticket_price">Ticket price</string>
</resources>

The boldface lines in this listing provide meanings for the names @string/enter_age, @string/submit, and @string/ticket_price in the activity's layout file.

This is your chance to make your app usable for readers all around the world. For example, to create the French translation of your app, add a res/values-fr directory to the project. In a strings.xml file in the res/values-fr directory, use the same name attributes (enter_age, submit, and ticket_price). But between each start tag and end tag, put the French translation of the text that appears on the user's screen. When French-speaking customers use your app, they see the text in their native tongue.    

The main activity

Add code to the main activity's Java file, as shown in this listing.

package com.example.ticketprice;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
  EditText editText;
  TextView textView;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText = (EditText) findViewById(R.id.editText1);
    textView = (TextView) findViewById(R.id.textView1);
  }
  public void whenButtonClicked(View view) {
    String ageString;
    int age;
    String price;
    ageString = editText.getText().toString();
    age = Integer.parseInt(ageString);
    if (age < 18 || 65 <= age) {
      price = "$7.00";
    } else {
      price = "$10.00";
    }
    textView.setText(price);
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
}

In Listing 2, the button's onClick attribute has value whenButtonClicked. So in Listing 4, you respond to a click of the button with your new whenButtonClicked method. This whenButtonClicked method contains most of the statements that were in your original standard Java program. The big difference is the way you get text from the user, and the way you display results on the user's screen.

  • In the standard Java program you use JOptionPane to create dialog boxes. The boxes prompt the user for input, and display the program's results.

  • In the Android Java program, you use the widgets on the activity's screen for input and output. Having declared editText near the start of the code, you call editText.getText().toString() to fetch the user's input. And having declared textView near the start of the code, you call textView.setText(price) to display the program's results.

And there you have it — an Android program from a standard Java program!

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win an iPad Mini. Enter to win now!