2

I am setting following property for webview

    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);

and using following layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/solid_white"
    android:layout_marginLeft="1dp" android:gravity="center">


     <WebView
        android:id="@+id/MyView"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
    </WebView> 

</RelativeLayout>

This layout is for fragment. View is coming centrally vertical but it not coming centrally horizontal. What I need to do to set webview at the center of relative layout?

Vivek
  • 4,170
  • 6
  • 36
  • 50
  • 1
    Your code is showing perfectly in Center. – Bhavin Mar 30 '12 at 07:14
  • @jeet It is wokring But I want whatever size of webview it should take that much any other solution any other option. – Vivek Mar 30 '12 at 07:27
  • Hey try this property in your webview. `android:scaleType="centerInside"` perhaps it will work for you. – RobinHood Mar 30 '12 at 07:33
  • @RobinHood there is no such property for webview or relative layout. – Vivek Mar 30 '12 at 08:30
  • Check this link..http://stackoverflow.com/questions/4142699/need-help-in-displaying-image-correctly-in-webview-in-android – RobinHood Mar 30 '12 at 10:38
  • @Vivek did u solve the above problem,, please let me know the solution. – Adi Nov 28 '14 at 02:39
  • @Adi Yes I have resolved the problem I will add answer shortly – Vivek Nov 29 '14 at 10:20
  • @Vivek Please add the solution as soon as possible as I'm stuck with it.. you can mail me too @ adithya.am1989@gmail.com . awaiting your response – Adi Dec 01 '14 at 06:17

3 Answers3

4

Just try this:

MyView.getSettings().setLoadWithOverviewMode(true);
MyView.getSettings().setUseWideViewPort(true);

I hope it works for you.

Chirag Shah
  • 2,058
  • 2
  • 20
  • 30
4

Actually Android WebView's wrap content layout paramter fill the parent width, and I believe its coz it is being optimized to use all the screen width, so you need to set some fixed width, to show in center

jeet
  • 29,001
  • 6
  • 52
  • 53
  • is this the same issue you are referring to? http://stackoverflow.com/questions/18655827/webview-orientation-change-issue/18656844?noredirect=1#18656844 – anshul Sep 09 '13 at 16:32
1

In Relative Layout attribute change like this :

android:gravity="center_vertical|center_horizontal"

No need to add android:layout_centerInParent="true" in WebView Hope it will work.

Android Killer
  • 18,174
  • 13
  • 67
  • 90
  • As there is only one view WebView inaide RelativeLayout so there may be problem.can you post the image please.what your are getting till now if you dont mind. – Android Killer Mar 30 '12 at 07:31