0

I have an android activity with a relative layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.transactions.detail.TransactionDetailActivity"
    android:background="@drawable/background_gradient"
    android:id="@+id/wrapperLayout" >

    <!--CODE HERE-->

</RelativeLayout>

Programmatically (in Kotlin) I add a WebView to the layout:

    val wv = WebView(this);
    wv.setInitialScale(1);
    wv.settings.javaScriptEnabled = true;
    wv.settings.loadWithOverviewMode = true;
    wv.settings.useWideViewPort = true;
    wv.setPadding(0, 0, 0, 0);
    wv.layout(0, 0, 0, 0);

    wv.setWebViewClient(WebViewClient(this));

    val relativeLayout = findViewById<RelativeLayout>(R.id.wrapperLayout);

    relativeLayout.addView(wv);

When I load some urls, it happens that the size of the webview changes.

enter image description here

The behaviour I want is this:

enter image description here

How can I fix the webview size to cover the screen?

Related answer: https://stackoverflow.com/a/9499811/1148281

Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
Admdebian
  • 630
  • 6
  • 17
  • what size do you want the `WebView` to have? Match parent or specific size? you can simply add `WebView` in the `xml` as a child to `RelativeLayout` – Atiq Jan 16 '20 at 14:15

1 Answers1

1

You should change LayoutParams to achieve this.

val wv = WebView(this);
wv.setInitialScale(1);
wv.settings.javaScriptEnabled = true;
wv.settings.loadWithOverviewMode = true;
wv.settings.useWideViewPort = true;
wv.setPadding(0, 0, 0, 0);
wv.layout(0, 0, 0, 0);

wv.setWebViewClient(WebViewClient(this));

val relativeLayout = findViewById<RelativeLayout>(R.id.wrapperLayout);

relativeLayout.addView(wv)

//Solution here
wv.layoutParams = 
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
        RelativeLayout.LayoutParams.MATCH_PARENT)
dcanbatman
  • 165
  • 1
  • 13