Wednesday, November 10, 2010

How to customize control

There are some icons and xmls under C:\Android\android-sdk-windows-1.6_r1\platforms\android-1.5(or other version)\data\res\drawable

To use different icons for standard control, we can copy the xml file and icons used in the xml to our own \drawable folder and set it in the layout xml

1) Grab the checkbox_background.xml and checkbox.xml from above folder and copy to the drawable folder in my project

2) Copy all the PNG files for the checkbox to the drawable folder in my project

image

3) Change the layout to use the checkbox (In my case, it is a CheckedTextView)

android:checkMark="@drawable/checkbox"

image

Monday, October 25, 2010

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap

Recently our two applications on Android Market kept reporting error “java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap…”. If you look at the stack trace, there is no trace of our code. Below is one of the stack trace:

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@4478a900
at android.graphics.Canvas.throwIfRecycled(Canvas.java:955)
at android.graphics.Canvas.drawBitmap(Canvas.java:1044)
at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:323)
at android.widget.ImageView.onDraw(ImageView.java:842)
at android.view.View.draw(View.java:6740)
at android.view.ViewRoot.draw(ViewRoot.java:1407)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)

 

After searching around the Internet, I found some clues. It has something to do with the call of bitmap.recycle(). Some people suggest to remove all the calls to bitmap.recycle() and let the garbage collector to do the job. So I tried this solution and updated the Packing List application. I will give it a day or two to see if it settles the issue.

Thursday, August 19, 2010

Miscellaneous Tips

How to stretch a control?

Set Layout_weight to greater than 0

image

image

 

Issue with Check box in a list view

When I use anything other than textview in a listview, the context menu stop working. Now to have a check box in a list view, I have to use CheckedTextView. But the text in CheckedTextView appears on the left side of the checkbox. I don’t know how to change it, so I use a CheckedTextView with no text and a TextView.

 

Show checkbox in CheckedTextView

<CheckedTextView android:id="@+id/categoryCheckedTextView"
android:layout_width="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:layout_height="wrap_content"
android:clickable="true">
</CheckedTextView>

Display simple MessageBox in Android


AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Packing List");
alertDialog.setMessage("Could not find the file.");
alertDialog.show();

 

Change emulator orientation

Use KEYPAD_7 or KEYPAD_9 on you desktop. You need to turn off your Num Lock.


Dynamically change the background color

To dynamically change the background color, use myView.setBackgroundResource(R.color.my_color) instead myView.setBackgroundColor(R.color.my_color)

 

How to align text in text view?

When trying to align the text to the right in text , set the “layout_gravity” to right doesn’t work. Need to set “gravity” to right.

Monday, August 9, 2010

Re-import Android project issue

After my laptop crashes, I have to re-install everything. After importing the existing projects, I got the following error “The method onClick(DialogInterface, int) of type new DialogInterface.OnClickListener(){} must override superclass method”

image

And this is what I found on google:

Eclipse is defaulting to Java 1.5 and you have classes implementing interface methods (which in Java 1.6 can be annotated with @Override, but in Java 1.5 can only be applied to methods overriding a superclass method).

Go to your project/ide preferences and set the java compiler level to 1.6 and also make sure you select JRE 1.6 to execute your program from eclipse.

image

How to debug on device?

For detail information, go to - http://developer.android.com/guide/developing/device.html. The following is a simplified version for my own reference.

1. Declare your application as "debuggable" in your Android Manifest.

In Eclipse, you can do this from the Application tab when viewing the Manifest (on the right side, set Debuggable to true). Otherwise, in the AndroidManifest.xml file, add android:debuggable="true" to the <application> element.

2. Turn on "USB Debugging" on your device.

On the device, go to the home screen, press MENU, select Applications > Development, then enable USB debugging.

3. Setup your system to detect your device.

If you're developing on Windows, you need to install a USB driver for adb. See the Windows USB Driver documentation.

image

ClassNotFound exception when unmarshalling

The following code is the serialization part of the Category class in PackingList project. Since the category has an arraylist of items, I have to put it in Bundle. To de-serialize it, I need to retrieve items from the Bundle. To do that, I was using Bundle d = in.readBundle(). However, I received “ClassNotFound exception when unmarshalling”. I have to pass in the ClassLoader so it knows it’s for the Item.

 

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(category);
Bundle b = new Bundle();
b.putParcelableArrayList("items", items);
dest.writeBundle(b);

}

public static final Parcelable.Creator<Category> CREATOR =
new Parcelable.Creator<Category>() {
public Category createFromParcel(Parcel in) {
Category category = new Category();
category.category = in.readString();
Bundle b = in.readBundle(Item.class.getClassLoader());
category.items = b.getParcelableArrayList("items");

return category;
}

@Override
public Category[] newArray(int size) {
return new Category[size];
}
};

How to display progress dialog?

private ProgressDialog dialog;
public void onCreate(Bundle savedInstanceState) {
……

dialog = ProgressDialog.show(this, "",
"Loading Calendar and Tasks. Please wait...", true);

……
CalendarThread calendarThread = new CalendarThread();
calendarThread.start();

}

private class CalendarThread extends Thread {

@Override
public void run() {
//Long running process
……

handler.sendEmptyMessage(0);
}

private Handler handler = new Handler() {

@Override
public void handleMessage(Message msg) {
//Process after the long running process
……
dialog.dismiss();
}
};
}