Update: There is a problem with this method. It appears that the Android framework will not pickup the bool values using any type screen size modifier (e.g. sw600dp or xlarge) because they can change at runtime. It won’t give you an error, instead it’ll just completely ignore the resource and default to true. You can of course achieve the same results in other ways but unfortunately they’re not quite as clean as what i had hoped to achieve below. Sorry!
I already created a tablet app a while back when Honeycomb was first released but since then I’ve been working on phone only applications. Between then and now, the compatibility library has been released and a new way of handling screen sizes has been introduced into the Android framework. Below is a recipe on how to use these features to architect a single apk app that will run on both phone and tablets. Credit to Nick Butcher for inspiration from his talk at Droidcon UK 2011.
1) Encapsulate the functionality that you had (or would have) in your activities into Fragments
A Fragment is, generally, a chunk of a user interface with it’s own lifecycle. This sounds similar to how you might describe an activity doesn’t it? In fact, if you’re adapting an existing app, you can pretty much cut and paste your code from your old activity into your new Fragment. Then your old activity is only responsible for instantiating your new Fragment and listening to any events that it might trigger.
2) Create completely separate Activities to handle single pane layouts vs dual pane layouts (i.e. phone vs tablet)
Don’t think about hdpi, large or xlarge buckets any more. Think more like a responsive web designer e.g. here is my single pane layout for screens less than 600dp and here is my dual pane layout for screens wider than 600dp.
By way of example, an imdb style app might have two fragments: MovieListFragment (to show a list of movies) and MovieDetailFragment (to show the movie detail).
When designing for the single pane layout (e.g. for phones) you would most likely have two activities:
- SingleMovieListActivity – which is where the MovieListFragment would live and
- SingleMovieDetailActivity - which is where the MovieDetailFragment would live
Pretty simple eh? For the dual pane layout (e.g. tablets) you would have something like:
- DualHomeActivity – which would house both Fragments; MovieListFragment (on the left) and MovieDetailFragment (on the right).
Keeping them separate means that you don’t have messy code within your activites to detect which screen mode you are in before you can show or act on anything.
3) Use the (force) framework
You don’t need a splash activity to detect how many pixels are on screen before you forward to an appropriate starter activity. Simply add something like android:enabled=”@bool/single_pane” to any single pane activity elements within your Android Manifest. Then create the file res/values/bools.xml with the following contents:
<?xml version="1.0" encoding="utf-8"?> <resources> <bool name="dual_pane">false</bool> <bool name="single_pane">true</bool> </resources>
Handling the dual screen activities is also quite simple. Add android:enabled=”@bool/dual_pane” to any dual screen activity elements within your Android manifest. Then create the file res/values-sw600dp/bools.xml with the following contents:
<?xml version="1.0" encoding="utf-8"?> <resources> <bool name="dual_pane">true</bool> <bool name="single_pane">false</bool> </resources>
Now if your app is started on a device that has a width of at least 600dp (e.g. a tablet) then the framework will enable all of your dual pane activities and disable all of the single pane ones. Conversely, if your app is started on a device with less than 600dp then all single pane activities will be enabled and the dual pane ones will be disabled.
Note that the sw600dp modifier will only work for Android 3.2 and above so you should also copy the above bools.xml to res/values-xlarge/bools.xml to cater for the extra large screens in older versions of Android.
4) Decouple communication
So you have all these reusable fragments…but they can exist in different activities depending on whether you’re running on a device that has a width of more or less than 600dp. How does your fragment know whether to transition to a new screen or whether to animate in a new fragment when a movie is selected? The answer of course is that it doesn’t and shouldn’t.
The enclosing activity knows whether you are in single vs dual pane mode so you need a clean way to communicate your event (e.g. movie selected) to this enclosing activity. There are a few ways to do this but my favoured way is to use Broadcast Intents. Your enclosing activity (which is listening for these defined events) can intercept a broadcast intent and associated parameters and then decide whether to transition to a new activity (if in a single pane activity) or whether to replace the currently showing MovieDetailFragment (if in a dual pane activity).
Simples.
Show right-to-left Activity Animation when you click Home icon in your Android App (Dashboard Pattern)
September 6, 2011
Want to animate your activity from right to left when you click the home icon in the top left corner of your app?
Simples:
public static void goHomeTop(Activity context) {
final Intent intent = new Intent(context, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
context.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
}
Resetting OS X Lion back to factory settings
August 16, 2011
There seems to be a lot of confusion about how do this. Actually it’s pretty easy:
- Reboot your machine and hold down CMD+R.
- Choose the option relating to Disk Utility and erase the Macintosh HD.
- Close Disk Utility and from the list of options, reinstall Lion.
(Note, you’ll need to hook up your machine to a wifi Hotspot so that it can download the entire Lion image from the internet…zzzzzzz)
Install sqlite3 on your rooted Google Nexus One / Nexus S
August 5, 2011
Please note that all credit goes to Sun Chen for this solution (blogpost here: http://chensun.net/android-development/install-sqlite3-on-google-nexus-one/54/).
I simply applied the same technique to my Nexus S and have reproduced the steps below for my own future benefit.
[Notice! You must have root access on your Nexus One / Nexus S.]
1. Download sqlite3, which comes with SuperOneClickv1.7-ShortFuse, and copy it to your SD card.
2. Connect your phone to your computer. Open a Command Prompt, and type the following commands:
adb shell
$ su
# mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system
# dd if=/sdcard/sqlite3 of=/system/bin/sqlite3
# chmod 4755 /system/bin/sqlite3
# mount -o remount,ro -t yaffs2 /dev/block/mtdblock3 /system
Done! Now you can use sqlite3 to examine database on your Nexus One / Nexus S from a Remote Shell or even from an Android Terminal Emulator.
Start animation in onCreate or onResume on Android?
June 8, 2011
Maybe you’d like to perform a nice subtle animation such as fading some text in as soon as your activity launches? If you try to start this animation in onCreate() or onResume() you’ll be sorely dissapointed.
One possible solution is to set a timer in one of these methods so that the animation will spawn after a fixed interval. This approach is problematic because if the delay is too short the animation won’t start (as before) and if it’s too long then the user experience will drop. Furthermore, such a delay might not behave quite the same on a G1 compared to a spanking new Samsung Galaxy S II for example.
A better way is to start the animation in the onWindowFocussedChanged method which is called when the current window of the activity gains or loses focus and gives the best indicator whether the activity is visible to the user. You can use the *hasFocus* parameter to distinguish between gaining or losing focus. Example below:
private TextView myTextView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
myTextView= (TextView)findViewById(R.id.my_textview);
anim = AnimationUtils.loadAnimation(this, R.anim.fade_in);
}
@Override
public void onWindowFocusChanged (boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus)
myTextView.startAnimation(anim);
}
The animation itself is defined in an xml file in res/anim/fade_in.xml:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="1500" />
This may be obvious to some but not me. ..
Like a good little programmer, I use strings.xml all the time in my Android applications which means that this file can get quite large. For a customer that wanted to be able to modify some settings before recompiling and distributing to their customers, I wanted to extract those variable “settings” into a different file for clarity.
It’s actually extremely easy to do. So easy that I thought it couldn’t possibly be that easy and started exploring more complicated routes.
All you have to do is create a new xml file e.g. “settings.xml” and then move your string value (e.g. <string name=”default_retry_time_value”>10</string>) from “strings.xml” into this new file. That’s it!
You don’t need to update any views in your layouts to do to something like this android:text=”@settings/default_mcode_value”.
You just need to make sure that your settings.xml file is in res/values (same place as strings.xml) and Android will pick it up.
