Sure, you can launch your Android app in IntelliJ’s debugger, but that’s slow. IntelliJ allows you to dynamically attach the debugger to a running device using the “Attach debugger to Android process” button. That’s fast!
Pivotal Android Tabs
We have published a simple Android project that illustrates how to use tabs in an Android app:
android:divider. Thank you Pivot Ryan, the original author, for taking the time to write and open source this. Check it out, fork it, and enjoy — https://github.com/pivotal/Pivotal-Android-Tabs
See Hex’d Colors
IntelliJ trick: in a
colors.xml file, place your cursor on a hex value and hold down
Shift. You’ll see a large preview of the color.
Colors and States
(Repost from the 6/22/2011 Standup blog): You can use a selector drawable to set Android text color for the various states (focused, selected, etc.) using a drawable xml file. IntelliJ will complain and say this is invalid syntax but the application will use the file as you would expect. This only seems to work for the
android:textColor attribute in
Android ListView#addHeaderView and ListView#addFooterView methods are strange: you have to add the header and footer Views before you set the
ListView‘s adapter so the
ListView can take the headers and footers into consideration — you get an exception otherwise. Here we add a
ProgressBar (spinner) as the
// spinner is a ProgressBar listView.addHeaderView(spinner);
We’d like to be able to show and hide that spinner at will, but removing it outright is dangerous because we’d never be able to add it again without destroying the
ListView — remember, we can’t
addHeaderView after we’ve it’s adapter:
So let’s hide it! Turns out that’s hard, too. Just hiding the spinner view itself results in an empty, but still visible, header area.
Now try to hide the spinner:
Result: header area still visible with an ugly space:
The solution is to put the progress bar in a
LinearLayout that wraps it’s content, and hiding the content. That way the wrapping
LinearLayout will collapse when its content is hidden, resulting in a headerView that is technically still present, but 0dip high:
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <!-- simplified --> <ProgressBar android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
Then, set the layout as the header:
spinnerLayout = getLayoutInflater().inflate(R.layout.header_view_spinner, null); listView.addHeaderView(spinnerLayout);
And when we need to hide it, hide the layout’s content, not the layout:
Now the header disappears from view. No more ugly space at the top!
Un-shadowed Method Warnings!
On two workstations every unit test was failing with the following error:
// …many levels of stack trace, finally: Caused by: com.xtremelabs.robolectric.bytecode.IgnorableClassNotFoundException: msg because of javassist.NotFoundException: android.content.DialogInterface$OnShowListener at com.xtremelabs.robolectric.bytecode.AndroidTranslator.onLoad(AndroidTranslator.java:80) at javassist.Loader.findClass(Loader.java:340)
Cause: there was an old
tmp/cached-robolectric-classes.jar that was causing these errors and our tests ran successfully after deleting it. That’s two answers from Pivot Tyler!
Don’t DDOS Yourself With Your Own App
The Bump Android team wrote an article about a good idea gone wrong. Moral of the story: not all devices behave the same, and this might cripple your servers rather than the devices.
Robolectric Google Group
Join it, contribute, and learn about unit testing your Android apps.