Introduction: Reuse JAVA Written Code in Android
For this instructable I will demonstrate how to reuse, apply and integrate a simple java program into an Android app with GUI.
This tutorial is for those who has already a bit or some experience in eclipse (android sdk), java coding. Nonetheless the finished project (as zip) is provided at the end of the instructable.
Android SDK, Java development kit 1.6 - current
Step 1: Integrate Native JAVA Code in Android
I was wondering if it is possible to integrate a native JAVA program into an Android app. So Android is based on JAVA, but it has a VM called DalvikVM instead of JavaVM. It's like you have to express certain code parts in a different way to get similar results, although the syntax are the same.
Step 2: Differences in GUI Programming
GUI Programming is different if you compare the JAVA 'dialect' from Android with the 'standard'. In Android you define the GUI elements in a XML file, then combine with JAVA syntax. In native JAVA there are frameworks like Swing, JFrame, AWT, so that you code the gui all in one language.
You have to try to find a similar equivalent for certain methods, listeners or tasks. For example there is no ActionListener (that I am using frequently) the similar equivalent would be the onClickListener. What I have also missed is JPanel to develop the GUI in small structured parts and put together in a JFrame.
But nonetheless it is a huge benefit if you have already programming experience with the native JAVA language.
Step 3: Reuse of Java Code in Android
What could you reuse from a native java program for an android app?
In our instructable I have used for example a simple sorting algorithm. So the good news is you can reuse the algorithm without changing a single line (image above)
What the code does:
It sorts any type of data (Strings, integer, char...) , also known as Generics, as you can see the return type is a template that stands for generic type. It is also a sorting algorithm based on Bubble Sort, with a small modification, it can sort backwards too (descending). The if clause checks in which order the elements should be sorted.
Why using one of the slower algorithms?
I could have used the already implemented sorting method in JAVA (Collections.sort(), Arrays.sort()). but the code above has an educational purpose, how things work, how to integrate code in a GUI. You won't also notice the difference between faster and slower algorithms if the number of the elements are less about ~100.
Step 4: Testing Code With JDK
Test the code in the main (image above) or with JUnit.
What it does:
So that's the starting point of the program. Declaring arrays of integers, Strings with examples and passing to the sorting method. Our testprogram should be also able to sort letters of a single word alphabetically. For example cup -> cpu -> upc, eat -> aet -> tea
Step 5: Results of the Test
Should look like the image above.
Here is the dllink of the project:
in eclipse or Android SDK general -> import -> existing projects into workspace -> select archive
Step 6: Create an Android GUI and Reuse the Existing Code
Now we come to the interesting part, as I said we can reuse the logic part of the program. Our attempt is now to create the GUI and connect with the algorithm.
We start a new project: File -> New -> Android Application Project and leave everything with default settings. (4x times next and finish)
The image above shows which files are to edit. (yellow marked) . In the src folder copy the bSort.java in a package or directly (depends on how you structure the files)
Step 7: Edit the Activity_main.xml
In activity_main.xml change the first tag (RelativeLayout to LinearLayout) and add the line
android:orientation= "vertical" so that the gui elements are aligned vertically and terminate with (last line).
Inside the LinearLayout tag we have a Textfield , with three RadioButtons, a checkbox and a nested LinearLayout for aligning the buttons horizontally. (second picture)
Step 8: Edit the Strings.xml File
In the next step the GUI elements (Buttons, checkbox...) has to be labeled. Through the @string name from activity_main.xml you can access to the gui element and label these.
Step 9: MainActivity.java
The starting point of an Anroid app and where we assign functions to the buttons with listeners.
At first we make instances of gui elements, an instance of the sorting algorithm and 3 sorting modes (a sequence of integers, a word, a sequence of Strings)
Inside the buttonListener() method we have 3 Listeners:
- for the Checkbutton that checks if descending order true (checked) otherwise false (unchecked).
- A sort button that contains the 3 sorting modes. The Text of the EditText field is stored as a single String so you have to split them into parts with the split method as array, which is accepted by the sorting method.Depending on the sorting mode you have to filter certain Strings with regular expressions into Strings with numerals, a single word which is split in letters or only numerals. The output will be set, overwritten in the textfield itself.
- And at the end the last button is the clear button, simply clears the textfield.
Step 10: The Resulting App
Should look like in the picture above, it sorts the word 'cat' as 'ACT' and voila.
The whole project is stored as zip, just import like a normal java project.
Thanks for your attention!
Step 11: Update: Upper and Lowercase
The previous version of the program could only sort uppercase letters. When we allow sorting with upper and lower case letters, then the order would we wrong (second pic)
So the cases has to be ignored during the sorting process with .compareToIgnoreCase() and the type has to be casted as String. The preferred result is showed in the third pic. (Updated project archive as download)