Posts

Showing posts from November, 2011

Loading data into an android database

Image
AutoCompleteTextView I wanted to demonstrate how it is possible to back an AutoCompleteTextView  with data from a database Cursor. To do this I needed to put a few thousand rows of data into the SQLite database on the device. I got my list of words here: http://www.wordlistgenerator.net/ I just left it with the default options to give me a nice long list. I took the apostrophes out to avoid having to escape them - I am only looking for semi junk input afterall. Location of 'words' file I saved the list into a file called 'words'. Note that there is no extension here. I am going to use this as a resource in Android and the extension interferes with this. I dropped the file into my res/drawable folder (as good as any) in order to be able to access it with my load method below. Then I used the following code to load the rows from words into the database: public void load() { SQLiteDatabase db = getReadableDatabase(); try { InputStream is =

AutoCompleteTextView backed with data from SQLite in Android

Image
This is a handy piece of code. As your database gets bigger you can allow the user to pick the correct value by typing. If the db gets very big, you can always increase the threshold, so that the query does not run until there will be a suitably sized resultset returned. Running it at a threshold of 1 is fine for very small resultsets.  Here is the code you need to back your AutoCompleteTextView with a cursor adapter. @Override protected void onResume() { super.onResume(); text = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); final AdapterHelper h = new AdapterHelper(this); Cursor c = h.getAllResults(); startManagingCursor(c); String[] from = new String[] { "val" }; int[] to = new int[] { android.R.id.text1 }; CursorAdapter adapter = new MyCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, c, from, to); adapter.setFilterQueryProvider(new FilterQueryProvider() { public Cursor runQuery(Cha

Custom RadioButton with new XML attributes on Android

Image
The 'conversion' RadioButtons I am building an app to log a users Pedometer steps. It has a set of RadioButtons for different sports. By clicking on these buttons the user can choose a conversion ratio to steps. This allows them to take part in a variety of sports and still count them as 'steps' toward their goal. I wanted it to be easy to add new sports to the list without having to do any database work, or other programming. The way I chose was to extend RadioButton to take another XML attribute for conversion factor. This would be read from the file on construction of the button and could then be applied to any value entered. This is what the new class looks like: public class ConversionRadioButton extends RadioButton { private float conversion = 1.0f; public ConversionRadioButton(Context context, AttributeSet attrs) { super(context, attrs); conversion = Float.parseFloat(attrs.getAttributeValue( "http://schemas.android.com/apk/res/ie.n

Date comparisons in SQLite on Android

Image
I have been tripped up a few times using date comparisons in SQLite. This is where I pass in a correctly formatted date string and want rows from the database which are equal to this for example. I get this to work in a couple of steps: 1. Every date I put in the database goes through this method. It tries to parse the date according to a pattern I have defined and if the date wont parse, returns a null. public static String dateForDb(String date) { String formattedDate = null; try { Date d = null; d = DateFactory.SDF_SHORT.parse(date); if (d != null) { formattedDate = DateFactory.SDF_SHORT.format(d); } } catch (Exception e) { e.printStackTrace(); } return formattedDate; } This ensures that everything that gets into my database is correctly formatted for the next bit. 2. In my sql statement I take out the date = string stuff and replace it with a static call to this method: public static String dbDateCompare(St

Update to Pedometer Log deployed today

My latest app is called Pedometer Log. It is a way for users of pedometers to log their daily steps and try to achieve goals be they related to weight loss, or just staying in shape. It tracks the number of steps taken in the previous week and the week before that and show how you did relative to your goal. A common target among pedometer users is to do 10,000 steps in a day. It's a lot of walking, but it is worth investing the time for a while to see if it works. If you want something a bit faster than walking there are a number of other sports in there for which equivalent step conversions are included. This allows you to move toward your goal while getting some variety in your workouts. You can find the app on the Android market here .

List of required Android skills - a follow up

Image
So it has been a month since I first blogged about the skills I thought I needed to consider myself a decent Android programmer.  So here is the list reposted along with updates of how I am doing along with some new additions. Build arbitrarly complex layouts  - this is a bit vague. I can certainly build more complex layouts now than I could a month ago, but I would not say that I find this stuff easy yet. Probably just need more practice.  Services Bound service and a  s tarted service - have done both of these since the original post. A tricky subject, but I think I have it down as well as I need it for now. Broadcast receivers Create own (with custom broadcast) and  Respond to a system one - Both of these are done. Have written a few broadcast receivers of my own and have an app in the market which responds to the rebooted broadcast from Android. Database - This has always been a strongpoint for me. Nothing more required really.  Shared preferences - Done. In

Our 2nd App released

Today we released our second app. It is called Piano and is a game for small children. It features bright colours, sounds of animals that they like and might keep them amused for a few minutes. It's free to download at the market: https://market.android.com/details?id=ie.nolantech.piano

Google's guide to the app universe

This is an excellent site for budding developers. I took a look at it when it was first released (earlier this year, I think) and liked what I saw, but I was not doing any real android development at that stage. I am developing software now, so it has been worth the refresher.

Providing default values to a custom View via the XML

I used this post recently to get started on a custom Time picker for my app. It works pretty well, but I needed to add default values to it from the XML. I found another post  which did just that. I am just going to show the combined result here. First here is the code for the Time picker. This is mostly the same as that provided by Eric Bessette with the addition of the default reading stuff. The relevant parts are in bold. package ie.nolantech.flow; import ie.nolantech.util.TimeSplitter; import android.content.Context; import android.preference.DialogPreference; import android.text.format.DateFormat; import android.util.AttributeSet; import android.view.View; import android.widget.TimePicker; /**  * A preference type that allows a user to choose a time  */ public class TimePickerPreference extends DialogPreference implements TimePicker.OnTimeChangedListener { private static final String TAG = "TimePickerPreference"; private int mHour = 0, mMin

Getting yourself a 'bash prompt here' for cygwin

When I am setting up a new windows PC one of the first things I do is to set up a ' command prompt here ' shortcut on my context menu in windows explorer. This is fairly straightforward and there are a number of ways to do it. The one at the link given above is pretty fool proof. I do however use cygwin for most of my day to day command line needs. Getting to a specific directory in cygwin is a bit trickier than getting to C:\... This link describes the simplest way to get this job done for Cygwin. Note that this is just for Cygwin. If you are using another flavour of bash for example you will need to dig that answer out too.

Using Google Analytics in Android

Even though Google Analytics is aimed squarely at web applications, they have written a library for Android which allows you to use their considerable reporting and analysis tools from within an App. It's very simple to use. First get set up as an analytics user. This is free and results in you getting a code a bit like the maps api key. Then download the Android Analytics SDK  and make put the jar in your apps lib dir (add to the build path too of course). Next pick a sample Activity from one of your apps (preferably one you have deployed to the market so that your data is real, not just you poking around) into which you will place the tracking code. There is no point in putting tracking code throughout your app (or apps) until you know what you are doing. There are a number of things you can track, but for now you just need to be sending back a row to the analytics server whenever a user carries out some action - say clicking a button. Get an instance of the tracker (do thi

Exporting drawings from Inkscape using the command line

Inkscape  has a command line that lets you do stuff in batch form. The most obvious use of this for an Android developer is to produce the various image resolutions required to support multiple devices on Android. This code opens inkscape, opens the drawing indicated (I created the $desktop variable to facilitate this) and then exports this drawing as a png file. $ inkscape --file=$desktop/inkscape-work/mystar.svg --export-png=$desktop/myfile.png -D The -D switch makes sure the whole drawing is exported, not just the page, or selected objects (you can select objects from the command line too). Width and height can also be provided, or you can give a resolution for the output. This is where the value will be for me. I will get this bit working next time I need multiple resolution drawings. Some of the command line options can be found here .