Wednesday, October 30, 2013

A Mission to Android - The Android Project

Over the past days I have started my mission to android. The goal creating a functional clock home screen widget. Last week I posted about my design and now that I have a plan it is time to get the idea realized in code. So the first thing I had to find out is how to create a widget in android.

I went through all sorts of different sources on the internet. Among others I read the guides on android developer, went through diverse tutorials and caught up on stuff that has happened since Ginger Bread , - my last try at developing for android-. As hours turned in to days I realized that I have lots of work ahead of me and so I decided to break up the steps and present them in steps that are hopefully easy to digest.

Overview

In this post I will go over how I setup the android project using Android Studio. I decided to use Android Studio even though it is still in an early release phase but since it is the future why not go for it. It seems to be stable enough for my purposes. So I downloaded the application and installed it on my development machines. This also downloads and installs a copy of the Android SDK. Once that was done I was ready to fire up Android Studio and create my project.

Creating an Android Project

Before I can actually create a widget I have to create a android project of which the widget is part of. The easiest way to get started is by simply using the "New Project" feature in Android Studio.

Now I have to choose the name for my project and the included module as well as the base package for the java code. I named my project and module E34Clock. I also have to choose the target and minimal Android SDK. For my project I decided to go with Ice Cream Sandwich as the minimal supported sdk. This corresponds to sdk level 14. The target of my application is level 18 which corresponds to Jelly Bean 4.3.3. Be careful which versions you choose because depending on this your app will have to run on different android versions and you might have to support multiple API calls due to version changes. This also impacts for which devices your app will be displayed in the app store later on.

Since I want to create widget I created a project that contained a single blank activity which I will later use for customizing of the widget.

In the last step I have to name the activity and the corresponding layouts.

Pressing "Finish" will create the typical android project structure and setup a gradle build system to build your app without needing an IDE.

The src/main/java/ folder holds all relevant classes for your app. The src/main/res/ folder holds all assets, images, xml files your application needs. We will get back to this when we start creating the application.

The most important file is the AndroidManifest.xml file. This file describes the join points of your app with the Android OS and the user.
Among other things you define your activities, services your receivers as well as the permissions your application needs. This file also tells Android which SDK version you support and which version your code is. I will come back to this file many times during the course of this journey.

<!--
  ~ (c) Copyright 2013 Stefan Langer
  ~
  ~    Licensed under the Apache License, Version 2.0 (the "License");
  ~    you may not use this file except in compliance with the License.
  ~    You may obtain a copy of the License at
  ~
  ~        http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~    Unless required by applicable law or agreed to in writing, software
  ~    distributed under the License is distributed on an "AS IS" BASIS,
  ~    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~    See the License for the specific language governing permissions and
  ~    limitations under the License.
  -->
<manifest android:versioncode="1" 
  android:versionname="1.0" 
  package="de.element34.e34clock" 
  xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-sdk android:minsdkversion="14" 
      android:targetsdkversion="18"/>

    <application android:allowbackup="true" 
      android:icon="@drawable/ic_launcher" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme">
        <activity android:label="E34ClockSettings" 
          android:name=".ClockSettingsActivity">
          <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER"/>
          </intent-filter>
        </activity>
    </application>
</manifest>

Since I created the project with a blank activity this activity is defined here.

    <application android:allowbackup="true" 
      android:icon="@drawable/ic_launcher" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme">
        <activity android:label="E34ClockSettings" 
          android:name=".ClockSettingsActivity">
          <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER"/>
          </intent-filter>
        </activity>
    </application>

The activity consists of the name of the activity class and the label this activity is known by. The "." in front of the activity name tells Android to use the package named in the package attribute of the Manifest tag. Additionally we define an intent filter which defines on which intent we will react. For now we simply specify that the activity supports the MAIN action in the LAUNCHER category. This tells Android to include this activity in the app screen. When the displayed launcher icon is pressed it opens the activity. I will explain intents and intent-filters in detail when we comeback to them during coding of the widget.

Running this application by pressing the launcher icon gives you a simple blank activity which can be started but doesn't do anything.

Lookout

In the next post I will start creating the code for the widget. Hope to see you then.

Yours truely

Stefan Langer

Creative Commons License

No comments:

Post a Comment