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(new WebViewClient());

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 :

            WebView w =new WebView(this);
            LayoutParams lp = new LayoutParams(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_height match_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.

<LinearLayout xmlns: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

 public class MainActivity extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        loadView();

    }
    public void loadView(){
        View insertPoint = findViewById(R.id.layout);

        WebView web = new WebView(this) ;
        web.setWebViewClient(new WebViewClient());
        web.setBackgroundColor(Color.GRAY);
        int lHeight = 200 ;
        int lWidth = 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"