I was porting an iPad app to Android for the new Samsung tablet, and I had some trouble getting my web view to scale to the size of the device screen so that I wouldn't have to resize any of my assets. Obviously this is a questionable tactic, but I was experimenting and wanted to see how it would look :)
Here's the meat of my main App.java class:
public class App extends DroidGap {
// declare the original size of the iPad app
protected float ORIG_APP_W = 768;
protected float ORIG_APP_H = 1004;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
// set some defaults
this.appView.setBackgroundColor(0x000000);
this.appView.setHorizontalScrollBarEnabled(false);
this.appView.setHorizontalScrollbarOverlay(false);
this.appView.setVerticalScrollBarEnabled(false);
this.appView.setVerticalScrollbarOverlay(false);
// get actual screen size
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
// calculate target scale (only dealing with portrait orientation)
double globalScale = Math.ceil( ( width / ORIG_APP_W ) * 100 );
// make sure we're all good
Log.v( "ORIG_APP_W", " = " + ORIG_APP_W );
Log.v( "ORIG_APP_H", " = " + ORIG_APP_H );
Log.v( "width", " = " + width );
Log.v( "this.appView.getMeasuredHeight()", " = " + height );
Log.v( "globalScale", " = " + globalScale );
Log.v( "this.appView.getScale()", "index=" + this.appView.getScale() );
// set some defaults on the web view
this.appView.getSettings().setBuiltInZoomControls( false );
this.appView.getSettings().setSupportZoom( false );
this.appView.getSettings().setGeolocationEnabled( true );
this.appView.getSettings().setLightTouchEnabled( true );
this.appView.getSettings().setRenderPriority( RenderPriority.HIGH );
// set the scale
this.appView.setInitialScale( (int)globalScale );
}
}
I also updated the AndroidManifest.xml file to lock the app into portrait orientation, work on tablet-sized devices, have a nice app name, and give the device access to the Internet and geolocation:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.phonegap.testapp"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:debuggable="true">
<activity android:name=".App"
android:label="Test App"
android:configChanges="orientation|keyboardHidden"
android:noHistory="true"
android:stateNotNeeded="true"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!-- allows access to phonegap hardware features -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<supports-screens
android:largeScreens="true"
android:normalScreens="false"
android:smallScreens="false"
android:resizeable="true"
android:anyDensity="true"
/>
</manifest>
And finally my res/layout/main.xml file, though I'm not sure if this is different from the Phonegap default:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView android:id="@+id/appView"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
</LinearLayout>
I hope this helps someone port their hybrid html5 iPad app to Android tablets.