A Simple Android App

I’ve had to clean up a few “first apps” and that led me to wonder- are there any good tutorials out there? This app is probably the most super simple Android app you can make. A button! That does something! But it’s a good starter- it’s bare bones, only what you need to have.

Download full final code here.

This is for SDK version 19 (KitKat). This sample app will have a button, that when clicked, does something. This works, though for any SDK 8 and up.

1. Download the Android SDK from Google.

2. After successfully installed, create a new Android App: File/New/Android Application Project.

3. Give your app a name, such as “ExampleApp”.

4. Set “Theme” to “None.”

5. Click “Next”

project_setting

6. Unclick “Create Activity”

7. Click “Next”

8. Click “Next”

Screen Shot 2014-06-15 at 5.22.34 PM

9. Unclick “Create Activity”

10. Click “Finish”.

last_activity

Why did we uncheck things?
Basically, with version 19, you get a lot of complicated features with a default app- such as fragments and menus- that are best explained after you make a simple app.

Clean Up Project (Remove Unnecessary Files)

Once you click Finish, Eclipse creates an app with relevant directories in your workspace.

1. In Eclipse, open AndroidManifest.xml. In this file, remove the line, under Application, “android:theme=”@style/AppTheme”” Make sure the Application still has an ending bracket, “/>”.

2. In each “values” directory, delete the “styles.xml” files.

3. In lib/ delete the “android-support-v4.jar” file.

4. At this point, go to Project/Build Project. It should build successfully, and you should have no red x’s near any of your directories.

If you do, compare your project to mine up on Github, a bare-bones template for starting an app with no activities or Java classes.

Creating an Activity

1. Right click (or Control-Click for Macs) on the project name in your Package Explorer, and select “New/Class”.

new_class

2. Enter in “MainActivity” (or whatever you want to name your first view). For the Superclass, enter “android.app.Activity” and select “Finished”.

3. In the AndroidManifest.XML file, add an Activity element within the Application. Also, for this Activity, nest an Intent-Filter to launch the activity.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.example.exampleapp"     
    android:versionCode="1"     
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
       >
       <activity android:name="MainActivity">
           <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
       </activity>
    </application>
</manifest>

4. In MainActivity.java, add the following code: the package (your full app name), the libraries we will use, and we’re inheriting and modifying (later on) the onCreate() method.

package com.example.exampleapp;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    }
}

Take a moment to Build Project to make sure you haven’t introduced any errors.

5. Now we’ll create the layout- the first user interface element. Select the layout directory in the Project Navigator, and right-click or Control-Click, select New/Android XML File.

6. Name the file “activity_main.xml”. In “Root Element” leave LinearLayout selected. This will create an xml file in your layout directory named “activity_main.xml”.

Save files, build project, to ensure everything is trouble-free. There should be no red x’s next to files.

7. Next, open up MainActivity.java and in the onCreate method, add the direction to load the xml file:
setContentView(R.layout.activity_main);

    @Override
    protected void onCreate(Bundle savedInstanceState){
        setContentView(R.layout.activity_main);
    }

8. Back in the XML file, let’s set the background to red, so we know it’s working properly: in LinearLayout tag, add “android:background=”#cc0000″”:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout   
        xmlns:android="http://schemas.android.com/apk/res/android"      
        android:layout_width="match_parent"     
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:background="#cc0000" />

Build, and make sure no errors were introduced.

9. Tether your android device to your computer. Select Project/Run, a window will pop up to select what device, select your android device, and see your app pop up with the correct color you set in the activity_main.xml file.

Adding a Button

1. In “activity_main.xml”, within the LinearLayout tags enter in the Android xml code for a button:

<Button
    android:id="@+id/myButton"
    android:layout_height = "wrap_content"
    android:layout_width = "match_parent"
>

2. Save, build, and run. A button should appear.

3. Let’s add a value for the name of the button. To do that, open values/strings.xml and add <string name="button_title">Press Me</string>

Save and build. Check for errors.

4. In activity_main.xml, for the button property, add, android:text="@string/button_title"

Save, build, and run on your device to see the text appear.

Adding Action to a Button

1. Let’s add some action once you click. In activity_main.xml, add to the button property: android:onClick="wasPressed"

2. In MainActivity.java, let’s add a method called “wasPressed()”:

public void wasPressed(View v){
    Log.d("myexampleapp","Button was pressed.");
}

This will log “Button was pressed” to “LogCat” at the bottom of Eclipse, there is a little running Android robot icon, click that and view the Logs. Create a filter by clicking “+” in the left hand panel, and add for the tag, “myexampleap”. That will filter out the many messages to just the ones spawned by your app, and the ones you’ve coded with Log.d (d for debug).

Save and build, open Logcat and select your filter. Run the app. Click the button. You should see the message “Button was pressed,” each time you click it.

Troubleshooting
If tethering your device doesn’t work, go to settings/developer mode and make sure USB debugging is set to on.

If you errors that you can’t clear: go to Project/Clean. Uncheck “build automatically.” Select your project, clean, and then build again. Back step through the steps to and verify each way. Usually it’s XML errors, so keep an eye out.

3 thoughts on “A Simple Android App

  1. Pingback: banane » Blog Archive » Basic Android App

  2. Pingback: Adventures in Mobile Marketing » Incredibly Simple Android App

  3. Pingback: A Simple Android App | - appgong

Comments are closed.