Scala on the phone

Source: Internet
Author: User
Tags scala ide

Prerequisite

In this article, we'll create a mobile application that runs on an Android device. You will need to install the Android SDK; This article uses the V1.5 SDK. Application code will be written in the Scala programming language. If you've never used Scala, then it doesn't matter, because this article will explain the Scala code. However, even if you are unfamiliar with Scala, it is recommended that you be familiar with the Java language at least. This article is developed using Scala V2.7.5. Good Eclipse plug-ins are available for both Android and Scala. This article uses the Eclipse V3.4.2 and the Android Development Tools (ADT) V0.9.1 and the Scala IDE plug-in V2.7.5.

Set up

Writing an Android application sounds like a complex proposition. Android applications run in their own virtual machines: Dalvik virtual machines. However, the Android application's build path is open. The basic strategy we will use is shown below.

Figure 1. On Android, Scala's Build path

The idea is that we first compile all the Scala code into Java class files. This is the work of the Scala compiler, so there's nothing too complicated about it. Next, get the Java class file and use the Android Dex compiler to compile the class files into the format used by the Dalvik VM on the Android device. This is known as dexing, and is also a regular compilation path for Android applications. Typically, you'll experience the process from. java files to. class files to. dex files. In this article, the only difference is that we start with the. scala file. Finally, the. dex file and other application resources are compressed into a APK file that can be installed on the Android device.

So, how do we make this happen? We'll do most of the work with Eclipse. However, there is also a more complex step: You need code from the standard Scala library to run your code. In a typical Scala installation, this is a separate jar in the/lib/scala-library.jar. However, this JAR includes some code that is not supported by Android. Some code needs to be adjusted slightly, and some code must be removed. Scala-library.jar's custom build is the best run, at least for now. We'll call this jar the Android library jar.

With this JAR, the rest of it is easy. Just use Eclipse's ADT plug-in to create an Android project. A Scala feature (nature) is then added to the project. Replace the standard Scala library with the Android library mentioned earlier. Finally, the output directory is added to the classpath. Now it's time to start. This is described in more detail by the main Scala site. Now that we have the basic setup, let's look at the Android apps that we'll create using Scala.

Unitsconverter

Now that we know how to take the Scala code and convert it into a binary format that will run on the Android device, we can then use Scala to create a mobile application. The application we will create is a simple unit conversion application. This application makes it easy to switch back and forth between the Imperial Unit and the metric unit. This is a very simple application, but we will see that even the simplest applications can benefit from using Scala. Let's first look at the Unitsconverter layout elements.

Create a layout

You may be excited about writing Scala running on your phone, but not all mobile development programming should be done in Scala or the Java language. The Android SDK provides a good way to separate user interface code from application logic using an xml-based layout system. Let's take a look at the main layout file for the application in this article, as shown in Listing 1.

Listing 1. The main layout of the Converter application

<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent"
Android:gravity= "Center_horizontal" android:padding= "10px"
>
<textview android:id= "@+id/prompt_label" android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/prompt_metric"/>
<edittext android:id= "@+id/amount" android:layout_below= "@id/prompt_label"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"/>
<textview android:id= "@+id/uom_label"
android:layout_below= "@id/amount"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/uom"/>
<spinner android:id= "@+id/uom_value"
android:layout_below= "@id/uom_label"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
<button android:id= "@+id/convert_button"
android:layout_below= "@id/uom_value"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/convert_button_label"/>
<textview android:id= "@+id/result_value"
android:layout_below= "@id/convert_button"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"/>
</RelativeLayout>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.