Skip to content Skip to sidebar Skip to footer

How To Restrict The Webview Inside In Android Program

i want to restrict webView's limit inside some pixels which is have declared at the time of webView initialization . View insertPoint = findViewById(R.id.layout); WebView web =

Solution 1:

Your code should work if you also add the line:

web.setWebViewClient(newWebViewClient());

Otherwise your code will start the browser(which is full screen). If you click BACK for example you should see your gray box.

Solution 2:

You can try this code for setting the height width :

WebVieww=newWebView(this);
            LayoutParamslp=newLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            w.setLayoutParams(lp);
            w.getLayoutParams().width = 200;
            w.getLayoutParams().height = 200;
            ( (ViewGroup) insertPoint ).addView(w) ;

Solution 3:

Yes your code is working check layout xml file weather you have declared your layout_width,layout_heightmatch_parent or fill_parent if so then change it to wrap_content it will work .

put this layout instead of your layout if it works then there is nothing wrong in your code otherwise u have problem in ur xml.

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/layout"   ></LinearLayout>

Activity class

publicclassMainActivityextendsActivity {


    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        loadView();

    }
    publicvoidloadView(){
        ViewinsertPoint= findViewById(R.id.layout);

        WebViewweb=newWebView(this) ;
        web.setWebViewClient(newWebViewClient());
        web.setBackgroundColor(Color.GRAY);
        intlHeight=200 ;
        intlWidth=200 ;


        ((ViewGroup) insertPoint) .addView(web, lWidth, lHeight) ;

        web.loadUrl("http://www.google.com");

    }

Post a Comment for "How To Restrict The Webview Inside In Android Program"