Home » Android » java – Opening a webpage in an Android App

java – Opening a webpage in an Android App

Posted by: admin June 15, 2020 Leave a comment

Questions:

I was wondering what you guys/gals would recommend in terms of opening a webpage inside an app (i.e a smaller window with a webpage open in it but not a web browser) Im trying to integrate my webpage into my app more or less. Thanks 🙂

How to&Answers:

Have you tried using the WebView layout?

In your layout file:

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

Include the INTERNET permission into your manifest:

<uses-permission android:name="android.permission.INTERNET" />

And then in your Activity:

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.example.com");

This will embed a web-browser into your application (rather than open an external browser).

Answer:

You can place you view group and then place the webview inside and modify size as needed

<LinearLayout
    android:orientation="vertical"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
 <WebView
         android:id="@+id/webView"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">

 </WebView>
</LinearLayout>

In your manifest file give the permission

<uses-permission android:name="android.permission.INTERNET" />

then you get the object from code

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.google.com");