Part of my GTD system includes sending notes to myself in order to capture anything that comes to mind that I’ll want later. Android’s “note to self” voice action is great for this kind of thing. Continue reading
Tag Archives: android
Installing Samsung Galaxy Nexus Drivers On Windows 7 x64
I was trying to install an Android app I wrote onto my phone, but I ran into an issue.
Eclipse wasn’t recognizing my (Verizon) Samsung Galaxy Nexus (Model #: SCH-I515MSAVZW) as a debugging device, so I had to install drivers for it. When I tried to install the drivers from Samsung’s website, though, I received this error: Continue reading
An MVP Pattern for Android
Here’s a blog post I did for my company, Magenic, which demonstrates a simple MVP Pattern for Android.
MonoDroid: XML Layout Not Created in Resource.Designer.cs
I’ve started diving into MonoDroid a little bit, and I was following the directions here to create a ListView. The first step is to create a layout for each of the ListView items, which I followed, but the Resource.Designer.cs file was not updating with my newly created layout.
While developing with Android in Eclipse, the resources are auto-generated by just creating the files in the layout directory, which, evidently, is not the case for MonoDroid in Visual Studio. After searching a bit, I found this BuildProcess page for MonoDroid. It explained that resource files need the Build Action property of AndroidResource in order for the Resource.Designer.cs file to be updated with the layout.
Not sure if it’s possible to change the default, but out of the box the Build Action for an xml file is Content.
To fix:
- Right-click the xml file in the Solution Explorer
- Click Properties
- Change Build Action to AndroidResource
Useful Eclipse Shortcuts
I do a lot of programming on a laptop without a mouse, and Eclipse shortcuts are imperative if I want to remain productive. Even when I do have a mouse, using keyboard shortcuts is usually faster than trying to click around for what I need. Here is a list of shortcuts that I use often.
Ctrl+Shift+l – Show Shortcuts
Navigation
F3 – Go to Definition
Alt+Back – Back
Alt+Forward – Forward
Ctrl+l – Go to Line
Ctrl+m – Maximize/Minimize Active View or Editor
Ctrl+Home – Go to Beginning of File
Ctrl+End – Go to End of File
Productivity
Ctrl+1 – Quick Fix (i.e., create method if you are calling one that does not exist)
Ctrl+7 – Toggle comment
Ctrl+d – Delete line
Ctrl+Shift+f – Format
Alt+Up – Move Lines Up
Alt+Down – Move Lines Down
F2 – Show Tooltip Description
Debugging
F11 – Debug
Ctrl+F11 – Run
F5 – Step Into
F6 – Step Over
F7 – Step Out
F8 – Continue
Ctrl+Shift+b – Toggle Breakpoint
Emulator
F6 – Turn On/Off Trackball
Ctrl+F12 – Switch to Next Orientation (Portrait, Landscape)
Simplifying Context in Android with Roboguice
If you have done any database work for Android by extending SQLiteOpenHelper, you have also had the experience of passing a Context down through the layers of your application to your database adapter. This is annoying because while you want the focus of your programming efforts to be on your application domain, part of your application design is focused on forwarding a framework-specific variable. Fortunately, roboguice comes to the rescue with just a few handfuls of code.
First, you’ll need to create an application class that inherits from RoboApplication:
public class MyApplication extends RoboApplication { }
RoboApplication contains the method protected void addApplicationModules(List<Module> modules)
that you can override and add any modules that you’ve defined to the List, so you can add the following to your application class:
@Override protected void addApplicationModules(List<Module> modules){ modules.add(new AbstractAndroidModule(){ @Override protected void configure() { requestStaticInjection(DBAdapter.class); } }); }
The method requestStaticInjection()
injects static fields that have the @Inject annotation into the specified class. Therefore, we can add the following to our database adapter class:
@Inject private static Provider<Context> contextProvider; private Context mContext; public DBAdapter() { mContext = contextProvider.get(); }
The last piece of the puzzle is for your Activities to extend from a Robo*Activity (RoboActivity, RoboListActivity, etc.) which will hook up the static injection:
public class MyActivity extends RoboActivity { }
Now, whenever an instance of the DBAdapter is created, roboguice will inject our current Context into contextProvider before calling the constructor:
DBAdapter dbAdapter = new DBAdapter();
Another way to inject a context provider into the DBAdapter is to use the usual guice injection strategy of creating an interface, implementing it, and configuring an AbstractModule. This approach requires more overhead, but perhaps creates a cleaner solution. I might write another post detailing this method if there’s interest.
Thanks to Donn Felker for clarifying some points.