Android – Validate email input from user

Android | Tuesday October 28 2014 11:53 | Comments (0)

I’ve previously run into situations where I’ve had to validate email addresses in Android, and I’d usually do this by employing some form of regex to accomplish this task. However, I’ve just come across another method. Maybe this is well known, maybe its not. All I know is, I’ve just discovered it myself.

This method uses built in Android pattern matching. It might not be an exhaustive regex pattern, but it seems to get the job done.

The API url is here:

http://developer.android.com/reference/android/util/Patterns.html#EMAIL_ADDRESS

It doesnt provide any information, which is probably why it doesn’t seem as widely used as it should be.

Anyway, to validate an email address using this method, try the following in your own method:

        Pattern pattern = Patterns.EMAIL_ADDRESS;
        if(!pattern.matcher(email).matches()) {
            Log.i(TAG, "email doesnt match pattern");
            Drawable icon = getResources().getDrawable(R.drawable.error_icon);
            emailTextBox.setError("Bad email. Please try again!", icon);
            emailTextBox.requestFocus();
            return false;
        }

Easy!

The NEW Android Location API

Android | Tuesday September 2 2014 11:18 | Comments (0) Tags: , ,

Since the release of the new Google Play Services 4.2, some new APIs have been added and Google have changed their Client API Model. One such API model to be affected is the Location API.

If you are targeting the latest version of Android in your app, you will see that your existing Location API code will now be flagged as being deprecated.

Previously, you would interact with the Location API as follows:

    LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    String provider = locationManager.getBestProvider(criteria, false);
    locationManager.requestLocationUpdates(provider, updateInterval, 0, this);

    ....
    public void onLocationChanged(Location location) {
        //Handle location update
    }

Now with the new Location API, it will look similar to this:

    GoogleApiClient mGoogleApiClient;
    LocationRequest mLocationRequest;
    ...
    mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addApi(LocationServices.API)
    .addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) this)
    .addOnConnectionFailedListener(this)
    .build();

    ....
    public void onConnected(Bundle arg0) {
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(UPDATE_INTERVAL);
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

        ...
    public void onLocationChanged(Location location) {
        //Handle location update
    }

You’ll need to import:
com.google.android.gms.common.api.GoogleApiClient;
com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener
(note it’s the GoogleApiClient, instead of the GooglePlayServicesClient version).

As well as LocationListener, your class also needs to implement ConnectionCallbacks and OnConectionFailedListener (assuming the same class is handling all the callbacks).

Also, onDisconnected() has been renamed to onConnectionSuspended(int cause).

Quick android tip, with ems (setEms())

Android | Saturday April 13 2013 14:26 | Comments (0) Tags:

I recently ran into an issue with my TextView not wrapping text. For some reason, if the line was too long, it would continue off screen.
I tried many different fixes from across the web, but none worked. I thnk the main problem was that I was not using a defined layout in XML, but rather creating the textview dynamically upon encountering an error.

The answer that I finally settled on was the use of em’s (http://en.wikipedia.org/wiki/Em_(typography)). This type or measurement relates to font sizes, and in Android, helps keep fonts and other objects the same relative sizes across any screen size or resolution.
Loosely speaking, a typical screen width in ems, is equivalent to (the screen width / 24). Going back to my original problem, where I had the text go off the screen, I could use this calculation to set a max width of the text view, and manually make it wrap the text.

Since I also target older versions of android, eg 1.6 and 2.2, I had to use an older deprecated method to get he screen width. If you only target newer versionm eg API 17+, you can use the DisplayManager, but if like me, you want to have maximum compatibility, then you can use the following code to set the width of a layout to be equal to the screen size of whatever device you’re on:.

Display display = activity.getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
TextView tvError = new TextView(this);
tvError.setEms(width/24);

Android does come with some built in functions to help text wrap, but from some discussions online, I saw that certain themes, eg Holo, can cause issues like what I’ve experienced. This fix only came about because I didn’t want to change from the Holo Theme.

I hope this helps someone.

DDPB error: Cannot find BarDeploy.jar

Blackberry | Friday January 11 2013 23:24 | Comments (0) Tags: ,

While testing my apps on my Blackberry PlayBook, I sometimes have to sideload them if I am on another computer without the proper SDK tools installed.

For sideloading, I use a tool called DDPB. It connects to your device, lets you select the .BAR file, and basically handles everything for you.

However, after transferring my copy of DDPB over to another machine, I ran into a problem. When I try to connect to my device, it gave an error: Unable to access jarfile C:\Downloads\BarDeploy.jar

As it happens, I had copied it from a 32-bit machine to a 64-bit machine. DDPB is installed in Program Files directory in Windows, but on 64 bit machines, the 32bit program files directory is called Program files (x86) so when I transferred the backup over, it had the old path and could not find the jar file.

Long story short, if you get this error, just recreate your shortcut to DDPB!

Making games for beginners

Blackberry | Thursday November 22 2012 22:21 | Comments (0) Tags:

Following on from my last post from a number of months ago, RIM were at it again! The latest freebie deal was part of their 36 hour Game Port-a-thon, which was held last week. Rather than give everyone a free PlayBook tablet, they opted for another approach. For every game app that was submitted, they would give you US$100, with further rewards for submitting more apps.

For example, if you submit:
< 3 games, you get US$100 per app,
3-5 games, you get US$100 per app, free PlayBook,
5-10 games, you get US$100 per app, free Playbook, free BB10 phone (first 100 only)
10+ games, you get US$100 per app, free PlayBook, free trip to Dev Conference in Sand Diego (first 10 only)

As you can see, its quite the deal.

MAKING GAMES
When I found out about this deal, I figured I wanted some of the action. Unfortunately, I have never made a game in my life! Not even a small one! I began searching for Idiots guides to making games and beginners tutorials, and I found some good ones. However, since I only started looking 3 days before this Game Port-a-thon, I didn’t have time to learn all these skills. Instead, I found a clever piece of software called Construct 2 (www.scirra.com). Construct 2 enables you to make relatively basic games, with relative ease. The interface is almost drag and drop. There’s a small learning curve at the start, but once you get your head around it, its very manageable. The games get created based on events, and all of the physics is handled internally so you can get results very fast. See the video below for a quick sample.

Construct 2 demo

I was impressed with Construct 2 actually, so much so that I might even splash out the $100 on the full licence! I decided to try the free version for a night to see if it was any good, read one or two tutorials, and found that it was actually pretty easy to set up and get going, at least for the basics. I didn’t try the more complex games, mainly because I didn’t have time, but also because the free version only allows 40 events per game. This is still a very usable limit, and some very fun games can be made with this. The software is aimed at non-developers and requires little or no coding for basic games. In the space of a few days, I was able to create 3 basic, but half respectable HTML5 games.

Platform Conversion
Contruct 2 also comes with the ability to export your game into an array for platforms. For the Blackberry Game Port-a-Thon, obviously you had to submit the game in a format suitable for their devices. This is why I chose to export my games as HTML5. If you read my previous post about porting Android apps to the PlayBook, you might be aware that RIM created an Eclipse plugin that allows Android developers to port their Android app for use on the PlayBook in about 3 mouse clicks (once configured). Similarly, they’ve also created a tool for converting HTML5 games into apps compatible with their own devices.

To cut a long story short, I was able to create 3 games in as many days, with experience in making apps, but no experience in making games, and I was able to make them compatible with RIMs upcoming BB10 OS. In that time, I looked around for the quickest ways of producing games and the quickest, hands down, was Construct 2.

Also, as far as I know, you can convert HMTL5 games into Windows Phone, Android and iOS formates, so its win/win all round. Construct 2 even comes with PhoneGap compatibility to do all conversion for you!

So, if you want to get started at making games, and haven’t got the programming skills, then try Cosntruct 2. For more advanced games (and programmers) however, maybe Unity might be a better solution!

Free Blackberry PlayBook for Android developers

Blackberry | Tuesday February 14 2012 22:21 | Comments (1) Tags: , , ,

I recently got an email from a tech magazine I’m subscribed too. Usually they have interesting articles, and all the news in the world of Computers and Technology, as would be expected. However, in last weeks magazine, there was an interesting article relating to Blackberry and their flagship tablet, the PlayBook.

With the imminent release (at time of writing) of their OS2.0 software, Blackberry have decided to give away free (yes, FREE) PlayBooks to any developer who submits an app specifically for the tablet before the deadline. Since the new OS contains its own implementation of Android, which allows Android Apps to run pretty much natively, the offer was originally open exclusively to Android developers. However, with a flood of complaints from existing Blackberry devs, and some noise from iOS and WP7 developers, Blackberry decided to open up the offer to any dev, of any platform, who submits an app before the deadline. Originally the deadline was Monday 13th Feb 2011, but since they’ve had an overwhelming response (over 6000 new devs submitting at least 1 app) since last week, they have extended the deadline. Developers have until Wednesday the 15th of Feb to register as a dev, and until March 2nd to submit an app.

Blackberry PlayBook

There is a bit of small print, however. Not every app will be accepted, and qualify for the free PlayBook. The app has to be of sufficient complexity, and be deemed (by a human tester) to be something that people will be interested in. Examples of apps that will be rejected are: Web Browsers, shortcuts, themes/wallpapers and web apps. They’ll also reject apps with a single function like a “Fart Button” or Buzzer app.

Since I stumbled across this offer, I have submitted my Lotto App, which has been accepted. I even got the email asking for my shipping address, and expect the tablet to be shipped any day now, all for the one time fee of ZERO!! :D

For anyone looking for info, here’s the latest link with info on the deadline extension:

Free PlayBook offer extended

Technical Specs of PlayBook:
1Ghz dual core processor
1GB RAM
HTML5 / Flash 10.2
Proper multitasking due to QNX
7 inch screen
Weight: 0.9lb/425g
Width: 7.6/194mm
Height: 5.1/130mm
Thickness: 0.4/10mm

1080p Playback
3MP front camera
5MP back camera

New Android app: Medical Student MCQ App

Android | Saturday October 22 2011 18:08 | Comments (0) Tags: , , ,

So, after being bitten by the Android Publishing bug with my first app, the Irish Lotto helper app, it’s no surprise that I jumped at the chance to create a 2nd app, when the opportunity came knocking.

My 2nd app, which has just been published, is called Medical Student MCQ App.

The purpose of this app is to aid Medical Students who are in their final year of med school, by asking them a difficult multiple choice question (MCQ) once per day, for the entire duration of their college year. The theory behind it is if they can learn that one piece of information each day, then by the end of the year they should fair much better in their exams!

The idea for the app came about when my brother, who is an experienced paediatrician, asked me if it would be possible to make such an app. I responded positively and we decided to take it from there, with me doing the technical side, and him writing the questions.

For this to be as efficient as possible, I had to write a basic web form in PHP, using a MySQL backend. He could simply fill in the form with a question, 5 possible answers, a radio button representing the correct answer, and a textarea at the bottom for him to fill in the “tip”. The tip pops up when the user gets the correct answer, and gives them an expanded explanation of why the answer is correct.

Anyway, due to time constraints, we had to enlist 2 other doctors to help with the questions. This worked out well as now we have a team of 3 doctors, each with their own specialised field, able to write relevant and helpful questions.

On the client side, I had to choose between including the full database, or forcing the user to download the database on first run. I chose the latter, and for good reason.

Due to the number of questions, it is possible that errors could sneak in somewhere. Rather than releasing a new app version for each spelling mistake, and hoping everyone upgrades promptly, I thought it would be better to have direct control over the content they see every day. To achieve this, I have put in an “update” button in the menu, whereby the user can download the latest database at the touch of a button. This will include all the updated/refined questions, without forcing an app update through the market. Also, if we want to add new questions, or remove questions completely (maybe due to curriculum changes) I can do so through my little web front end that I built for this.

Medical Student MCQ app, available for 4 euro from the Android Market

The app is available now, for a small price of €4, and is available for download from the Android Market here:
Medical Student MCQ App

-Eoin

New Android app: Lotto Helper IE

Android | Saturday October 22 2011 17:49 | Comments (1) Tags: , , , , , ,

So, after dipping my toes in the Android water over the summer, I began working on my own app.

I took me roughly one month to do it, from start to finish. Admittedly, I slowed down once college started, but that’s to be expected since I did have to learn Python, and Django, as well as how to parse websites!

So, what does my first app do exactly? Well, its called Lotto Helper IE. It is an app to show the user what the most common lotto numbers are in the Irish lottery since its inception in 1988. I basically parsed a website with all the numbers, put them into an XML file, and from there I counted each number and sorted them in order. The problem with this method is that, in 1992 the Irish Lottery Commission added some new numbers, extending them from 35 to 38. The Bonus ball was also added. A few years later, the numbers were then extended to 42. Now, based on frequency alone, this would lead to inaccuracies, since the draw began in 1988 and some numbers were only added 4-6 years later.
To counter-act this, I’ve made a few tweaks. Firstly, I counted how many draws were missed by each new number. Then I subtracted that from the total amount. I could then work out an accurate percentage and sort the numbers by this.

On top of this, I have also added in support for the Euromillions. Similarly, I parse the numbers, store in XML, and then do some calculations. The result gives me an ordered list of the monst common numbers, and how many times they’ve come out.

On the Irish Lotto website, there is a number checker. I’ve checked my predicted numbers and they seem to be doing well. Over the last month, anyone who had played these numbers would have netted themselves over €30! Not bad for a free app!

Apparently, there is big business in betting shops where people can bet on number pairs to come out in each lotto draw. I’m hoping to extend the app to include a feature like this. In the meantime, I’ve a “stats” screen, which displays the raw data for each number in both the Irish Lotto, and the euromillions. I’ve a few hundred downloads so far, which is cool, ranging from Ireland, the UK, and America to Eastern Europe, China and even Brazil!

Lotto Helper IE available for free from the Android Market

My first personally publish app can be download, for free, from the Android Market here:
Lotto Helper IE

-Eoin

So I havent written much lately, neither has Shakespeare!

college | Saturday October 22 2011 17:30 | Comments (0) Tags: , , , ,

I think the last time I put up a blog post was about 3 months ago. During this time, I was working for a small start up company in Dublin, next to the Guinness Brewery! I spent the summer working doing mobile development. I started off doing Blackberry (J2ME) and then went on to do Android (J2SE).

During that time, I worked on 2 different apps. The first one was the blackberry port of THIS, which is the company’s flagship app. Nearer the end of the summer, I was moved on to Android development and given the opportunity to pretty much write my first full app. I didn’t make 100% of the app, but I made most of it. I had obviously worked with Android before, and made a couple of small apps in college, but this was the first one that I would have published to the market (under the company’s name, of course). The result, was iamhere. It also simultaneously got released in China, but since China and Google aren’t getting along too well at the moment, the Android Market is blocked over there. As a result, other methods of distribution were used such as uploading to the many 3rd party Android app websites that have sprung up in China as a result of no Market access. Last I checked, it had at least a few thousand users in China. However, that was over a month ago so I’ve no idea how many users it has now. Still though, its nice to know there are people in a distant part of the globe using something that you’ve made.

Anyway, since the summer, I’ve come back to college to finish my Honours Bachelors of Science Degree in Software Engineering in IT Tallaght. Its pretty challening so far, but I suppose nobody ever said a Degree was easy!

Another thing to mention is, in the final year, obviously, we have to do a project. IBM have an agreement with IT Tallaght that they’ll take the top 5 students every year and allow them to work in IBM for one day a week throughout the year. When I say “work in IBM”, I don’t mean that anyone is getting paid. Basically IBM have projects that need doing, and either they don’t want to do them themselves, or they see this as am early recruitment processes for the prospective graduates, but either way, each student gets assigned one of these projects. At the end of the year, if all goes well, the common thinking is that IBM will offer the new graduate a job. Since an interview is only roughly 30-60 mins, and this project last 8 months, I’m sure they’ll get a better idea of how good a worker the student is come graduation time. I’ve been lucky enough to be chosen for one of these projects.

For the next 8 months, every Wednesday, I’ll be out in IBM HQ in Dublin working on a project that will be used by everyone in IBM. I’ll be using Python, STAF/STAX, as well as some Database backend and other cutting edge technologies. I haven’t started yet, but I’m looking forward to it! Either way, if I don’t get a job after this, I’ll still have a few lines on my CV/Resume saying I worked in IBM for a year,and thats sure to get me any job I want! Or at least, I hope….

Blackberry CRC32 issues

Blackberry | Sunday July 3 2011 13:56 | Comments (0) Tags: , , ,

I’m currently a few weeks into my internship where I’m developing a new blackberry app for a small start-up company. One of the core features of this app is that it can communicate with a remote server. Anyone that knows anything about security will tell you that you simply cannot send these communications in plain text.

So, this week I’ve been working with encrypting strings and sending them to our server. The way we’re handling the encryption/decryption is by using a common method on both client and server side so that messages can be easily decrypted. Pretty common enough. But, in order for the client and server to tell if messages have been tampered with before the arrive at their destination, we also send a CRC of the string that we have encrypted. Once decrypted, we call the CRC method again and can tell if the data is as it should be.

Here’s where the problem arises.

Since the app is cross platform, we have to ensure that the CRC value gotten on a blackberry device matches that gotten on an Android device, not to mention the server will need to be working off the same algorithms. Initially you might think thats no problem because CRCs are universal. Incorrect. It took me a few days but I have realised that the Blackberry implementation of CRC32 is different to the Android and the Apache/PHP implementation. It was frustrating having the Android sending and receiving messages with ease, when my blackberry app was getting errors.

The Solution
Like all good programmers will tell you, there’s no point reinventing the wheel. It felt to me like that was going to be my only solution if I was to ever get this to work, but after spending way too long on this small piece of code, I decided to do what I should’ve done a long time ago….and that is to Google java.util.zip.CRC32 source (and java.util.zip.checksum) and copy and paste the source into a new .java file in my project! Basically the blackberry CRC32 code seems “broken” so why invent the wheel when I can just use the same “working” code that the others use!!!

After I did this, the code worked straight away. I didnt even have to rewrite the code too much because they all have pretty similar methods! Plus, my code is a little bit more “cross-platform” now for when we package all the common stuff into a JAR later.

Next Page »