Android Counter
Aug31
I’ve been learning Android for various reasons. Though I’m primarily a Flash guy, I felt that I needed to learn Java. What better way to learn then to use it for Android apps. I’m currently not a fan of AIR for Android to build apps, we’ll see when it comes out. My current though is use AIR to get flash apps on the phone that are already built, but if it’s new, code native for Android. Games are another story.
Alright, let’s move on. I made a counter app when I was at FITC SanFran. I was Stage Manager and one of my duties was to count the number of people in room. This was hard to do on the first day cause people kept asking me question and I had to start over. That made me think of this counter.
Step 1: Setup your environment: http://developer.android.com/sdk/index.html
Step 2: Setup your view (res/layout/main.xml)
< ?xml version="1.0" encoding="utf-8"?> <linearlayout android:id="@+id/mainlayout" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <textview android:gravity="center_vertical|center_horizontal" android:text="0" android:id="@+id/count" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="200sp"/> </linearlayout>
Step 3: Write your code (src com.joshspoon.Main)
package com.swfitgood;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.TextView;
public class Main extends Activity implements OnClickListener, OnLongClickListener {
/** Called when the activity is first created. */
private int numCount = 0;
private TextView tView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//link object to UI of TextView
tView = (TextView) findViewById(R.id.count);
//set up listeners for short and long press. The Main Activity is registered as the listener for the TextView
tView.setOnClickListener(this);
tView.setOnLongClickListener(this);
}
//click tied to the listener
@Override
public void onClick(View v) {
numCount += 1;
tView.setText(String.valueOf(numCount));
}
//long click tied to listener
@Override
public boolean onLongClick(View arg0) {
numCount = 0;
tView.setText(String.valueOf(numCount));
return true;
}
}Step 3: AndroidManifest.xml: has all the instructions for intents, startups and permissions
< ?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.swfitgood"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
<activity android:name=".Main"
android:label="@string/app_name">
<intent -filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent>
</activity>
</application>
<uses -sdk android:minSdkVersion="8" />
</manifest>Pretty Easy Hu?