- Basic XML knowledge
- Basic Java knowledge
- Basic Eclipse knowledge
- 2h of your time
- Before you can start you need the Android SDK and a IDE. Android offers a special bundle for that: Android SDK Bundle
- Download the bundle, unzip and run the “SDK Manager.exe”.
- start Eclipse
- Click on “Windows” at the navigation toolbar
- Open “Android Virtual Device manager“
- Open “File“
- “New“
- “Android Application Project“
- Navigate in the package explorer to “/res/layout/” and open “activity_main.xml“
- Right-click on “Hello World” and delete
- Select “/res/values/strings.xml“
- “Add” a new entry
- Select the Color entry – press OK and set the following attributes:
- Name/value: “miles” / “to Miles“
- Name/value: “kmh” / “to km/h“
- Name/value: “calc” / “Calculate“
- <resources>
- <string name="app_name">TutorialApplication</string>
- <string name="action_settings">Settings</string>
- <string name="hello_world">Hello world!</string>
- <color name="myColor">#eeeeee</color>
- <string name="miles">to Miles</string>
- <string name="kmh">to km/h</string>
- <string name="calc">Calculate</string>
- </resources>
- Select “/res/layout/activity_main.xml“
- Open Android editor via double-click
- <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_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:layout_marginLeft="24dp"
- android:layout_marginTop="31dp"
- android:ems="10"
- android:inputType="numberDecimal|numberSigned" >
- <requestFocus />
- </EditText>
- <RadioGroup
- android:id="@+id/radioGroup1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/editText1"
- android:layout_below="@+id/editText1"
- android:layout_marginTop="28dp" >
- <RadioButton
- android:id="@+id/radio0"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:checked="true"
- android:text="RadioButton" />
- <RadioButton
- android:id="@+id/radio1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="RadioButton" />
- </RadioGroup>
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/radioGroup1"
- android:layout_centerVertical="true"
- android:text="Button" />
- </RelativeLayout>
- Navigate to “res/layout/” and open the Graphical Layout of your “activity_main.xml“
- right-click on the first Radio Button and open “Edit Text”
- Assign the miles property to the second Radio Button
- Set the “Checked” property for the first Radio Button (Other Properties -> inherited from compoundbutton -> checked -> true)
- Set the “Input type” property for the Text Field to “numberSigned” and “numberDecimal“
- Assign “calc” to the Button and set “calculate” for the “onClick” property (Other Properties -> inherited from view -> onClick)
- Set Background-Color (Right-click on an empty space on your Application -> Edit Background)
- Switch to “src/com.example.tutorialapplication/” and open “MainActivity.java“
- package com.example.tutorialapplication;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.RadioButton;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- // public var
- private EditText text;
- // default func
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // findViewById = Finds a view that was identified by the id attribute
- // from the XML that was processed in onCreate(Bundle).
- // (EditText) = typecast
- text = (EditText) findViewById(R.id.editText1);
- }
- // default func
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- /*
- * Will be executed by clicking on the calculate button because we assigned
- * "calculate" to the "onClick" Property!
- */
- public void calculate(View view) {
- RadioButton mileButton = (RadioButton) findViewById(R.id.radio0);
- RadioButton kmhButton = (RadioButton) findViewById(R.id.radio1);
- // if the text field is empty show the message "enter a valid number"
- if (text.getText().length() == 0) {
- // Toast = focused floating view that will be shown over the main
- // application
- Toast.makeText(this, "enter a valid number", Toast.LENGTH_LONG)
- .show();
- } else {
- //parse input Value from Text Field
- double inputValue = Double.parseDouble(text.getText().toString());
- // convert to...
- if (mileButton.isChecked()) {
- text.setText(String.valueOf(convertToMiles(inputValue)));
- // uncheck "to miles" Button
- mileButton.setChecked(false);
- // check "to km/h" Button
- kmhButton.setChecked(true);
- } else { /* if kmhButton isChecked() */
- text.setText(String.valueOf(convertToKmh(inputValue)));
- // uncheck "to km/h" Button
- kmhButton.setChecked(false);
- // check "to miles" Button
- mileButton.setChecked(true);
- }
- }
- }
- private double convertToMiles(double inputValue) {
- // convert km/h to miles
- return (inputValue * 1.609344);
- }
- private double convertToKmh(double inputValue) {
- // convert miles to km/h
- return (inputValue * 0.621372);
- }
- }
Android Developer Reference
Style | Android Developers
Patterns | Android Developers
Building Blocks| Android Developers