Then in the test app you MUST REFERENCE the project. Thanks to this blog for the excellent details on what to do!
Wednesday, June 6, 2012
Illegal Access Error and Referenced Projects
I've been writing a number of test classes for a very complex Android app. Recently I ran into a problem with a third party jar file misbehaving and throwing a java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation. This is due to a class path problem. The solution is to reference like you normally would the jar in the source app and BE SURE TO EXPORT IT.(last tab in Eclipse properties-->Classpath
Then in the test app you MUST REFERENCE the project. Thanks to this blog for the excellent details on what to do!
http://blog.js-development.com/2010/06/android-instrumentation-test.html
Then in the test app you MUST REFERENCE the project. Thanks to this blog for the excellent details on what to do!
Sunday, June 3, 2012
Deleting files from the SDCard
As part of a recent project, I enabled users to export data from the database to the file system by clicking a button. Users then wanted an easy way to delete the files that they created. This little bit of code below does just that. It looks in the standard Android download directory (where I'm saving the files) for all the files that end in testfile.txt (I'm naming files {_id}_{date}_testfile.txt) and then deletes them one by one..
Button clearJsonFiles = (Button) findViewById(R.id.clearJsonFiles);
clearJsonFiles.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
File path = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File[] files = path.listFiles();
for (int i = 0; i < files.length; i++)
{
try {
Log.d(TAG, "Found File:" + files[i].getCanonicalFile());
if (files[i].getCanonicalPath().contains("testfile.txt")) {
Log.d(TAG, "Deleting File:" + files[i].getCanonicalFile());
//deleteExternalStoragePublicFile(files[i].getCanonicalPath());
files[i].delete();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
Tuesday, May 29, 2012
Android Coding Standards
One of the challenges facing any large development effort is ensuring that all developers follow the same coding standards. Maintaining coding standards, which is "a set or rules or guidelines for formatting code", has numerous benefits including helping lower the cost of code maintenance, enabling new developers to learn the code base faster and overall lower cost of ownership. This link talks about the benefits of coding standards in a very concise and easy to understand way.
http://www.valid-computing.com/benefits-of-coding-standards.html
The Android development team has its own code standards that they use for the Android source. It's published here. http://source.android.com/source/code-style.html I try to follow these standards the best that I can, although individual projects may have their own set of standards that should be followed. The important thing is to have standards and to make sure that everyone uses them.
http://www.valid-computing.com/benefits-of-coding-standards.html
The Android development team has its own code standards that they use for the Android source. It's published here. http://source.android.com/source/code-style.html I try to follow these standards the best that I can, although individual projects may have their own set of standards that should be followed. The important thing is to have standards and to make sure that everyone uses them.
Thursday, May 24, 2012
Getting the Android id from a URI - Content Provider
I've been working quite a bit with Android content providers on a recent project and one thing that keeps coming up is how to determine the android id (_id) of a record that has just been created via a content provider. The insert statement returns the uri pointing to the row that has been created and this URI ends with the key to the object. In other words the URI can tell you the id, all you have to do is parse it.
An easy way of doing that parsing is shown below.
Uri uri=getContentResolver().insert(ProviderHelper.determineURI(myCarObj), myCarObj.createContentValues());
Log
String carId = uri.getPathSegments().get(1);
Log.d(TAG, "Content inserted at: " + uri);
Log.d(TAG, "Car_id: " + carId);
05-24 13:49:41.290: D/CarProviderTest(11228): Content inserted at: content://com.supercar.edc.android.contentproviders.Car/car/123
05-24 13:49:41.290: D/CarProviderTest(11228): Car_id: 123)
123 is the Android Id (_id) for the record I just created.
An easy way of doing that parsing is shown below.
Uri uri=getContentResolver().insert(ProviderHelper.determineURI(myCarObj), myCarObj.createContentValues());
Log
String carId = uri.getPathSegments().get(1);
Log.d(TAG, "Content inserted at: " + uri);
Log.d(TAG, "Car_id: " + carId);
05-24 13:49:41.290: D/CarProviderTest(11228): Content inserted at: content://com.supercar.edc.android.contentproviders.Car/car/123
05-24 13:49:41.290: D/CarProviderTest(11228): Car_id: 123)
123 is the Android Id (_id) for the record I just created.
Friday, May 18, 2012
Setting up a dialog template
A real common task is to pop up a confirm/cancel dialog on button clicks. I've moved this code into a method that I can easily copy and paste into Android Activities and call the dialog with one line of code like the line below. This sample code shows a one button dialog, it's easy to do a two button dialog. It's easy to set up your custom dialog xml layout which is really similar to any other layout.
showOneButtonDialog(getResources().getString(R.string.errorDialogTitle), message);
private void showOneButtonDialog(String titleStr, String message) { AlertDialog.Builder builder = new AlertDialog.Builder(WeeksOfPractice.this); LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.layout_root)); TextView title = (TextView) layout.findViewById(R.id.headerTitle); title.setText(titleStr); TextView text = (TextView) layout.findViewById(R.id.text); text.setText(message); ImageView image = (ImageView) layout.findViewById(R.id.image); image.setImageResource(R.drawable.ic_launcher); builder.setView(layout); AlertDialog errorDialog = builder.create(); errorDialog.setButton(getResources().getString(R.string.dialogContinue), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); errorDialog.show(); }
Monday, May 14, 2012
Displaying PDF files
I recently had a request by a client to be able to display links that would go to hosted PDF files (files that are out in the cloud somewhere and not resident to the device). Now there is no guarantee that a particular device has a PDF viewer, say from Adobe installed. And using that viewer would kick the person out of the app anyway when the intent fires. One easy solution for this is to pass the PDF link to Google documents which would automatically convert the PDF to html and display it.
if (url !=null && url.endsWith(".pdf")) {
Uri uri = Uri.parse("http://docs.google.com/viewer?url=" + url);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "text/html");
startActivity(intent);
}
if (url !=null && url.endsWith(".pdf")) {
Uri uri = Uri.parse("http://docs.google.com/viewer?url=" + url);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "text/html");
startActivity(intent);
}
Sunday, May 13, 2012
XML based Drawables
Android gives you the ability to define shape drawables using xml.Basically these are simple shapes that are great for using as backgrounds or buttons. The sample below will create a white oval that I use for backgrounds of linear layouts, say for a product description with an image and price. More information is at the link. To add a drawable, pick one of res/drawable directories and save the xml file right to it. For example, I saved a file called shape_white_four_corners.xml to my drawables-hdpi directory. It'll appear as part of your resource and you can access it just like any other drawable.
http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:background="@color/white"> <gradient android:startColor="@color/white" android:endColor="@color/white" android:angle="270"/> <stroke android:width="0.5dp" android:color="@color/white"/> <padding android:left="7dp" android:top="7dp" android:right="7dp" android:bottom="7dp" /> <corners android:bottomRightRadius="8dp" android:bottomLeftRadius="8dp" android:topLeftRadius="8dp" android:topRightRadius="8dp"/> </shape>
Subscribe to:
Posts (Atom)