Email Us: androidref@gmail.com
Useful Links:  | What is Android? | Android Home | Developer Home | Application Fundamentals | Google for Android |
    | Architecture Videos | Google API Add-On | IBM developerWorks | Android Developer Forum | Stack Overflow Forum |
What's Inside:
  • Using the Google Maps API to map a location and add a marker to the map
  • How to create an SD card, store a contact photo, and display the photo on the handset
  • How to use ContactsContract to retrieve phone numbers and email addresses
  • Manually creating a hyperlink
  • Configuring the emulator's browser to load "localhost" web pages
  • Calling Sprint services
  • Files that make up an Android application
  • Using intents to invoke Google applications
  • Connecting a handset to a PC and downloading an application
  • Managing portrait/landscape orientation changes
  • Connecting a handset to a PC and downloading an application ^Top

    There seem to be a lot discussions in the forums about problems connecting a physical handset to a PC, so an application can be downloaded for testing. The following appear to be the steps necessary to perform this kind of debugging. More information can be found at Developing on a Device too.

    First, in your application manifest file, define the application as debuggable:

    
        <application android:icon="@drawable/icon" android:label="@string/app_name"
                     android:debuggable="true">
                

    Next, on the phone itself:

    • Enable USB debugging in "Settings / USB / USB debuggging".
    • Enable USB debugging in "Settings / Applications / Development / USB debugging".
    • Enable the Unknown sources option in "Settings / Applications / Unknown sources".

    Finally, connect your phone to the PC via a USB cable. The "Found new hardware" wizard will start, and you should install the appropriate driver for your phone. Once installed, you see see the device installed in My Computer / Manage / Device Manager. You can also run adb devices from a Command Prompt, and you should see your phone listed under "List of devices attached".

    When all that's complete, rebuild your application, and then use adb to install it on the handset:

    
        C:/MY_APP/bin> adb install MY_APP.apk
                

    If everything installs successfully, you will now have your app running on the phone for testing.

    Alternatively, if you're an Eclipse user, and you set your Deployment Target Mode to "Manual":

    Then, when you run you application in Eclipse, you'll be presented with the Device Chooser dialog below, from which you can choose your phone:

    TIPWindows Search - When doing an Explorer Search in Windows, you can choose File / Save Search after you get your results. But unfortunately, this save the search criteria, not the search results! If you'd like to save the results of your search, there's a freeware utility called SysExporter that "allows you to grab the data stored in standard list-views, tree-views, list boxes, and combo boxes from almost any application running on your system, and export it to text, HTML or XML file". By using SysExporter with the search results pane, you can finally save your results. Download the utility here.
    Managing portrait/landscape orientation changes ^Top

    Android phones support both portrait and landscape orientations. To change the orientation on an actual phone, simply rotate the phone. If you're working with the emulator in Eclipse, Ctrl-F12 will toggle the orientation. If you decide that your application layout should be different for the two orientations, then there are a couple of things to do.

    First, create two layout definition files, one for portrait and one for landscape. They should be named the same e.g. main.xml. Put your portrait layout file in the /res/layout directory, and the landscape layout file in the /res/layout-land directory. (Note the -land extension.) Now, when you're in portait mode, the first layout will render, and when you're in landscape mode, the second layout will render.

    It's not initially obvious that when you change orientation, your Activity is actually recreated. So, you may need to manage transitions between orientations and persist/restore certain data values to keep the user experience seamless. (For more information on the entire lifecycle of an activity, see Activity Lifecycle.)

    To demonstrate orientation programming, assume a simple grid app that changes layout across orientations. In portrait mode, it's a 2x8 grid, with a counter at the bottom that will count each orienation change.

     

    In landscape mode, the grid is now 8x2, and the counter should be one more than the previous portrait value.

    We can use two different layout files, as described above, to handle the two views. But we'll need to manually manage the counter. First, define the counter as a TextView in main.xml, and give it an initial value of "1".

    
        <TextView android:id="@+id/counter" android:text="1"
                  android:textSize="10pt" android:textColor="#FFFF00"/>
                

    Without a lot of extra explanation, the following code shows the lifecyle methods for onSaveInstanceState(), onCreate(), and onRestoreInstanceState(), in the order that they're called. It also shows how to move the counter value across transitions by storing it, then retrieving it from the instanceState Bundle.

    
        //
        //  Step 1 - Get the current value of the counter, increment it,
        //  then save it to the instanceState bundle as an integer.
        //
        protected void onSaveInstanceState(Bundle instanceState)
        {
            super.onSaveInstanceState(instanceState);
            int counter = Integer.parseInt((((TextView)
                findViewById(R.id.counter)).getText()).toString());
            instanceState.putInt("counter", ++counter); // increment counter here
        }
    
        //
        //  Step 2 - Standard onCreate(). Re-establish the view, and
        //  Android will figure out which layout to use.
        //
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    
        //
        //  Step 3 - Pull the previously saved counter from the
        //  savedInstanceState and add it to the new TextView field.
        //
        protected void onRestoreInstanceState(Bundle savedInstanceState)
        {
            super.onRestoreInstanceState(savedInstanceState);
            int counter = savedInstanceState.getInt("counter");
            ((TextView) findViewById(R.id.counter)).setText(String.valueOf(counter));
        }
                

    One last method to consider is the onConfigurationChanged() method. This will be called last in the sequence above, but it first requires one addition to your activity definition in your manifest file. Add android:configChanges="orientation" to turn on orientation change notifications:

    
        <activity android:name=".Test" android:label="@string/app_name"
                  android:configChanges="orientation">
                

    Once that's done, you can override the method to do some work:

    
        //
        //  Step 4 - Called last, if android:configChanges="orientation" is defined.
        //
        public void onConfigurationChanged(Configuration newConfig)
        {
            super.onConfigurationChanged(newConfig);
            //
            //  Do your work here...
            //
        }