Tuesday, April 27, 2010

Comparing The Old & New Eclipse Theme .prefs Files

by Guest Contributor Bill Mote

If you're wondering what's inside the 2 prefs files we replaced in the Eclipse themes post; here is a "compare" (or diff) using Eclipse's own built-in compare function.  The files are human readable.  The original unedited file is on the left in each image.  Click each image to enlarge:

org.eclipse.jdt.ui.prefs

org.eclipse.ui.editors.prefs


Monday, April 26, 2010

Applying Themes to Eclipse

by Guest Contributor Bill Mote

--> Skip to the How-To

I'm new to event driven development.  I stopped coding back when VB was in it's infancy.  I did pickup classic ASP and do enough scripting in interpreted languages to kindle the desire, but it was not until the release of Android that I genuinely got on fire about coding again.

What did I find?  A lot of barriers.  You wouldn't think that's true given the open and free nature of Android.  Greg, aka Android Code Monkey, has put together an idiot proof tutorial on setting up the development environment.  That was my first hurdle.  Check.  I moved on to his Hello Android tutorial.  Check.  Next I tried the Android Developer's NotepadCodeLab tutorial.  Easy enough I thought.  This time you just "open the AndroidCodeLab folder" as instructed and you're moving right along.

Open it how?  I tried various imports.  I tried opening the source and various .xml files.  All I did was build my frustration, but I'm not giving up.  Instead I purchased "Head First Java."  The book is awesome.  It focuses on teaching Java and avoiding the IDE hurdles but I decided to code the examples in Notepad++ and Eclipse so I could be 1 step ahead when I finished.  The leap from C++ to Java isn't that great and things seemed to make sense so I'm moving right along.

One thing I noticed about the Eclipse environment is how absolutely blinding it is!  During the day it's tough enough, but at night, when I get most of my keyboard cycles for development, the open IDE is exhausting on my eyes.  Another hurdle ...

Cut to the chase already ...

When Greg asked me to be a guest contributor I couldn't wait to get this information posted.  I want to remove as many barriers as I can for new developers like myself!

My first searches for Eclipse and Themes didn't produce much and the information that was available was, or at least seemed, convoluted and complicated.  Turns out changing the themes in Eclipse is very, very easy!

There are 2 files used to display the themes:

Colors are managed in this file: org.eclipse.jdt.ui.prefs
Text Editors are managed in this file: org.eclipse.ui.editors.prefs

Those files can be found below your Eclipse workspace folder which can be found here:

Windows -- .metadata\.plugins\org.eclipse.core.runtime\.settings
Linux/MAC -- .metadata/.plugins/org.eclipse.core.runtime/.settings/

I found an archive file with 6 different themes including the "default" to easily undo any of your changes.  Here's what they look like:

Inkpot

Sula


Vibrant Ink


Wombat


Zenburn

The archive contains the 6 images and 6 folders; 1 for each theme.  In each folder you'll find the 2 aforementioned files.  Simply extract the 2 files from your desired theme into the appropriate Eclipse folder as described above and launch Eclipse!

Monday, March 15, 2010

Getting Started with Android Development

You want to be begin Android Application Development. But how do you get started? The first thing you need is to download and install the development tools. The good news is they are free and fairly straightforward to setup. The bad news is there are so many options it can become confusing which tools are required, optional or compatible with your desired setup. Following the setup I am using and will work for you if you using a Windows system.

Step 1. Install the following tools
  • Eclipse IDE for Java Developers (v3.5 Galileo)
  • Java Platform (JDK 6 Update 18)
  • Android SDK Tools, Revision 4
You can follow my previous post for step by step directions (with screen captures) here.

Step 2. Create your first application. Try my Hello World tutorial here.

Feel free to post questions or comments.

Monday, March 8, 2010

How to base64 encode decode Android

The official documentation states that base64 encode and decode in Android is in the Android.util package. But when you go to use it you will find it's not there!

After much searching, I confirmation Android left base64 encode and decode out of the Android.util package. So what to do? Don't worry, Robert W. Harder has but together a very fast Base64 encoding and decoding class you can include in your project. Here is how to add base64 encode and decode to your project.

Step 1. Visit Robert's website http://iharder.sourceforge.net/current/index.html and download the class file.

 
Step 2. The zip file you downloaded in step 1 includes the base64 class and a folder with examples and documentation.
Step 3. Copy the Base64.java class file into your project. Copy into your appropriate "src" folder.
Step 4. Refresh your project so the new file is included.
Step 5.When the Base64.java file is included in your project, you will get the following error "The declared package does not match the expected package". Fix this by opening the class file and adding a package declaration that matches your project.
Step 6. Done. You're ready to base64 encode and decode.
 

Download the complete tutorial here

Saturday, March 6, 2010

Vidoes - My First Android Application in the Market

My first Android application is now in the Market. Why did I create this application? Frankly, because it's missing from Android OS. There is a music application for listening to your music. But, to view your videos you must launch the gallery and view your videos mixed in with your pictures. Also, you don't get the video title or video length.

The "Videos" application fills this void with a simple scrolling list of just the videos on your phone. The features of the application:
  • Thumbnail of your videos
  • Video name
  • Video length
  • Time left (if you are in the middle of watching a video)
  • Launch the video

If you have an Android 2.0+ phone you can find it in the market by searching "Videos" or get more information here.


Videos screen capture

Videos screen capture

Saturday, February 6, 2010

Tutorial: Listview

Android "To Do" Application Tutorial

This is Part 1 in a series of tutorials. Each will build on the previous covering concepts which should be transferable to many applications. In order to cover many typical coding requirements, we are going to create a simple "To Do" application. Each tutorial part will conclude with a functioning application, albeit incomplete. In these series of tutorials we will cover adding, editing, and deleting items in a ListView. Here is what this series of tutorials will cover:
  • Start a new Android application.
  • Add, edit, and delete items in a ListView.
  • Edit "To Do" form.
  • We will start with a simple "one line" ListView. Then we will upgrade it so we can customize and dress up each row.
  • We will add some graphics to the ListView.
  • To improve performance and encapsulation we will move To Do items into a separate class that can  cache the TextView and ImageView widgets.
  • Create a landscape layout.
  • Create a database and table to store our tasks.
 Part 1: Create a new project



1. Create new project

     
2. Select "Android Application". Select "Next"

3. Set new project properties the click "Finish"
  • Project Name: To Do Tutorial
  • Build Target: Select Android 2.1 (you may select any build target, but this tutorial is 2.1)
  • Application Name: To Do Tutorial
  • Package Name: org.androidcodemonkey.todo
  • Create Activity: DetailForm
  • Min SDK: 7 (leave the default)

4. Open layout "main.xml"

5. Right click on "Hello World, DetailForm!" and select "Remove"
6. Now we are going to modify our layout. You can drag controls onto the layout in "Layout" mode, but I prefer to edit the XML directly. It provides me greater control. I am going to add the following controls in the XML mode (add the code from lines 2-26):
  • TableLayout
    • TableRow
      • TextView
      • EditText
    • TableRow
      • TextView
      • RadioGroup
        • RadioButton
        • RadioButton
        • RadioButton
    • TableRow
      • TextView
      • Button

 
  
   
   
  
  
   
   
     
     
     
   
  
  
   
   
 



  
   
  



7. Change to LayoutView and it should look like this. Feel free to change settings on the display to see how it affects the controls.
 
8. Now we need an "entity" class. To keep our class files organized, let's create package.
  • In the Package Explorer > Right click on "src" > "New" > "Package"

  • Name: "org.androidcodemonkey.entities" Then click "Finish"
9. Let's add the a class for our "Task"
  • In the Package Explorer > Right click on "org.androidcodemonkey.entities" > "New" > "class"
  • Name: TaskBE. Click "Finish"
10. Create the following properties and methods.
  • Create string "item"
  • Create string "priority"
  • Create getItem() 
  • Create setItem()
  • Create getPriority()
  • Create setPriority()
  • Implement "toString()". 
Your TaskBE class should look like this:

package org.androidcodemonkey.entities;

public class TaskBE {
 private String item="";
 private String priority="";
 
 public String getItem() {
  return(item);
 }
 
 public void setItem(String value) {
  this.item = value;
 }
 public String getPriority() {
  return(priority);
 }
 public void setType(String value) {
  this.priority = value;
 }
 public String toString() {
  return(getItem());
 }
}
11. Open DetailForm.java. We are going to add an "onSave" method that will be called when the save button is pressed. When the save button is pressed we are going to retrieve the information from the View.
  • Import reference to entities (line #3)
  • Create a new TaskBE object. (line #20)
  • Create an EditText to retrieve the item entered into the view (line #21)
  • Set the item property of the Task (line #22)
  • Create a RadioGroup to retrieve the priority (line #23)
  • Set the priority property of the task (lines #24-#34)
  • (In the next post we will implement adding the task to an adapter) (line #35)
  • Clear the controls on View (lines #37-#38)
package org.androidcodemonkey.todo;

import org.androidcodemonkey.entities.*;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;

public class DetailForm extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
private View.OnClickListener onSave=new View.OnClickListener() {
 public void onClick(View v) {
  TaskBE newTask =new TaskBE();
  EditText taskItem=(EditText)findViewById(R.id.txtTask);
  newTask.setItem(taskItem.getText().toString());
  RadioGroup priorityGroup=(RadioGroup)findViewById(R.id.priority);
  switch (priorityGroup.getCheckedRadioButtonId()) {
   case R.id.highPriority:
    newTask.setType("high_priority");
    break;
   case R.id.medPriority:
    newTask.setType("med_priority");
    break;
   case R.id.lowPriority:
    newTask.setType("low_priority");
    break;
  }
  //To do - Add task to adapter. To be implemented in next post
  //Clear form
  taskItem.setText("");
  priorityGroup.clearCheck();
  }
 };    

}


12. Now when the form is created we need instantiate a button and set it to the save button (line #18) and register a callback for when the button is pressed (line #19).
package org.androidcodemonkey.todo;

import org.androidcodemonkey.entities.*;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

public class DetailForm extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btnSave = (Button)findViewById(R.id.btnSave);
        btnSave.setOnClickListener(onSave);
        
    }
private View.OnClickListener onSave=new View.OnClickListener() {
 public void onClick(View v) {
  TaskBE newTask =new TaskBE();
  EditText taskItem=(EditText)findViewById(R.id.txtTask);
  newTask.setItem(taskItem.getText().toString());
  RadioGroup priorityGroup=(RadioGroup)findViewById(R.id.priority);
  switch (priorityGroup.getCheckedRadioButtonId()) {
   case R.id.highPriority:
    newTask.setType("high_priority");
    break;
   case R.id.medPriority:
    newTask.setType("med_priority");
    break;
   case R.id.lowPriority:
    newTask.setType("low_priority");
    break;
  }
  //To do - Add task to adapter. To be implemented in next post
  //Clear form
  taskItem.setText("");
  priorityGroup.clearCheck();
  }
 };    

}
13. You can now run the program. When you click "Save" your controls should be cleared. Your screen should look like this


Following is the complete code

Download zip file of complete Eclipse project





DetailFrom.java
package org.androidcodemonkey.todo;

import org.androidcodemonkey.entities.*;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

public class DetailForm extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btnSave = (Button)findViewById(R.id.btnSave);
        btnSave.setOnClickListener(onSave);
        
    }
private View.OnClickListener onSave=new View.OnClickListener() {
 public void onClick(View v) {
  TaskBE newTask =new TaskBE();
  EditText taskItem=(EditText)findViewById(R.id.txtTask);
  newTask.setItem(taskItem.getText().toString());
  RadioGroup priorityGroup=(RadioGroup)findViewById(R.id.priority);
  switch (priorityGroup.getCheckedRadioButtonId()) {
   case R.id.highPriority:
    newTask.setType("high_priority");
    break;
   case R.id.medPriority:
    newTask.setType("med_priority");
    break;
   case R.id.lowPriority:
    newTask.setType("low_priority");
    break;
  }
  //To do - Add task to adapter. To be implemented in next post
  //Clear form
  taskItem.setText("");
  priorityGroup.clearCheck();
  }
 };    

}
TaskBE.java

package org.androidcodemonkey.entities;

public class TaskBE {
 private String item="";
 private String priority="";
 
 public String getItem() {
  return(item);
 }
 
 public void setItem(String value) {
  this.item = value;
 }
 public String getPriority() {
  return(priority);
 }
 public void setType(String value) {
  this.priority = value;
 }
 public String toString() {
  return(getItem());
 }
}

 
  
   
   
  
  
   
   
     
     
     
   
  
  
   
   
 



Tuesday, January 26, 2010

How to take Android Screen Shot

Currently, there are no applications available to take a screen capture for Android. This is a step by step guide to take a screen captures.

Step 1: Install Java Platform (JDK 6 Update 18)
Step 2: Install the Android SDK Tools, Revision 4
Step 3: Configure your Nexus one
  • From your home screen (pictured below) press "Launcher" icon to view all applications.
  • Start "Settings"
  • Select "Applications"
  • Select "Development"
  • Check "USB debugging" if it's not already checked.
 Step 4: Launch the Dalvik Debug Monitor
  • Go to your Android SDK folder > "Tools". On my system I placed the Android SDK here "C:\Program Files\eclipse\android-sdk-windows" so I will navigate to "C:\Program Files\eclipse\android-sdk-windows\tools"
 
  • Double click on "ddms.bat" to launch the Dalvik Debug Monitor.

NOTE: IF IT FAILS TO LOAD and the window just closes immediately, you might have to update your system Environment path for JAVA.
  • If it did not launch go to "Control Panel > System > Advanced system settings"
  • Press "Environment Variables..."
  • Select "Path"
  • Press "Edit..."
  •  Add the path the path to JAVA (;C:\Program Files\Java\jdk1.6.0_17\bin) to the end of the "variable value"
  • Press OK and save your changes.
  • Continue by double clicking on "ddms.bat" to launch the Dalvik Debug Monitor in the "tools" directory. 
  • The application will launch and your phone will be listed.
  • Select "Device > Screen capture..."
  • The capture appears.
  •  Now you can copy and paste the screen capture into a photo application or save a copy.

Saturday, January 23, 2010

How to Setup Android Development Environment

There are many ways to develop for Android and because of all the options it is sometimes a little daunting getting your environment setup. I found myself bouncing from web page to web page, not really sure if I had the right tools and if they were setup correctly. I wanted to document the process that worked for me.

Goal: Step by step instructions to install and configure your Android development environment for Windows.

Environment/tools: Here is the setup I have chosen to use.
  • Windows (I'm using Windows 7, but any flavor of windows will do)
  • Eclipse IDE for Java Developers (v3.5 Galileo)
  • Java Platform (JDK 6 Update 18)
  • Android SDK Tools, Revision 4
 Step 1. Install Eclipse IDE
I chose to use the Eclipse IDE for Java Developers (v3.5 Galileo). You can download the it here
  • This is a zip file and does not require installation. Just copy the eclipse folder into your programs directory
Step 2:  Install Java Platform (JDK 6 Update 18)
  • Select your platform

  • Now run the JDK installation. I did not change any of the default installation options. When complete continue to step 3.


Step 3: Download the Android SDK Tools, Revision 4

  • This does not require installation. Just unzip and copy the files. You can install the package anywhere but remember where you put as you will need to reference it in Eclipse. I happen to install it inside my eclipse folder.

Step 4:  Configure Android SDK


  • Launch "SDK-Setup.exe" located in the Android SDK install folder"
  • If you receive the following "Failed to fetch url https://dl-ssl.google.com/android/repository/repository.xml" we need to change the URL to not use SSL. Close the error dialog.
  • Press "Cancel" on the "Choose Packages to Install" Dialog
  • Select "Settings" in the left pane
  • Check "Force https://... sources to be fetched using http://"
  • Press "Save and Apply"
  • Select "Available Packages" in the left pane
  • Select "Documentation" (at the top of the list)
  • Select "Usb Driver Package, rev 3 (at the bottom of the list)
  • And then select any platform you want to develop. You can select all (be prepared for a long download) or you can select only the platfrom for you phone. The Nexus One uses 2.1 and the Droid for Verizon uses 2.0.1.
  • Check "Accept All" and Press "Install Accepted"

  • When done press "Close"
  • Now we are going to create a virtual device (AVD)
  • Select "Virtual Devices" in the left pane
  • Select "New..."
  •  Create the following new Android Virtual Device
    • Name: Android2.1
    • Target: Android 2.1 API Level 7 (Or which ever API you installed)
    • SD card Size: 4000 MiB to 32000MiB (16000 MiB shown)
    • Leave the rest at the default settings

  • Press "Create AVD". Be patient it may take a few minutes to create your new AVD.
  • Close the SDK setup. 
Step 5:  Configure Eclipse
(These instructions are for Galileo version of Eclipse. Please see this page for instructions if you downloaded the Ganymede version of Eclipse)

  • Launch Eclipse for the first time. You will be asked where you want to your workspace folder to be located. 
    • I used the default location. 
    • Check "Use this as the default and do not ask again" so you don't have to answer this question every time.
    • Press "OK".

  • Select "Help > Install New Software..."
  •  Press "Add..."
  • Fill out the Add Site information
    • Name: Android
    • Location: https://dl-ssl.google.com/android/eclipse/
    • Press "OK"
 If you get an error don't use SSL. change location from "https://dl-ssl.google.com/android/eclipse/" to "http://dl-ssl.google.com/android/eclipse/"
  •  After a connection is made and available tools are retrieved expand "Developer Tools" and select "Android DDMS" and "Android Development Tools"
  • Press "Next"
  • Press "Next" again.
  • Check "I accept the terms of the license agreements"
  • You may receive a Security Warning about unsigned content. Press "OK"
  • Done. Now you should restart Eclipse.
  • After rebooting we need to tell Eclipse where we copied the "Android SDK" in step 3.
  • Select "Windows > Preferences"
  • Select "Android" from the left pane. After you select "Android" you will receive an error. That is because it is not setup yet. We are setting it up now.
  • For the SDK Location, browse to where you installed the SDK.
  • Press "OK"
  • A dialog box will appear. If you want to send usage data to Google leave it checked, other uncheck it and press "Continue". This dialog box can sometimes get trapped beneath your window. Minimize Eclipse if you don't see it.

 You are now ready to build your first application. To create a "Hello World" app check out my post here. If you have any questions feel free to post a comment and I will do my best to answer.