Showing posts with label Eclipse. Show all posts
Showing posts with label Eclipse. Show all posts

Sunday, April 24, 2011

Simple Backends for Android Apps - Part 1

        In my recent lunacy I decided to take up my friends offer and work with him on a multiplayer android game. It's a dice game, but we hope that people will give it a chance since it's very addicting. I'm currently suffering from Feature Rampage, and I'm deciding to add complex features before I'm done implementing simpler ones. It's a terrible business practice but considering that we specified no budget for this and that we are doing this part time, I don't care ;) One of the features that I decided to implement was online play (including friends and chats)! It's pretty ambitious considering this is going to be the first product I ever made and that I don't know any html, http or any other language besides Java. What can I say? I learn best by doing : D

       For this post I won't dabble too much into the details of the framework, instead I'm going to provide an overview and explain how I implemented push notification for android versions 1.6+.

       Since we already established that I'm a n00b at everything (I dropped the hint in the Blog name btw) I set out on Google's Blue, Green, Yellow, and Red road to find the right assets for me and my current needs. My criteria was simplicity, reliability and speed. Simplicity because I want to release this app before I get out of High School, so while I would love to read and learn about new technologies it wouldn't fit the schedule. I wanted reliability because I didn't want to re write the whole app because I chose the wrong service; I would rather take my time choosing that waste my time switching services. And lastly I wanted Speed because frankly who likes to use slow, unresponsive apps?

       Still here? I'm surprised you care that much!...erhmm....right! backends...I happened to mention that I wanted push notifications. I basically want to avoid polling for anything. I'd Rather put a load on a server than on a user's phone. I couldn't use Android's C2DM because I would be leaving almost 30% of Android Users       without Push, so XMPP ( or Jabber for you old timers) seemed like the perfect match up for me. After all, C2DM is based off of XMPP! I just needed to copy it.

     I set out to make my own XMPP server and after numerous failed attempts I found this wonderful service called XTIFY. It was a blessing! Saved me a ton of work and I have not had a single dropped notification. Don't be fooled, while the site heavily pushes developers to use their pre-made Notification Activity you can send a "custom" notification with your own custom intent in it. Like so:

In XTIFY you can add your custom intent



And in your code add this:



Btw this is assuming you followed the implementation guide (which is fairly good!)

From this central Broadcast Receiver I can organize and send intents to other activities, write to databases and just treat this like a response from a server without having to deal with stupid http requests. That's going to come in the next part of this series ;)

Sunday, March 27, 2011

Another Reason for Admob Ad's to not load and the Virtues of the Logcat

The Admob library for Android can be a bit of a pain to get it to work sometimes. Today I learned a valuable lesson: Don't put padding on the Layout where the ad will go OR THE AD WON'T SHOW UP. Unless you know how to use the logcat for android, this will be a very hard problem to solve.

Since were on the topic of logcat  here's a helpful hint if you're using Eclipse. Instead of opening up a cmd (command) line to look at the logcat and typing in "adb devices" then "abd shell" then "logcat"just go to Eclipse

Window>Show View>Other> then look under the Android Tab and double click on the Logcat View And viola!

Wednesday, March 23, 2011

Easy Android App Project Hosting with Eclipse and Google Code

One of the things I love about Google is all the different kinds of services that they offer to users, and the fact that I only need to remember my Gmail login to access all those features. One service I find particularly useful is Google code. It allows me to develop on different work stations and also helps me and my coding friend(s) keep connected no matter where we are.

This tutorial expects Android and Eclipse to be installed on the system.

So if your interested comon' and follow me. I'll Show Ya!


1. Set up a project.
  • Go Here and make a new project. *Choose the version control as Mercurial* <-very important
2. Create a new Android App Project in Eclipse
3.Install the Mercurial Eclipse Plug-in

4.Make a local Repository
  • Right Click on the new Android App Project (1)
  • Team (2)
  • Share Project (3)

  • Click On Mercurial
  • Next
  • Click on Next until you can click on Finish
5. Synchronize with the Goggle Project you set up in the first step.
  • Go to the homepage of the Project. Click on Source
  • Keep this page open. Your gonna need to use the info here shortly.
  • Open up Eclipse
  • Right Click on the Android Project you made 
  • Team
  • Synchronize with
  • Next (1)
  • Now the URL is the one shown on the Checkout Tab of your Project Host (after hg clone) (2) 
  • The username is your Gmail (3)
  • The Password is shown when you click on the google.com password link (4 & 5)
  • Next until Finish
6. First Commit! (Almost There!)
  • Right click your project (1)
  • Team (2)
  • Commit (3)

  • Check Select All (1)
  • Put in "Project Start" for the Commit Message
  • Ok

7. PUUUUUUUUUUUSH! *Grunts* wait its not that hard!
  • Right Click on Project (1)
  • Team (2)
  • Push (3)
  • Finish (4)

8. TA DA!
  • Go to your Project Homepage
  • Click on source
  • Now you can either Browse through you project files or
  • See your Changes 

10. Wait a minute!

    You probably want to know how to have your work show up in other computer that you wanna work on (Which already have Android ,Eclipse ,and the Mercurial Eclipse Plug-in installed already) Easy!

(Try this on another computer if you want can to see some magic happen)
  • Go to Eclipse
  • File (1)
  • Import (2)
  • Clone Existing Mercurial Repository (Under the Mercurial Tab)
  • Next

  • Now just put in all the info that you put in when you Synchronized the Local Repository to the Google Project website.


Now you can keep your project synced between colleagues, or just between your different workstations. When you wanna save some work just Team -> Commit and when you wanna sync it with the Project Hosting just Team -> Push


PS: Write any suggestion in my comments or contact me on twitter: @tytdfn



Android: "Smart Folders"

I love clean code. One of the features android has that makes it easier to write clean code is a feature I nicknamed "Smart Folders". Take for example this scenario: You would like your application to have different layouts based on the screen orientation. Your first try might look something like this

public void onCreate(Bunde icicle)
{
       super.onCreate(icicle);
       int orientation = getResources().getConfiguration().orientation;
       if (orientation = Configuration.ORIENTATION_PORTRAIT)
       {
              setContentView(R.layout.main_port);
        } else if(orientation = Configuration.ORIENTATION_LANDSCAPE)
       {
             setContentView(R.layout.main_land);
       }
}

Then you stumble across this post and magically your code turns into this:

public void onCreate(Bunde icicle)
{
       super.onCreate(icicle);
       setContentView(R.layout.main);
}

You ask how is this possible? Simple: Android at runtime checks the configuration of the phone and looks in your /res folder for matching Resource Directory Qualifiers and uses the correct resources accordingly. WHAT THE HECK DOES THAT MEAN?   I hear you sayin'?

It means that if Android detects that the phone is in landscape mode it will check the /layout-land directory/folder for the main.xml . If it can't find a match then it defaults to checking the regular layout folder. Not only does this make your code easier to read but it will also make it more manageable down the road.

For a list of all the qualifiers android offers look Here (Scroll down a little to see the table) 
Some of the qualifiers include

  • Language and region: So you can Localize your strings
  • Screen size and/or Screen pixel density (dpi) : So your app looks good on any screen
  • Keyboard availability: So you can control when to show the soft keyboard
  • Primary non-touch navigation method: So you can support trackballs and trackpads!


TL;DR:
1. Put main_port.xml in the /layout  directory/folder
2. Put main_land.xml in the /layout-land directory/folder
3.Rename both xml's to main.xml

Piece of Cake! Next Post will be a tutorial on how to set up your app project with a new Google Code Project and how to easily sync your code to your project with the Mercurial Eclipse Plug-in. Please Stay tuned and pass this around.

PS: Write any suggestion in my comments or contact me on twitter: @tytdfn