The tutorial of “How to Develop Android Application” is based on API Level 17 and Android 4.2 (Jelly Bean).
Develop Android Application:
- What do you Need:
- Knowledge of Basic XML
- Knowledge of Basic Java
- Knowledge of Basic Eclipse
- Prerequisites:
- First you need the Android SDK and IDE. (Android offers a special bundle for that: Android SDK Bundle)
- Download the Android SDK Bundle then unzip it and run as “SDK Manager.exe”.
- Start the Eclipse
- Create a Android Virtual Machine (dalvik):
On your computer you can create and run a virtual android machine to run, test and debug your application. Later to this virtual machine you can deploy your application.
- At the navigation toolbar click on “Windows”.
- Open “Android Virtual Device Manager”.
Create a “New” Virtual Device:
Be sure that “Use Host GPU” is enabled. To use the Host GPU this allows the AVD and this helps you to render the AVD as much faster.
After that you can start the AVD:
- Create a New Project:
- First open a “File”.
- Then select “New”
- New Android Application Project window is open.
For your project chose a new and click to the next:
Now Configure a Project and click to the next:
Now Configure Launcher Icon window is open:
Here, you can choose a launcher Icon that will be displayed on your mobile phone and click to the next.
Create a New Activity window is open and click to the next.
New window is open to configure your Activity and then click to the Finish.
Eclipse looks similar to that after finishing it.
- Implement the Look and Feel:
To “/res/layout/” navigate in the package explorer and open “activity_main.xml”
Right click on the “Hello World” and then delete it.
5.1 Create an static Attributes:
Select “/res/values/strings.xml”
“Add” a new entry
Select the color entry and then press ok and after that set the following attributes:
Add a few more String (!) attributes:
Name/value:”miles” /”to Miles”
Name/value:”kmh” /”to km/h”
Name/value:”calc” / “Calculate”
Switch from “Resource” to “string.xml” and make sure that your code is look similar to that snippet:
view plaincopy to clipboardprint?
<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>
5.2 Add Views
Select “/res/layout/activity_main.xml”
Via double-click, open Android editor.
There are two possibilities. Via drag & drop you can create a new view or edit the XML source code. In this tutorial via drag & drop we can add the views.
Now, let’s start building our app. At first for the output, we have to add a “Text Field”.
To your application drag this field.
Select the section of “Form Widget” and to your App drag a Radio Group and make sure that the Radio Group has two Radio Buttons. Finally you can add a normal button.
Switch from “Graphical Layout” to “activity_main.xml” and make sure that your code looks similar to that:
view plaincopy to clipboardprint?
<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>
5.3 Edit the view properties
Via right-click on the view or via XML, you can edit the properties of Views.
Navigate to “res/layout/” and open the Graphical Layout of your “activity_main.xml”
Right-click on the first Radio Button and open the “Edit Text”
To the second Radio Button, assign the miles property
For the first Radio Button set the “Checked” property (Other Properties -> inherited from compound button -> checked -> true)
For the Text Field to “number Signed” and number Decimal” set the “Input type” property.
To the Button assign “calc” and for the “on Click” property set “calculate” (Other Properties -> inherited from view -> on Click)
Set Background-Color (Right-click on an empty space on your Application -> Edit Background)
After that change the Background should be #eeeeee! To see the difference it can be difficult.
- Implement the Logic
After we implement the Frontend-View we have to implement the logical part with Java!
Switch to “src/com.example.tutorialapplication/” and open “MainActivity.java”
view plaincopy to clipboardprint?
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);
}
}
That’s was the short overview of “How to Develop Android Application”!!!!