Java Swing to Android

Here is a quick, brief 10,000ft overview/observations on Android from a Swing developers point of view.

Firstly if your coming to Android from Swing you are approaching it with a big advantage to J2EE developers. Now I realise Android doesn’t actually have the AWT/Swing API’s but that doesn’t matter as the general front end/event driven mechanism in it is so close to Swing that you will find it easy. Now Android does rely a lot more on XML defs than Swing ever did and that took me a while to get my head around and realise how powerful a feature it actually it is if used correctly.

I started off by buying a couple of books:

Learning Android
Programming Android

I’d recommend both of these as they cover the basics very well, from then on you can use the online Android SDK docs. Other books I’ve seen are not great to be honest, you will be better off googling.

Android GUI is like Swing, it’s on a single thread, if you do some heavy lifting on that thread it will cause some issue’s just like with Swing. There are mechanisms and classes available that like SwingWorker help you out (more of this in later blogs).

A typical Android screen is created by an Activity class that you have extended. This class typically inflates an XML definition on the screen layout. The XML defines the textfields, comboboxes (called Spinners) and so on. Eclipse is typically used to create the XML (I use the Graphical Layout Tool in Eclipse which is still very buggy but ok). You can also hard code the layouts but I wouldn’t bother as it’s easier to use the XML. The Activities are state driven, they are created, paused, resumed and so on. The states are there because they run on a small device where calls can come in, other programs started an so on. The screens for your app can be hidden, displayed again, killed off and restarted …. You need to handle this and in some cases persist the data.

The Activity states are a bit of a head scratcher to start with but once you try a few examples out its straightforward enough.

Handling Events from components (Views in Android) is sosimilar to Swing it makes me wonder if somebody copied the event mechanism ;)

Any Activity you create needs to be defined in your applications Manifest file, this file is used by the Android device to figure out you apps main class, the icon, theprivilegesit needs and so on. If you don’t specify an Activity on the manifest then the app will stack trace when you attempt to display it.

Jumping from screen to screen is done by the use of Intents. Intents are used by Android to signal that you want something to happen. You can create an Intent passing in the class that you want to process the Intent (so say an Activity which displays another screen) or you can ask for some operation, so for instance display this web page, Android in this case would open a browser and display the page. If you had several browsers installed it might ask you which one to use. Android keeps an internal stack so that when the back key is pressed then it will switch back from the browser back to your apps Activity (remember the states I mentioned, your Activity has just had its resume state called ;).

Android has some nice features to let you define backgrounds and the look of buttons. This seems to be similar to JavaFX. Basically you can define gradients, patch 9 images (a way of taking a small image and extending it to fit a background nicely), shapes …….

Android comes complete with SQLite3 which is a nice lightweight database. I’ve found it very quick and nice to use. I’ll return to this in another blog as I’m not sure the preferred way to use the database in the most books is actually a good idea.

Finally there is a emulator to test your apps on. Its ok, lets you test things on different versions of android and different device specs (screens, memory, input methods (rollers, soft/hard keyboard)) but its slow and really I’d say if you want to really write and app you need atleast one Android device. I have a nice Nexus S Mmmmmm lovely.

Anyway thats a very brief overview and when I finish my App I will have some time to spend adding some nice new blogs.

Enjoy

Reference: Swing to Android from our JCG partner Steve Webb at the Java Desktop Development at the Coal Face blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

2 Responses to "Java Swing to Android"

  1. Max Nelson says:

    For real Swing aficionados and people with existing desktop applications I’d recommend looking at AjaxSwing. It automatically converts Swing apps to HTML/JavaScript so they can be viewed on Android, iOS and any other web client.

    http://www.creamtec.com/products/ajaxswing/solutions/java_swing_ui_on_ipad.html

  2. elad says:

    how Android to Java Swing ???

Leave a Reply


2 + one =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close