views:

363

answers:

3

Hi,

I am trying to use a WebView in my Android application. I am creating my webview in code-side (not in XML). My problem is; when I call loadUrl method of webview, the webview goes fullscreen mode. How can I keep the size of the webview for example 200x200 pixels?

If there is any other option instead of webview, of course welcome :)

Thanks,

A: 

See my reply and comments on http://stackoverflow.com/questions/3369196/how-to-show-a-webview-inside-an-activity-in-the-middle-of-screen/3369223#3369223

Apply LayoutParams to your WebView or to the Layout (i.e. LinearLayout) that's surrounding the WebView, if any, i.e. setLayoutParams(LinearLayout.LayoutParams(200, 200));

Mathias Lin
Thank you for your quick answer. But, I tried that. I set LayoutParams as;RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, 200);webView.setLayoutParams(params);This works if I do not call loadURL metdoh. When I call the method, webView goes fullscreen.
Kaan
That's weird. Can you post your code (edit/add to question), especially where you do all the settings and initialization of the WebView. Are you sure the webview you're seeing is your own in-app webview and not the default browser?
Mathias Lin
+2  A: 

Quite possibly what you are seeing is not your activity, but the Browser application, because the URL you linked to did a redirect. Use WebViewClient and shouldOverrideUrlLoading() to catch the redirect and send it back to your own WebView.

CommonsWare
That's most likely is the case (and the remedy)
DroidIn.net
A: 

Hi again,

@Mahtias Lin, please see my code to create and use WebView;

WebView webView = new WebView(this);

RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, 416);

WebSettings webSettings = webView.getSettings();
webSettings.setSavePassword(false);
webSettings.setSaveFormData(false);
webSettings.setJavaScriptEnabled(true);

webView.setLayoutParams(p);
webView.loadUrl("http://www.google.com/");

Above code is not set my webview to 416px height.

@CommonsWare, I tried your suggested with following code and amazingly it works, thanks.

webView.setWebViewClient(new WebViewClient() {  
   @Override  
   public boolean shouldOverrideUrlLoading(WebView view, String url)  
   {  
       view.loadUrl(url);
       return false;

    }  
 });  

But this usage brought some new problems. When I override shouldOverrideURLLoading, the webview displays with no-addressbar, no-navigation (back, forward etc...)?

And also, the webview doenst accept user inputs?

Ok, I am editing my question :)

I searched and I found the following additional set to make webview to able to get inputs;

webView.requestFocus(View.FOCUS_DOWN);

On the other hand, I guess I will create my own back, forward buttons and address-bar for my webview. I will also attach "what happened" to this thread when I create my buttons and address-bar.

Best,

Kaan