Wednesday, March 30, 2011

Beautiful Code: Pt. 1

***I'm starting this new series which will highlight code that I think is pretty/gorgeous/mind-blowing or all of the above. Generally it will be simple cases, and most of it will be Java only because of my limited knowledge of coding languages. Feel free to submit code to me (Using Pastie works best I believe) or to correct code highlighted in these segments.***

findPairs()

I'm making a dice game for android with a close friend of mine. The game is called Farkle and its highly addictive. A player needs to roll 6 die and choose the best combinations to score the most points. This method I just created will return the positions of other dice with the same value. The method came out very clean and I'm extremely proud of it. Even if it's extremely simple

public int[] findPairs(int index, int value) {

      int[] dirtyArray = { 0, 0, 0, 0, 0, 0 };
      int count = 0;

     for (int j = 0; j < dieValues.length; j++) {

            if (j != index && dieValues[j] == value) {
                  dirtyArray[count++] = j;
            }
     }

    if (count == -1)     return null;
    int[] cleanArray = new int[count];
    System.arraycopy(dirtyArray, 0, cleanArray, 0, count);
     return cleanArray;
}


Edit: Teehee made a mistake and just found it. Guess that's what happens when you post "Beautiful" code before testing. Note to self: variable++ returns the variable before it got incremented


Double Edit: And another one! It's perfect now (I promise)

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