|
Navigation: » No topics above this level « Tutorial: Build a weight converter app |
Scroll Prev Top Next More |
Welcome to Code on the Go! This tutorial helps you write, build, and test an Android app using Java. This tutorial contains:
•A specification for the app
•An overview of the app's code
•Step-by-step instructions for building the app in Code on the Go
This tutorial assumes that you have some experience with programming and are familiar with common features found in other IDEs. In the tutorial, sections that begin with a lightbulb icon (
) contain information about navigating the Code on the Go interface.
You will build a tool that converts kilograms to pounds and pounds to kilograms, depending on the user’s selection. When the user enters or changes a numerical value, the app will instantly display the converted measurement.
If you are new to Android development, some instructions may be difficult to follow. Complete text for main_activity.xml and MainActivity.java are provided in the tutorial so you can view the code. The standalone files are available in the CodeOnTheGoProjects/Tutorials folder. Using your file manager, you can move these files to your project directory.
In the app, the user will enter a numerical value that is converted from kilograms to pounds or pounds to kilograms, depending on the selection. The user-provided value is the input measurement, and the value that the app provides is the converted measurement. This can be further divided into the following parts: •The numerical value of the input measurement is the quantity. •The unit of measurement of the input measurement is the input unit. •The app displays an interface with the three following elements: oA text field that lets the user enter an input quantity. oA dropdown menu, which lets the user choose an input unit (either “kg” for kilograms or “lbs” for pounds). oA result field, which displays the converted measurement, including its quantity and output unit (either “kg” for kilograms or “lbs” for pounds). The image below is a screenshot of the finished app that you will create using this tutorial. When the user first opens the app, a default input measurement of 0 lbs will be displayed. If the user changes the input quantity or input unit, then the app will automatically update the result field to display a new converted measurement based on those choices. |
When you open Code on the Go for the first time after installation, the main menu is displayed. 1.On the main menu, tap Create project. The New project page is displayed, which contains templates with starter code for different kinds of projects. (Click here to learn more about templates.) The image below shows Code on the Go’s built-in application templates. 2.Tap the Empty Activity template. A configuration page for the project is displayed. 3.In the App name field, enter a name for your app. In this tutorial, enter the name WeightConverter. To use this tutorial's sample code, you must enter the text WeightConverter exactly as displayed. 4.In the Project language menu, select Java. 5.In the Minimum SDK menu, you can choose the lowest version of Android that your app will support. In this tutorial, we will use API 21: Android 5.0 (Lollipop), which is the default option. 6.In the lower-right corner, tap the Create project button. Co on the Go will initialize the project.
When the project has fully initialized, the "Project initialized" message will be displayed at the bottom of the page. The Build icon (a green triangle) will be displayed at the top of page near the project's name. The image below shows the main project page after the project has initialized. Like most IDEs, Code on the Go provides a file browser to navigate the contents of your project. To use the file browser, follow these steps: 1.Tap the Project menu, which is represented by the “hamburger” icon in the top left corner. 2.Tap any of the folder icons to see the contents. In this tutorial, you will only modify the following two files: •MainActivity.java: The source file for the main activity of the app. •activity_main.xml: The main activity’s layout XML file.
After opening the files, both activity_main.xml and MainActivity.java are displayed in the code editor. To switch between the files, tap the tab that contains the filename. |
Code on the Go contains detailed documentation on several subjects, including Java and Kotlin keywords, the Java API, the Kotlin Standard Library, and the Android API. If you need help with any item in the Code on the Go interface, you can long-press that item to display a tooltip. To view more information, tap +See more. If you need additional help, each tooltip contains a link to a more detailed documentation page. When you're using the editor, you can view tooltips for text that is displayed in the editor window including keywords, classes, packages, and more. To display a tooltip for text in the editor, follow these steps: 1.Select the text string. 2.Long-press the text string. The code editing toolbar will be displayed. 3.Tap the Help icon (the question mark at the right side of the code editing toolbar). A tooltip will be displayed. 4.To access more information, tap +See more. 5.To access the complete documentation, tap the link at the bottom of the tooltip. To return to the editor, tap Back to Code on the Go at the top of the page. |
|
In this step, you will build the app so you can view the layout on your device. To build the app, tap the Build icon (the green triangle) in the slider on the top right side of the screen. Code on the Go will begin building your app. Like when you initialized the project, a progress indicator is displayed at the top of the page and status messages are displayed at the bottom. When the build is finished, a message about the build's success or failure will be displayed. To view more information, including details on the build process, swipe up to display the Build Output panel. (Learn more about output panels.) After the build is finished successfully, you have built the app! (Later in this tutorial, you will make changes and build the app again.) The first time you successfully build an app, a prompt will ask if you want to install it on your device. Tap Yes to install the app. After you tap Install, Android will display a security warning because the app you just build has not been verified as secure by Google Play Protect. The text and appearance of this prompt will differ depending on your device. The image below shows an example of the security warning. To proceed, tap Scan app. Android will scan the app to ensure that it isn’t harmful. When the scan is complete, tap Install. After the app is installed, you'll be asked if you want to launch the app. Tap Yes to launch the app. The app will open and display the layout you created. Because we haven't written any of the app's logic, only the layout elements are displayed. Next, you will write the Java code to convert units that the user enters. |
Before you edit MainActivity.java, let’s look at three useful features of the code editor in Code on the Go. These tools can help you write code faster and fix problems more easily. Auto-complete suggestions As you type, Code on the Go will suggest ways to complete your code. A pop-up list will contain options to complete the text you've started to type, including: •Variable names •Class names •Keywords (such as if, while, or return) •Common code snippets This can save you time and help you avoid mistakes. Learn more about autocomplete. Code actionsFor Java, Code on the Go also includes special time-saving features called code actions. To use code actions, follow these steps: 1.Long-press to select any text in the code editor. The code editing toolbar is displayed. 2.Tap the angle brackets (< >) on the left side of the toolbar. A menu of options is displayed. Code actions can automate common tasks including: •Adding or removing comments in the code •Finding where a variable is used or declared •Automatically creating setters/getters, constructors, and toString() •Organizing imports and removing unused imports Learn more about code actions. Diagnostics (error checking)Code on the Go displays a red wavy line under any part of your code that may need correction. In addition, the built-in diagnostics can identify several potential problems, including typing errors, the use of a variable that has not been created, the wrong type of value for a variable, and more. Learn more about diagnostics. |
For the unit converter to work, you must add event listeners to both the input quantity field and dropdown menu. These listeners will detect any changes made by the user. For example, the user may enter a new input quantity or switch the unit type from "kg" to "lbs." When a change is detected, the listener will call a method named updateConversionResult that will: •Read the quantity and unit of the current input measurement. •Perform the conversion based on the input measurement. •Update the result field to display the converted measurement quantity and unit (for example, 45.36 kg or 100.0 lbs). The updateConversionResult method causes the result field to immediately display a new measurement conversion any time the user changes an input. The next step is to add options to the Spinner dropdown. The Spinner is a type of view called AdapterView, which uses an Adapter to display a list of options. In this tutorial, you will create an ArrayAdapter that uses a list of strings containing two items: kg and lbs. The Adapter will use a simple layout to display options in the dropdown menu. We'll begin by replacing the contents of MainActivity.java with the code for this part of the project.
|
|
After you've entered all the code, it's a good idea to look at the built-in diagnostics to identify typos and other kinds of errors. •For Java projects, Code on the Go adds a red wavy underline to code that may have problems. Please note that the diagnostics may flag error-free code, notably import statements that refer to your app's own generated code and not to external libraries. (For example, diagnostics will flag the statement import com.example.weightconverter.databinding.ActivityMainBinding; because it's not a recognized outside class, even though it's valid and required for your app.) •The Diagnostics panel contains a complete list of potential issues. To display the diagnostics panel, swipe up from the bottom of the page and then tap the Diagnostics tab. Tap anything in the list to go to that item in the code editor. (Learn more about output panels.) |
When you've entered all of the code and resolved any problems , tap the green Build icon in the action bar to rebuild and run it. When your app finishes building, tap Update application. Congratulations! You have built your first app using Code on the Go. |
