Search This Blog

Sunday 29 April 2012

Debugging and Testing the Application

I tested my app using the Android Emulator which is integrated with Eclipse (We previously discussed the Emulator in the post entitled "Writing your first Android program").

Having tested the app on the Emulator, I decided to test the app on my own phone.  I have found a quick and convenient way to get the app on to my phone using Dropbox.  I simply drop the app into the Dropbox folder on my PC, this is synchronised with the Dropbox app on my phone.  To learn more about Dropbox simply Google Dropbox.

Once I got the app onto my Phone, the first and most major bug I found was that my app worked fine provided the user did not rotate the phone from portrait to landscape.  You would be right in wondering why I did not spot this major flaw when testing with the emulator.  At the time I wrongly believed that the Emulator was limited on functionality, I now know that this is not the case.  You can rotate the Emulator screen by pressing Ctrl + F12.

So having found this major bug in my code, how did I fix it.  Well my good friend Google came to the rescue as usual.  A quick search of the internet revealed that unless you handle config changes then the Activity is restarted and the onCreate( ) function in the activity is called.

In order to handle config changes you need to tell the android system that you wish to handle configuration changes.  This is done in the manifest file.  I have used bold text in the extract below to show the change I had to make to the manifest file for my Maths Test App.


        <activity android:name=".TestActivity"
         android:configChanges="keyboardHidden|orientation"
                  android:label="@string/app_name">  
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

This change tells the android system that I will handle configuration changes in my code, so don't call onCreate( ) when the configuration changes, instead call

public void onConfigurationChanged(Configuration newConfig)

which I have implemented in my code.  In this function I load a different layout depending on the orientation of the phone and the size of the screen. I have obviously had to create multiple layouts (User Interfaces implemented using xml - see previous posts to learn more about layouts.).  The code extract below which I have simplified to make my point shows how to check the phone orientation and the size of the users screen.  Checking screen size requires you to do a Bitwise And - essentially I am checking to see if the small screen bit is set.

if((newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)&& ((newConfig.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) != Configuration.SCREENLAYOUT_SIZE_SMALL ))

        {
       
        setContentView(R.layout.main_smallscreen);
       
        }
        else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
        {
       
        setContentView(R.layout.main);    
        }

This has probably been fairly heavy going, so a quick summary of what has been learnt may well be useful.


  1. How to debug using the Emulator - Ctrl + F12 rotates the emulator screen
  2. How to get an app onto your phone using Dropbox
  3. How to handle configuration changes - so that your app does not restart when you rotate from portrait to landscape.
  4. How to handle different screen sizes.
I will leave you to digest the above, any questions please get in touch.







No comments:

Post a Comment