import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;
import static com.example.tencho.cst.R.id.*;
public class MainActivity extends AppCompatActivity {
private WebView MywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MywebView =(WebView)this.findViewById(WebView);
WebSettings webSettings=MywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
MywebView.loadUrl("www.cst.edu.bt");
}
}
This is the MainActivity.java. it's always giving me an error cannot find symbol variable WebView.
I did read the past similar post but I couldn't solve it error. Please need help!
At this line
MywebView =(WebView)this.findViewById(id);
id should be like R.id.webview1
webview1 is the id in your XML layout
The error is on this line:
MywebView =(WebView)this.findViewById(WebView);
The cause of this error is that the generated R.id class has no field named WebView (the argument you are passing to the findViewById() method). This is probably happening because your layout's <WebView> tag is missing an id attribute. Add this line to your <WebView> tag:
android:id="#+id/WebView"
Ben's answer is correct. In addition, if your new to Android, if you added this part in your tag which can be found in your activity_main.xml layout..
android:id="#+id/webview"
then in your MainActivity.java, it should be like this:
MywebView =(WebView) findViewById(R.id.webview);
Related
I am loading a webView in my app that displays some buttons, each button starts a route that is generated using google maps.
The problem is that when I click "Navigate in app" nothing happens.
I was having the URL_UNKOWN_SCHEME error but solved it by adding and #override
My code looks like this:
package com.example.listarehtgps2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView web;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = findViewById(R.id.webview);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
web.loadUrl("webViewURL replaced for privacy reasons");
web.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// When user clicks a hyperlink, load in the existing WebView
if(url.contains("intent:")) {
Uri gmmIntentUri = Uri.parse(url);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
startActivity(mapIntent);
return true;
}
view.loadUrl(url);
return true;
}
}
);
}
}
I can not find any solution to open the link in the google maps app.
I managed to solve the UNKWN_URL_SCHEME but this is frustrating now.
Thanks in advance!
The URL_UNKOWN_SCHEME is because it's not recognising the scheme part of your URI (the bit up to :), are your URIs simply starting with "intent:" ?
To open a map at a location, try switching to the geo scheme, so a URI something like "geo:0.01,0.01?q=London" (along ofc with changing your url.contains check to look for that). You could also insert a mapIntent.setPackage("com.google.android.apps.maps") before the call to startActivity(mapIntent) if you want Google maps to take priority over any other map application the user may have installed.
If however you want navigation, use a URI starting with google.navigation such as "google.navigation:London" or "google.navigation:0.01,0.01", equally for street view you can use a URI starting with google.streetview
I already tried referring both to this StackOverFlow question as well as this tutorial but still have not found a solution to my problem.
I set permissions already in the Android Manifest XML file, and have my webview problem under the FullscreenActivity file. My code is here:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
WebView view = (WebView) this.findViewById(R.id.webView);
view.setWebViewClient(new WebViewClient());
view.loadUrl("platinumskincare.com");
view.getSettings().setJavaScriptEnabled(true);
mVisible = true;
mControlsView = findViewById(R.id.fullscreen_content_controls);
mContentView = findViewById(R.id.fullscreen_content);
The error I keep getting says 'Cannot Resolve Symbol 'WebViewClient', and I believe it is on line 44 of my code. What should I do and how do I fix this problem?
Check if you have these imported
import android.webkit.WebView;
import android.webkit.WebViewClient;
Clean and rebuild your project in Android Studio.
I don't know how to access my index.html webpage when the app is loaded. Actually on my code I use file:///android_asset/index.html, I've tried to load a Google page to test, it works, but it launch the Chrome app...
Here is my code of my MainActivity.java :
package com.totominc.fr.webapp;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_asset/index.html"); // error may be here...
}
}
But the error which I got is that it can't find the file index.html.
Here is a screenshot of my project.
Thanks for helping me!
try this
file:///Android_res/drawable/index.html
As your index html is in drawable folder. this would work
You need to put your index.html file into your assets folder, and not /res/drawable.
So your index.html should be in /assets/index.html.
Create your assets folder in src/main/assets/ and put your index.html there, not inside your drawables. Regarding the page opening in Chrome, try this instead
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
// Changed here
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("file:///android_asset/index.html"); // error may be here...
}
I'm trying to port a Flash based game to Android.
I've ported it successfully (WebView), but now, the problem is that when I do a short click (short tap) somewhere in the game, it does 2 clicks (double tap), and when I do a long click (long tap) it does a short click (short tap).This is really annoying, specially when it does double fire instead of one fire. I've tried with the touch screen and a mouse; both had the same issue, so the input isn't the problem.
After reading a lot of stuff I think it must be a bug(?) on Andriod (4.x+ I think) WebView.
I hope you can help me to invert those things or to prevent those double taps as soon as possible.
Here is the code of the MainActivity.java:
package usr.sh.raul.dofus;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
#SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity
{
WebView mWebView;
/** Called when the activity is first created. */
#SuppressLint("SdCardPath")
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setBackgroundColor(Color.parseColor("#000000"));
mWebView.loadUrl("file:///sdcard/Android/data/usr.sh.raul.dofus/data/loader.swf");
}
}
Thank you in advance and sorry for my bad english.
This is my code:
package sai.datla.game;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class GametestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.clearSslPreferences();
webView.loadUrl("file:///android_asset/www/index.html");
}
}
but it is saying that it cannot find the link in the emulator. I have checked many times if my index.html file is in the www folder, which is in my assets, but it won't work. Please help.
By the way I am only twelve years old so please make the answers easy to understand for a child.
// Find view in layout
WebView wv = (WebView) findViewById(R.id.webView_tobe_loaded);
// Get settings
WebSettings wbset = wv.getSettings();
// Enable JavaScript
wbset.setJavaScriptEnabled(true);
// Set new client (to handle website in your app)
wv.setWebViewClient(new MyWebViewClient());
// Example of the URL
String url = "http://www.google.com";
// Load website
wv.loadUrl(url);
This code will help you to solve the problem.
It worked for me.
instead of loadUrl, try using the loadDataWithBaseURL method:
webview.loadDataWithBaseURL("android.resource://<package_name>/assets/www/file_name", html, mimeType, encoding, "");