Installation

Install the Eclipse ADT Bundle. After unpacking, try to start the included Eclipse. You might need to install a current Java JDK. Also start the included SDK Manager and install / update packages you might need. Be prepared to wait some time here ... Building Your First App.

In the SDK Manager, Create an AVD And try to start it. You might face the problem that the start takes ages and the AVD (which simulates an Android device) is very slow. One workarround for windows computers with an Intel CPU is to install in the Android SDK Manager the "Intel x86 Emulator Accelerator (HAXM installer) and emulate an Intel Android device instead of the normal ARM one. After the installation of the HAXM installer you need to run this installer manualy once otherweise you will get this error while trying to start an Intel device:

emulator: ERROR: x86 emulation currently requires hardware acceleration!
Please ensure Intel HAXM is properly installed and usable.
CPU acceleration status: HAX kernel module is not installed!
This installer may again fail with this error message:

This computer meets the requirements for HAXM, but Intel Execute Disable Bit (XD) is not turned on. HAXM can be installed, but will not work until  XD is enabled.
Please refer to the Intel HAXM documentation for more information.

In this case try to run this (as Administrator!)

bcdedit /set nx AlwaysOn

Reboot, start the HAXM installer again. If it fails with a memory related error, assign the AVD less memory than you allowed during the HAXM installation.

However, this is still slower and less realistic than a real device, so it might be prefereable to let Eclipse run your app on a real device. Just enable USB debugging on a device and connect it via an USB cable

Strings

In the file res/values/strings.xml you will find Strings which you can reference for example in the activity_main.xml. This is an exmaple for such a file

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">ThorstensFirstAndroidApp</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="edit_message">Type your value here</string>
</resources>

You can provide different languages just by creating values folders for each language, e.g. values-es, values-de, values-fr, ...

Layout

A ViewGroup holds one or more ViewGroup or one or more View. An examples for a ViewGroup are the LinearLayout or the RelativeLayout

In your project in the res/layout folder there is a file called activity_main.xml which holds the layout. If a View or a ViewGroup element should be inside another, you write it inside its XML tag. You need to make sure that all Strings on OnClick methods exist in the strings.xml / the MainActivity.java as a method. Example for an Layout:

<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="com.example.thorstensfirstandroidapp.MainActivity" >

    <TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/hello_world" />

    <EditText
       android:id="@+id/edit_message"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignLeft="@+id/textView1"
       android:layout_below="@+id/textView1"
       android:layout_marginLeft="13dp"
       android:layout_marginTop="29dp"
       android:ems="10"
       android:hint="@string/edit_message" />

    <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignRight="@+id/edit_message"
       android:layout_below="@+id/edit_message"
       android:layout_marginTop="23dp"
       android:onClick="heyYouHitTheButton"
       android:text="@string/button_send" />

</RelativeLayout>

Your layout is scaled automatically for larger or smaller screens and it rotates also automatically if the screen is rotated. You may however also choose to to provide a complete different layout for larger screens or for screens currently in landscape rotation. Just create next to the layout folder one called for example

layout-large-land/

For a larger device in landscape mode. If you need finer control check Designing for Multiple Screens

Fragment

You may also create parts of the layout via program code. See Building a Dynamic UI with Fragments

Activity

You start with the MainActivity. This is where your program starts and where buttons with onClick are searching for methods to execute. Our button expects for example a method called heyYouHitTheButton. This might be one:

public final static String MY_EXTRA_MESSAGE = "de.tgunkel.thorstensfirstandroidapp.MESSAGE";

public void heyYouHitTheButton(View view)
{
 // extract the value of the message the User entered before hitting the button
 EditText editText = (EditText) findViewById(R.id.edit_message);
 String message = editText.getText().toString();

 Intent intent = new Intent(this, MyFirstAndroidActivity.class);
 intent.putExtra(MY_EXTRA_MESSAGE, message);
 startActivity(intent);
}
 

Our method creates and Intent (see below) which starts a new Activity and passes some extra information to it.

Create a new Activity: In Eclipse New, Others, Android, New Activity The Hierarchical Parent is either the MainActivity or one of your other Activities.

In our Activity we just display the text we got via the Intent which started us and display it

public class MyFirstAndroidActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_first_android);

        // get the message we did send us from the MainActivity class
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.MY_EXTRA_MESSAGE);

        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        // Set the text view as the activity layout
        setContentView(textView);
    }
...

For further details about the different methods in an Activity and its lifecycle.

Intent

Each Activity is started by an Intent. You may explicitly start a specific Activity or you may ask the system to start any Activity which provides some service, e.g. make a phone call, take a picture or show a map. You (as the developer) do not care which app it will be. Example to show a map: intent sending

Uri location = Uri.parse("geo:0,0?q=+Deutscher+Bundestag,+Berlin");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);

// Verify it resolves
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(mapIntent, 0);
boolean isIntentSafe = activities.size() > 0;

// Start an activity if it's safe
if (isIntentSafe) {
    startActivity(mapIntent);
}

Other example, call a number

Uri number = Uri.parse("tel:123456789");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);

Uri webpage = Uri.parse("http://www.tgunkel.de");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);

Get a result from another Activity

Sensors

Get all hardware sensors (code works within an Activity)

SensorManager sensorManager;
sensorManager=(SensorManager) getSystemService(Context.SENSOR_SERVICE);
List<Sensor> list = sensorManager.getSensorList(Sensor.TYPE_ALL);

Periodic read of the light Sensor

public class SensorActivity extends Activity implements SensorEventListener
{

    private SensorManager mSensorManager;
    private Sensor mLightSensor;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.mSensorManager=(SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        //mSensorManager.registerListener(this, mLightSensor, SensorManager.SENSOR_DELAY_NORMAL);
        mSensorManager.registerListener(this, mLightSensor, 5000000);
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        mSensorManager.unregisterListener(this);
    }

    @Override
    public void onSensorChanged(SensorEvent event)
    {
        float lux = event.values[0];
        TextView test = (TextView) findViewById(R.id.textTemperatur);
        test.setText(Float.toString(lux));      
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy)
    {
    }

Saving data

Storage

Camera

Capturing Photos

Network