2

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);
serdox
  • 19
  • 10

1 Answers1

1

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;
        }
    }

 }
Tasos
  • 5,321
  • 1
  • 15
  • 26
  • ok, i successed running it on a virtual nexus 5 device in the emulator myself. the code i have is much shorter though and i dont know if its all conform. how can i post code here. am i allowed to put it here since it wont show it as marked as code. – serdox Nov 02 '15 at 17:05
  • i will try with your code too. is there a way that the application user agent is used and not the default browser? thanks by the way – serdox Nov 02 '15 at 17:07
  • @serdox -- for user agent settings have a look here on how its done -- http://stackoverflow.com/questions/7327160/android-mobile-user-agent -- http://www.useragentstring.com/pages/Android%20Webkit%20Browser/ – Tasos Nov 02 '15 at 17:10
  • ok my code was this. it worked: 'package comapps.gta5online.gta5interactivemapcheats; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebView;' ' – serdox Nov 02 '15 at 17:16
  • 'public class Home extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); String url ="file:///android_asset/interactive/map.html"; WebView view=(WebView) this.findViewById(R.id.webview); view.getSettings() .setJavaScriptEnabled(true); view.loadUrl(url); } } – serdox Nov 02 '15 at 17:16
  • what does your code do in addition to it, i try just to learn. – serdox Nov 02 '15 at 17:17
  • @serdox -- its ok to edit your question and add that code in as an Update. My code is just more organized, but there are many ways to write a webview – Tasos Nov 02 '15 at 17:17
  • you also imported webkit and webclient my code didnt have this – serdox Nov 02 '15 at 17:19
  • hey i really appreciate this @Tasos – serdox Nov 02 '15 at 17:20
  • @serdox -- no probs you can click the up arrow to give a point for the help -- take a look here as to what WebClient does -- http://developer.android.com/reference/android/webkit/WebViewClient.html – Tasos Nov 02 '15 at 17:23
  • i did already :) but it wont show up because i have not the min. req. rep. – serdox Nov 02 '15 at 17:26
  • @serdox -- so basically (shouldOverrideUrlLoading) is used for other urls if you have any such as links -- return false; -- use the webview -- return true; -- use the native browser on the mobile device. otherwise according to the doc you will get a popup window -- http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String) – Tasos Nov 02 '15 at 17:37
  • yep. hey i found a error i've made that you also took over in your code because of me. the WebView browser = (WebView) findViewById(R.id.webView); it should be R.id.webview at the end i guess. android studio says its wrong and it works when i write it all smallcaps as webview – serdox Nov 02 '15 at 17:37
  • @serdox -- That was a typo -- the id name is the id you given here (android:id="@+id/webview") -- so yes it should be (findViewById(R.id.webview); ) -- so if you had (android:id="@+id/jerry") then it should be (findViewById(R.id.jerry); ) – Tasos Nov 02 '15 at 17:39
  • thanks so much man. great help.made my day. my app is running hahaaa – serdox Nov 02 '15 at 17:43
  • i will post the link here for a screen capture. – serdox Nov 02 '15 at 17:44
  • @serdox -- that's great man. well if you ever find WebView lacks a bit of juice and Power you can always embed a Replacement Browser called Crosswalk which is based on Google Chrome. Its fast, but because you are Embedding it it also adds 19 extra megs to the App. -- https://crosswalk-project.org/ – Tasos Nov 02 '15 at 17:48
  • i will bookmark it for later use. the emulator is just so slowly loading. i gave it 1 gb memory still. – serdox Nov 02 '15 at 17:53
  • hey @Tasos the video came out bright but you get the idea: https://youtu.be/qUNEpZbfRDM – serdox Nov 02 '15 at 18:05
  • @serdox - that looks great what you done with the map, well done – Tasos Nov 02 '15 at 18:11
  • thanks @tasos. now i only need to find out how to add two more scrollable tabs, one with icons and descriptions and one with the cheats table. :D then there would be a adsense/admob banner displayed on the side or below. that would be everything hehe. let's see. – serdox Nov 02 '15 at 18:19
  • @serdox -- You need tabs and fragments -- a tutorial here https://www.youtube.com/watch?v=JJeeZHFgLfc – Tasos Nov 02 '15 at 18:38