serdox
serdox

Reputation: 19

Simple webview sample code Android studio

I'm trying to make a simple webview app for this interactive map: http://gta5online.com/map-interactive > there is also a fullscreen link below the map.

Now I created an asset folder and included the "interactive" folder which has all the files, icons, map tiles and HTML document in it.

I want to load the HTML document from there into a activity as a webview. So its a local file. I want the app to handle it not the default browser.

Here is what I did by now:

I created a new project and added those codes into the activity_home.xml 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"

Then I added this code to enable Internet access in to manifest even if its a local HTML doc that I want to load (for later uses):

I also enabled JavaScript at the first code block as you can see.

Should I've put some code into the home.java file too?

I try this but it gives errors:

 package comapps.gta5online.gta5interactivemapcheats;

 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;

 public class Home extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_home);
  }
 }

public class ViewWeb extends Activity {
    @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.webview);
      WebView wv;
      WebView view=(WebView) this.findViewById(R.id.webView);
      view.getSettings() .setJavaScriptEnabled(true);
      view.loadUrl("file:///android_asset/interactive/map.html");  
   }
 }

In a YT tutorial I saw that he used something like this in the Java file:

  #in mainactivity.java
setContentView(R.layout.activity_main);
String url ="file:///android_asset/interactive/map.html";
WebView view=(WebView) this.findViewById(R.id.webView);
view.getSettings() .setJavaScriptEnabled(true);
view.loadUrl(url);

Upvotes: 2

Views: 2309

Answers (1)

Tasos
Tasos

Reputation: 5361

It looks like you are missing a few imports. Replace all the code you have with this

package comapps.gta5online.gta5interactivemapcheats;

 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.webkit.WebSettings;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;

 public class Home extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_home);

        WebView browser = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = browser.getSettings();
        webSettings.setJavaScriptEnabled(true);
        browser.loadUrl("file:///android_asset/interactive/map.html");

 }

        private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return false;
        }
    }

 }

Upvotes: 1

Related Questions