I added every thing to my app but it keeps opening the page that said page not found even if i add a URL for google or any other link.
I look at most of the solutions for the same question but none of them worked (allowing internet access, allowing local access, changing the link to android asset, ......)
Any way hers my code hope you can help:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url="http://www.google.com";
WebView home = (WebView) this.findViewById(R.id.homeweb);
home.loadUrl(url);
home.setWebChromeClient(new WebChromeClient());
home.clearCache(true);
home.getSettings().setAppCacheEnabled(false);
}
}
And here is the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.alex.myapplication.MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/homeweb" />
</RelativeLayout>
I know it's very simple in code but I'm still new to programming HTML in Android.
Just follow below code for showing html code in browser,
WebView view = new WebView(getActivity());
view.setVerticalScrollBarEnabled(false);
((LinearLayout)rootView.findViewById(R.id.text_about)).addView(view);
view.loadData(Html.fromHtml(put your url here), "text/html; charset=utf-8", "utf-8");
put html file in assets folder then write following code in activity
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("file:///android_asset/your_file_name.html");
remove these line from xml code
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
also remove code from activity
home.setWebChromeClient(new WebChromeClient());
home.clearCache(true);
home.getSettings().setAppCacheEnabled(false);
Programs run from top to bottom.So your problem is just the arrangement of your code.
In the onCreate method, use this code:
setContentView(R.layout.activity_main);
webView = (webView) findViewById(R.id.webView_id);
webSettings websettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
//After configuring all the required settings then you load your url
webView.loadurl("https://www.google.com");
webView.setWebViewClient(new webViewClient());
Related
I try to load youtube website using WebView in Android application. Need to load whole youtube site to WebView (not only one certain video url). It means user select video on youtube site and then play. But the playing of video is jammming. Is it wrong way using youtube inside application ?
I am using this code: webView.loadUrl(url);
pls Follow this code it will work for you
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
private WebView mWebview ;
private String youtubeUrl = "https://www.youtube.com/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebview = (WebView) findViewById(R.id.youtubeWebView);
mWebview.loadUrl(youtubeUrl);
mWebview.getSettings().setJavaScriptEnabled(true);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/youtubeWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.constraint.ConstraintLayout>
and add Internet Permission inside your Manifest file
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
it will looks like this in the below pic.
you can load any url into web view and add the internet permission inside your app's manifest file and use like below code --
yourWebView.loadUrl(youtubeUrl);
this works for me , i am sure it will work mate
i try 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"
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("file:///android_asset/interactive/map.html");
/>
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):
<uses-permission android:name="android.permission.INTERNET"/>
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?
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);
can someone please help a noob to achieve this by explaining what i did wrong and simple steps how to complete this app?. i'm sure its simple for you guys even if its that hard for me.
Be very careful in Android, you can never place Java code into XML code.
This code:
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
myWebView.loadUrl("file:///android_asset/interactive/map.html");
webSettings.setJavaScriptEnabled(true);
In your activity_home.xml file must be only this:
<?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"/>
This is due to same origin policy violation, you need to use loaddatawithurl
Read
http://developer.android.com/reference/android/webkit/WebView.html
and
https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy
Hi everyone i want to display a pdf in my activity using a webview so i did:
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.lasergroup.ami.utilities.PDFActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/wv_PDF"/>
</RelativeLayout>
JAVA
WebView wv = (WebView)findViewById(R.id.wv_PDF);
String pdfURL = "http://www.randomurl.com/randompdf.pdf"
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginState(WebSettings.PluginState.ON);
wv.loadUrl("http://docs.google.com/gview?embedded=true&url="+pdfURL);
This worked well on emulator but when i use it on my device (LG G3 Android 5.0 API 21) it opens the browser (Chrome) automatically after wv.loadUrl. This is a very strange issue, so i want to understand if it's a code problem or a device one, and i want to know if someone can tell me why this happens.
I haven't any intent to open the browser so i can't understand what happens (only on some devices).
You should add WebViewClient before call loadUrl
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
This question already has answers here:
Clicking URLs opens default browser
(6 answers)
Closed 8 years ago.
In my xml file I'm having text and below that text,I have placed a webview.
I am trying to open google's home page in Webview. Instead of opening in webview,webpage is opening on browser.What I want is web page should load in webview which is below some text.Below is my code:
<TextView
android:id="#+id/txt"
android:text="Hello Android"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="#003399"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
/>
<WebView
android:id="#+id/webview"
android:layout_marginTop="50dp"
android:layout_below="#id/txt"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
Kindly help to solve this.Thank you
This is because a combination of two things:
the WebViewClient is not set (set to null), this makes the WebView try and offer every navigation as an intent to the system. Since you have a browser installed the system will try to handle that navigation there.
going to google.com ususally results in a redirect, which is why the stuff about navigations in the previous point matters.
Try this:
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient());
Create your activity and than use this code...
mWebview = (WebView) findViewById(R.id.webview);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {
Toast.makeText(youractivity_name.this, description, Toast.LENGTH_SHORT).show();
}
});
mWebview.loadUrl("http://www.yahoo.com");
I am using webview control for displaying a flash .swf file in my application.
when i run the app screen displaying whole white scrren.
Here it is my code,
public class flash extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.main);
String url ="file:///android_asset/beautiful.swf";
WebView webview = (WebView) findViewById(R.id.flash_webview);
webview.getSettings().setPluginsEnabled(true);
webview.getSettings().setAllowFileAccess(true);
webview.loadUrl(url);
}
}
and the xml file contains webview control
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView
android:id="#+id/flash_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
and also i added the Android:hardwareAccelerated:"true"
in the manifest file.
i am still stuck with the white screen.
Please help.
Thanks in advance.
Have you tried embeding the .swf file in an html file and load this html ?