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
Related
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());
I am writing an Android App for a charity, and would like to be able to display the charity's facebook feed within the App. I don't want it to go through the phone's web browser, nor the facebook app.
I read on the developers.facebook.com how to add facebook's android sdk to the app's build gradle. But what I can't find is code for a simple android activity that will display the facebook timeline. I don't have time to dive deep into facebook's sdk to fiigure it out. And the last thing I want to do is reinvent the wheel.
Can anyone point me towards some code for a simple activity to display a facebook timeline? Any help will be greatly appreciated.
I'm not home so I can't test this right now but it should work.
In your layout file, put a webview like so:
<WebView
android:id="#+id/mWebView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Then in the onCreate method:
WebView mWebView = (WebView) findViewById(R.id.mWebView);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://LinkToFacebookPage.com/");
And finally, in your manifest file put the following permission before < Application >
<uses-permission android:name="android.permission.INTERNET" />
I have a problem with Android Web view.
The slider work fine with Apple but doesn't load on Android WebView.
Can anyone help? www.pgc-usa.net/ivynails
Thanks
I think I know. Web views in Android by default do not have JavaScript enabled. This is because there is someway it can do harm to the app. To enable it though it takes two lines
WebSettings webSettings = yourWebview.getSettings();
yourWebview.setJavaScriptEnabled(true);
You can also set it so that links that are clicked open in the web view. ( Otherwise they open in browser.) to add this you just add
yourWebview.setWebViewClient(new WebViewClient());
All that together will make it come to this:
WebView yourWebview = (WebView) findViewById(R.id.myWebview);
yourWebview.loadUrl("http://www.pgc-usa.net/ivynails");
WebSettings webSettings = yourWebviewgetSettings();
webSettings.setJavaScriptEnabled(true);
yourWebview.setWebViewClient(new WebViewClient());
You can find out more by visiting this page on the Android developer site by clicking this link.
I need to load url in webView - http://intor.club/files/comments.php?serial=56
I can not see in the webView that I see in the browser. Mobile browser works.
JavaScript is on. Help me! Thank you!
mWebView = (WebView) rootView.findViewById(R.id.webView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://intor.club/files/comments.php?serial=56");
First make sure you xml file you showing your webview like that
width fill parent and height also
<?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"
/>
don't forget your internet permission in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
it should work know
i test it myself
when i comment javascript enable i got this
//wbeview.getSettings().setJavaScriptEnabled(true);
when i enable java Script i get this (there are no comment in this thread)
i see you want access comment , may be you not authorized to access it.
Things you should try :-
1- try connect your facebook or any other connection to this site and access this link again .
2- try connect to main Page it will work "http://intor.club/ "
I use the following code to play the Video from remote server,
WebView webView = (WebView) findViewById(R.id.webview);
webView.loadUrl("http://commonsware.com/misc/test2.3gp");
its not playing the Video, while if i put the link in browser it will working fine.What mistake i made i don`t know.Even i also put internet access permission in manifest file.
Thanks.
Try this:
wv = (WebView)findViewById(R.id.webview);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginsEnabled(true);
wv.loadUrl("http://commonsware.com/misc/test2.3gp");