Tushar
Tushar

Reputation: 261

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


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

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

Edited :

Full Screen is taken by that WebView , it is not residing inside 200px*200px .

Edited :

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:id="@+id/layout" >



</LinearLayout>

Upvotes: 0

Views: 528

Answers (3)

Abhijit Chakra
Abhijit Chakra

Reputation: 3234

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");

    }

Upvotes: 0

user
user

Reputation: 87074

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.

Upvotes: 1

Sanober Malik
Sanober Malik

Reputation: 2805

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) ;

Upvotes: 1

Related Questions