Tuesday, March 2, 2010

Corrupt Android Camera Preview

I have been playing code to use the Camera and came across an issue which I couldn’t find a resolution for. When you run any of the various samples around the Internet they all appear to suffer from the same issue, the Camera preview is corrupt in Portrait mode. I found a fudge which switches the mode to Landscape before showing the Camera Activity but this was less than ideal because you get a short pause as the phone switches display modes. I eventually came up with the solution below in the surfaceChanged method:
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

if (previewRunning) {
camera.stopPreview();
}

Camera.Parameters p = camera.getParameters();

////////////////////////////////////////////////////////////
// Check orientation and set size as w, h or h, w.
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

if(display.getOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
p.setPreviewSize(h, w);
} else {
p.setPreviewSize(w, h);
}
camera.setParameters(p);

try {
camera.setPreviewDisplay(holder);
} catch(IOException e) {
e.printStackTrace();
}

camera.startPreview();
previewRunning = true;
}

The key to this solution is the order of height and width in setPreviewSize, which should be h, w when in Landscape or w,h when in Portrait.

Monday, February 1, 2010

Android Numeric Soft Keyboard

I recently spent some time trying to figure out how get the Soft Keyboard to default to numeric input when I only required numbers entering. It’s actually quite simple but took awhile to figure out. All you have to do is add the ‘android:inputType’ to the EditText resource element. You can then add numberDecimal for numbers with decimal places or number if no decimal places are required.


<EditText
android:text=""
android:id="@+id/editReading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal">
</EditText>

Android Screenshots

One of the annoying things about Android is trying to get a screenshot from the device. With the current versions of Android there are two options, root your device and download an app or connect your device to a PC with the SDK installed. I will attempt to describe how to do the later on the Windows Platform.

Firstly you need to install the Java JDK from Sun. I am running 64 bit Windows but opted for 32 bit Java as I have run into difficulties in the past using the 64 bit version. Just install Java into the default locations. You then need to add the path of java.exe to the System environment variables. When you look in Programs Files (or Program Files (x86)) you will find that the JDK installed also installed the JRE (Java Runtime Environment), I would recommend adding the JDK location to your path and not the JRE. In my case this is:

C:\Program Files (x86)\Java\jdk1.6.0_17\bin

I also added a new environment variable JAVA_HOME. I’m not sure if this is strictly necessary but since I run a development system I have it set. Setting it if it isn’t necessary will do no harm. If you find you don’t need it please let me know. JAVA_HOME should be set to the JDK directory, in my case:

C:\Program Files (x86)\Java\jdk1.6.0_17

Once Java is configured you need to download the Android SDK and install it. Installing is simply a case of unzipping to a location of choice. You then need to create another new environment variable ANDROID_SWT which points to the path containing swt.jar. The SDK installed two versions of swt.jar, one for 32 bit Java and one for 64 bit. Note this does not depend on the version of Windows but the version of Java, so in my case I set it to:

C:\Programs\android-sdk-windows\tools\lib\x86

The next step is to install the USB device driver for Android. To do this you need to use the SDK to download the driver. You need to run ‘SDK Setup.exe’ from the root of the Android SDK directory, in my case:

C:\Programs\android-sdk-windows\SDK Setup.exe

This application will attempt to connect to the internet to download SDK packages and the USB driver. You can select Accept All and then Install Accepted. I would suggest that you follow the driver installations details from the Android Developer site.

Next on your phone you need to enable debugging. On my Android 1.5 HTC Hero, I selected Settings, Applications, Development, USB debugging. Once this is done connect your phone to the PC.

Finally we can have a go at taking a screenshot. To do this you need to run ddms.bat from the tools directory of the SDK, in my case this is:

C:\Programs\android-sdk-windows\tools\ddms.bat

This will execute the Dalvik Debug Monitor. In this application you should be able to see your device in the upper left hand windows. Select the device and then use the Device Menu to select Screen Capture...

Thursday, November 5, 2009

DatePicker Widget

I've finally started working on my first app. I needed to use a DatePicker but didn't want to use a dialog, I wanted the DatePicker to appear on the current page. Here is my code:


/**
* DatePickerActivity
*
*
*/
public class DatePickerActivity extends Activity {

private DatePicker datePicker;

private int year;
private int month;
private int day;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

////////////////////////////////////////////////////////////
// Get access to Date Picker
datePicker = (DatePicker)findViewById(R.id.DatePicker01);

Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);

datePicker.init(year, month, day, dateSetListener);
}


/**
* DatePicker.OnDateChangedListener
*
*/
private DatePicker.OnDateChangedListener dateSetListener
= new DatePicker.OnDateChangedListener() {

/*
* onDateChanged
*/
public void onDateChanged(DatePicker view, int newYear,
int newMonth, int newDay) {

String dateOutput = String.format("Date Selected: %02d/%02d/%04d",
newDay, newMonth, newYear);
Log.i("Debug", dateOutput);
}
};
}

Friday, October 23, 2009

Hero Kernel Source

HTC have made the Hero Kernel Source code available. Not sure how much use it will be to you and me but its available here, http://developer.htc.com

Monday, October 19, 2009

Another Eclipse/Android Issue

I ran across another small issue today. I tried import an existing Android Application into Eclipse and received the following error:

[2009-10-19 07:56:02 -]Android requires .class compatibility set to 5.0. Please fix project properties.

The solution is quite simple, select the following after right clicking on the project in the Package Explorer:

Android Tools >> Fix Project Properties

But importantly, you must restart Eclipse afterwards for the error to be resolved.

Hero Crash

My Hero crashed, I came to make a call and the phone would come out of standby. I first thought that I must have accidentally switched it off but it wouldn’t power on. So then I thought the battery must be flat so I plugged in the charger but the phone wouldn’t even charge. Starting to worry that the phone was dead and would have to be sent back, I resorted to removing the battery. Fortunately this resolved the problem but left me a little worried that this could happen again.