简体   繁体   中英

How can I open website in my android application?

I have responsive webpage. I want to have an android application, that open this webpage. So after i clicked on icon of application, it shows me menu and when i click on some button it shows me website inside this application. It is possible? If yes, how?

I tied only something like that:

    WebView webview = new WebView(this);
    setContentView(webview);

But it is not good for me... because it is only link to open it via browser... So finally I need something like mobile browser. I dont know if it is possible for me. Maybe you give me some more advices.

In your xml file add this code:

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

and In your activity:

private WebView webView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://www.google.com");

}

From your main activity, call startActivity so that Activity2 is started. For Activity2, call setContentView(R.layout.webview). In Activity2 onCreate() method, use WebView.loadURL(url).

You can configure settings via: WebView.getSettings().xxx();

xxx can be setBuiltInZoomControls(), SetJavaScriptEnabled, setDatabaseEnabled, or more.

Firstly create a fresh XML file like this ..

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

Now Create a new class for webView like this ...

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class ReviewView extends Activity {

    private WebView webView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web_view); // your xml file created above 

        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("https://play.google.com/store?utm_source=SEM&utm_campaign=Evergreen&pcampaignid=MKTADIN2771401PYBKWSTXHYSEM");

    }

} 

That's it .. you are good to go

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("www.yoursite.com");

   // you need to setWebViewClient for forcefully open in your webview 
    webview.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });

Let's sum up the answers:

-First, your application MUST have permission to access internet. You can do it by using this code block in your projects "AndroidManifest.xml" file:

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

Put this code block in manifest tag, not in application tag. Like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourproject
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="20" />

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

<application
    android:name="....

-Second, you must design a UI in xml which contains a WebView widget. Codes in other answers show you how to do it properly:

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

This is xml code of a webview inside your layout file. After you implement a webView to your layout.

-Last, follow code TeRRo gave, you can use it to show whatever page you want to load by changing web adress he wrote as example. Reposting it here:

private WebView webView;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);

webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM