My app needs to instantiate a WebView
that displays the content of a page of a website.
It works perfectly with cellphones (I've tried with various HTC's and with a recent Samsung too) but it doesn't with tablets (Galaxy Tab, or Asus Eee Pad Transformer) : instead of filling the screen the WebView
appears very small.
Depending of the tabs, roughly 25% to 50% of the viewport is filled, the rest is blank. It looks a bit as if I was using WRAP_CONTENT
instead of FILL_PARENT
.
The layout I'm using looks like this:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
The onCreate()
of the activity that uses this layout looks like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showdoc); // showdoc is the layout above
// Configuration of the WebView
final WebView webview = (WebView) findViewById(R.id.webview);
final WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
// Callbacks setup
final Context ctx = this;
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(ctx, getResources().getString(R.string.error_web) + description, Toast.LENGTH_LONG).show();
}
});
// Display of the page
webview.loadUrl(getResources().getString(R.string.doc_url) + Locale.getDefault().toString());
}
And the manifest file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
开发者_高级运维 android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:debuggable="true">
<activity android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
When I access to the same URL with the regular browser, the page does properly fill the screen.
The screenshot below were made using a Samsung Galaxy Tab 10 in. emulator, but the result is basically the same on a physical Eee Pad Transformer, the WebView
being 50% smaller. On a cellphone, no problem, the WebView
takes all the available room.
My app, in landscape mode:
My app, in portrait mode:
Same URL, same hardware, with the default browser, in landscape mode:
Same URL, same hardware, with the default browser, in portrait mode:
So it must be a matter of settings.
WHAT I HAVE UNSUCCESSFULLY TRIED SO FAR:
- Forcing the LayoutParams
I initially thought it was a LayoutParams
problem, so I tried adding what follows to my onCreate()
, but with no success. It just doesn't change anything:
// Zoom out
final Display display = getWindowManager().getDefaultDisplay();
final int width = display.getWidth();
final int height = display.getHeight();
webview.setLayoutParams(new LayoutParams(width, height));
- setScrollBarStyle()
I also looked at this discussion and then tried webview.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
, but with no success either.
- Using a LinearLayout
I have tried changing the Layout
as follows further to your feedback (see below), but it does not change anything:
<?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">
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Do you have any idea? Thanks in advance!
My guess is that it is running in compatibility mode for tablets. Check your manifest file. Could you post a screenshot of what you are seeing?
My suspicion was correct. It is running in compatibility mode.
<supports-screens android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:anyDensity="true" android:xlargeScreens="false" />
Put this piece of code inside the <manifest>
element, just before the <application>
tag in your manifest.
Try to put your webview in a Linearlayout and make both the height attributes of the Linearlayout and webview has android:layout_height="match_parent"
create a new instance of the webView and the KEY is to set the webview to the contentview setContentView(webView);
worked for me! hope it helps.
public class DisplayWebPage extends Activity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent(); // Get the message from the intent
String postString = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String url = ("http://api.myDomain.com/sbmRank/app/1/CSS-Slider-Menu.php?action=" + postString);
webView = new WebView(this);
setContentView(webView); //HERE IS THE KEY PART
WebSettings webSettings = webView.getSettings();
webSettings.setBuiltInZoomControls(true);
webView.setWebViewClient(new Callback());
webView.loadUrl(url);
}
private class Callback extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
}
}
精彩评论