Android Webview Example
Android Webview Example
com/android-webview-example
Let's see the simple code to display HTML web page using web view. In this case, html file must
be located inside the asset directory.
activity_main.xml
1 of 4 11/17/2019, 2:27 PM
Android WebView Example - javatpoint https://www.javatpoint.com/android-webview-example
File: activity_main.xml
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
To add the web page (.html, .jsp) locally in application, they are required to place in the assets
folder. An assets folder is created as: right click on app -> New -> Folder -> Assets Folder ->main
or simply create an assets directory inside main directory.
Activity class
File: MainActivity.java
package example.javatpoint.com.webview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mywebview = (WebView) findViewById(R.id.webView);
2 of 4 11/17/2019, 2:27 PM
Android WebView Example - javatpoint https://www.javatpoint.com/android-webview-example
// mywebview.loadUrl("https://www.javatpoint.com/");
mywebview.loadUrl("file:///android_asset/myresource.html");
}
}
Output:
Let's see the output if you load the javatpoint.com web page.
3 of 4 11/17/2019, 2:27 PM
Android WebView Example - javatpoint https://www.javatpoint.com/android-webview-example
4 of 4 11/17/2019, 2:27 PM