Taking Advantage of Android OS
Add Some Code
Before we get to the advanced UI stuff, let’s take a quick segue to add some code. This will enable the QuickContactBadge and show you how it works, and it gives us a chance to use the emulator again. You may have noticed a line in my style.xml file on the tablerow style:
< item name=“android:onClick” >onRowClick
Figure 10 shows the code added to both the main.xml file and the IBMi22Activity.java source. Note that the main.xml file got changed as I used the WYSIWYG editor, but I then cleaned it up to make better use of styles. The Java is pretty simple if you’re familiar with Java (and if not, it’s time for you to get familiar—the Welcome screen in Eclipse has a link to a tutorial on creating a Hello World application in Java). When a row is clicked, the function is called with a reference to the clicked row (it’s an object of type View named tableRow). I cast that as a ViewGroup and then get the sales rep from the second column. Note that Android uses a CharSequence instead of a String; it’s a feature of Android and just one of those programming techniques you get used to. In any case, I use the sales rep to change the text of the contact prompt. I then get the reference to the QuickContactBadge and assign the contact from the email address. You’ll notice that I default the email address to the sales rep’s initials at mycompany.com; this is a trivial algorithm but it gives you an idea of how it might work. Once you’ve connected to the business logic, you have several other options as to how to get the email address, so this is fine as a proof of concept.
That’s it for the programming! I’ve successfully attached my list to the QuickContactBadge widget. All that’s left for this article is to test the new code. Right-click on the project again and select Run As / Android Application, and the application will appear on your emulator.
The application starts with a list of orders (Figure 11). Click on one, and the label for the QuickContactBadge changes to indicate who you’re contacting. At this point, you’re still in your own application. But click on the image and that’s when the magic occurs: you’re taken to Android’s contact page for that user and from there you can do whatever you want.
Note that this no longer functionality that I have to program. This is the user’s contact information on their phone, and it’s the phone doing the work for me! They may have email addresses, alternate phone numbers, instant messaging, etc., and if for example they click on the Call operation in the contact page, the phone calls that person. I don’t have to write Java code to do that, Android does it for me. Their phone and the information in it is now another peripheral to your business application! This is some very sophisticated stuff, and it opens a lot of doors for your existing IBM i applications.
One thing you may be wondering about is that only three orders are displayed. We used three because that’s how many the Android SDK added for us when we dropped the table on the WYSIWYG editor. To be truly useful, you’re going to have to support a variable number of rows, and Android can do that. It’s not very hard, but it’s not very intuitive. That’s where we’ll start in the next (and last) article in the series, and then we’ll quickly move on to getting that variable data from an RPG program on IBM i. Visit my website to find the downloadable project and keep an eye out for that next article. In the meantime, have fun with your new Android programming skills.
Like what you just read? To receive technical tips and articles directly in your inbox twice per month, sign up for the EXTRA e-newsletter here.
comments powered by