Posts

Showing posts from 2010

Working with SQLite in Android. Tools and stuff.

The Notepad tutorial uses sqlite to store the users entered notes. This is a single table, so this method query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)  was fine. It takes a single table name and some params for the other clauses. To use more than one table I used the rawQuery(String sql, String[] selectionArgs) method. This takes arbitrary sql (union in my case) along with values to bind to any ? that you used. I have removed the SQLiteOpenHelper from the code also. This opens and creates your database and handles upgrading the database following schema changes. Overall I found this cumbersome and have switched to moving the database file from the device (emulator) to my pc. I then query it using SQLite 2009 Pro. I got it here . Excellent query analyzer type tool. I make my changes here (schema and data) and then push this back to the device when I am ready to test with the app. The pushing is don

Steps to learn Android

As you can see if you have read any of the other posts on this blog I am in the process of learning Android. I am going to keep a running list of the steps that I am taking to do this. Check this out every day. See what is being discussed. May even give a try at answering something for someone: http://stackoverflow.com/questions/tagged/android Check this every day also: http://android-developers.blogspot.com/ I subscribed to this one in Google Reader. Learn sqlite .

Themes and styles

Extracting style information from your layout files This is generally a good practice. It allows you to keep layout separate from style which in turn is kept separate from the program logic. Web development has gone this way with HTML, CSS and back end technologies like servlets. Android supports this separation too. The layout information is kept in the layout xml files. Style information can also be kept there, but for a large application this will get cumbersome. For example if you decide to change the font used on your buttons you will have a big search and replace on your hands. If you use separate styles it will just need to be changed in one place. The holy grail of programming . Finally the program logic is kept in the class files (Activities and the like). All nice and organised. A further separation that Android promotes is the removal of static strings from the logic. They are placed in the strings.xml file. This seems a bit long winded at first, but once your application

Basic localization

Image
By default in Android all of your static text is in a separate file called strings.xml. This lends itself very neatly to localization. I will just look at localization of strings here. Images, currency, time etc. are more complex but also well supported. First create your application as you normally would with your strings in their own xml file. Next using eclipse right click on your project and select New/Android XML file. In the dialog enter strings.xml as the file name (it won't clash with your existing strings it is going in a separate folder). Select 'language' as the resource configuration and enter the 2 letter code that corresponds to the language you are after. de for German in my case. Click finish. Next you need to translate that text. I just took the whole xml file and pasted it into the input box on Google Translate . It does not mess with your ids as they have underscores in them. It translated most of my text into German. The rest I just manually pasted in

Long and short clicks

Image
I want to set up a clickable list in my application. It needs to respond to both long and short clicks. Because there are a few ways to deal with events in Android this was a bit tricky, but I have it working now and seeing it here might simplify it a bit for other people. Displaying the contents of an array in a ListView This was the first thing I needed to do. Otherwise I have nothing to long and short click. Doing this involves 3 steps. Create a suitable Adapter object which will contain the data and know how to display it Get a reference to a ListView which you have placed in your Activity via the XML layout file Call the setAdapter method on that view to link the 2 together Here is the code: String[] vals = { "Anthony", "Emma", "Aoife" }; ArrayAdapter<String> adapter = new ArrayAdapter<String>( getApplicationContext(), R.layout.my_layout, vals); ListView lv = (ListView) findViewById(R.id.Lis

Notifications

Image
I am going to add a notification to my application. This will place an icon in the phone's status bar. First I need an icon. This link  contains some very handy design guidelines for Android apps. The table just after the start gives details of icon resolutions for different screen densities. I am just going to create a single icon here. I will look at multiple screen densities later. I am less interested now in the design of the icon than I am in how the notification framework operates, so just a simple red box with a dot in it for now. Not the most beautiful thing in the world, but it will do for now.  First up I am going to put a button on an Activity. The onClick will create the notification: private void setupNotify() { int icon = R.drawable.notify; // icon from resources CharSequence tickerText = "Hello"; // this is the text that displays on // the status bar when the // notification is first displayed. // The text of a

An Android Stopwatch TextView

Image
The Chronometer  class which is part of the standard Android SDK is useful for counting the number of milliseconds from a point in time. It does not however allow pause and restart like a normal stopwatch. I needed this type of functionality for my app, so I have extended Chronometer to provide this: public class StopWatch extends Chronometer { private long elapsedBeforePause; public StopWatch(Context ctx) { super(ctx); } public StopWatch(Context ctx, AttributeSet attrs) { super(ctx, attrs); } public void startClock() { setBase(SystemClock.elapsedRealtime()); super.start(); } public long stopClock() { long result = SystemClock.elapsedRealtime() - getBase(); super.stop(); return result; } public void pauseClock() { elapsedBeforePause = stopClock(); } public void restartClock() { setBase(SystemClock.elapsedRealtime() - elapsedBeforePause); elapsedBeforeP

Naming conventions for Android

I am going to collect my naming conventions for Android here. This may sound like reinventing the wheel, but a cursory glance around will show that there is not much agreement on what should be done. It will be short to start with and hopefully it will improve as I go along. I would of course be much happier to use the official version, but I can't find this. If it shows up I will switch and leave a link here. Widgets like buttons:  Prefix them with a short version of their class - example btn for button. Follow that by camel case for what they are. So btnCancel would be your cancel button. This has the advantage of allowing you to easily filter out all non buttons when you are wiring your xml layout files up to your java. btn - button txt - editable text field (EditText) lbl - non editable text fields (TextView) tgl - toggle button String identifiers in strings.xml : use all lower case, separated by underscores. For button labels use btn_ as a prefix.  For example b

First revision

Image
So far the app is very basic. It has a main page from where you can start your ironing or view the results. It just times each of your shirts and stores the result in seconds in a SQLite database. This is the first database access stuff that I have done. It is pretty straightforward. I used the first of the Notepad tutorials as a guide. This is the ironing page. I used a Chronometer to do the timing. This is a Chronometer example from the Android site, which is useful, but does not really tell you how to get the elapsed times. This is of course what you need for any stopwatch type activity. This is  how I did that.  Here is the onClick()  code from my Next Shirt button: @Override public void onClick(View v) { long elapsedMillis = SystemClock.elapsedRealtime() - mChronometer.getBase(); DataAccess data = new DataAccess(getApplicationContext()); data.open(); data.cr

Using SQLite with the Android emulator and Eclipse

One of the ways of persisting data with Android is to use a SQLite database. A fuller list of methods is given here . When you are persisting data in any application one of the things you will want to do is write ad hoc queries to see is you data getting to the database, how big are your tables etc. SQLLite lets you do this with the databases that your applications use in the emulator. You will need to connect to the emulator using the Android Debug Bridge (adb). Type this at a command prompt (you obviously need to have adb.exe on your path, or just open a command prompt in the tools directory of your android installation - \android-sdk-windows\tools in my case): adb -s emulator-5554 shell The name of your emulator may be different, you can check the name in the console of eclipse when you are launching an app to run on the emulator. Once you have run that you will need to use the sqlite3 command to mount the database you are interested in. Enter this at the shell prompt which

The ironing computer

A computer that irons clothes? I wish. No, just one that times your efforts to remove the creases from your shirts on a weekly basis. I mentioned that I had had a go at MIDlet programming on an earlier phone. The Ironing Computer was it. It was a good way to impress my son. Or maybe that was disgust. But anyway, no harm in starting off with something stupid (and indeed easy) as the first go at an app for my HTC Desire. The idea is that the app allows you to click when you start a shirt and times the effort. If you iron 5 at a go (me most weeks) then at the end of the bout of pressing you can see what your times are like. I know this is sad, but sometimes being a nerd does not mean doing useful and clever stuff for NASA. Sometimes it is just stuff that nobody else is interested in. The MIDlet version was brutal, so I should be able to do a bit better on Android. Application layout It's going to have a welcome screen which will contain the menu buttons. Start ironing and view

First day and how I got here

I am going to teach myself to program Android devices. Have always wanted to program mobile stuff, but have not made a decent job of it yet. I got a Dell Axim PDA a good few years ago now. Tried to program this. Not much success I'm afraid. My VB was rusty and the JVMs that I could get for it were not great. Gave up and ended up using that as an expensive to do list. I waited patiently for java to do what it was originally meant to and work on appliances (like phones and TVs). I had a Java enabled Nokia phone then. A 6300 . A pretty good phone if a little old school. Wrote some midlets for it. Misery. Was like programming with my hands tied behind my back. Very fiddly API. Overly restrictive security policies on the phones etc. There was no real likelihood that I would every be able to write something even vaguely commercial for that thing. So I gave up and just used that as a phone. I got an HTC desire about a month ago with the intention of programming it. It is a very nic