Tuesday, 27 December 2011

Backward compatibility using Android Support Package

Refer to the last exercise "Honeycomb's tab look of DatePicker", the app is created target API level 11. It can run on device with Android 3.0 or above only. If you want to make it run on early version, you can using Android Compatibility package (or named Android Support Package).

- Refer to the former article to Install and setup Compatibility Package.

- Modify AndroidManifest.xml to add minSdkVersion and targetSdkVersion.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidDatePicker"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="11"/>

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AndroidDatePickerActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


- Modify the main activity AndroidDatePickerActivity, to extends FragmentActivity. And import android.support.v4.app.FragmentActivity.
package com.exercise.AndroidDatePicker;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class AndroidDatePickerActivity extends FragmentActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}


- Keep no change on layout, main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<DatePicker
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>


- Now the app can run on device running Android 2.1 or above.

* Run on HTC Flyer running Android 3.2.1
Run on HTC Flyer running Android 3.2.1

* Run on Nexus One running Android 2.3.6
Run on Nexus One running Android 2.3.6

remark:
If you build the DatePicker exercise, target Android 2.2, API level 8. The app can run on both Android 2.3.6 and 3.2.1, and have traditional look.

* The app target API level8, run on HTC Flyer with Android 3.2.1
The app target API level8, run on HTC Flyer with Android 3.2.1

* The app target API level8, run on Nexus One with Android 2.3.6
The app target API level8, run on Nexus One with Android 2.3.6

No comments:

Post a Comment