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
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());
So I have a website with mobile version. I am trying to do an App for it.
This is my activity_main.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"
android:scrollbars="none"/>
and this is my MainActivity.java class
package com.example.esouqbh.esouq;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.example.esouqbh.esouq.R;
public class MainActivity extends Activity
{#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar as we already have it in the web app
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Point to the content view defined in XML
setContentView(R.layout.activity_main);
//Configure the webview setup in the xml layout
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
//Yes, we want javascript, pls.
webSettings.setJavaScriptEnabled(true);
//Make sure links in the webview is handled by the webview and not sent to a full browser
myWebView.setWebViewClient(new WebViewClient());
//And let the fun begin
myWebView.loadUrl("http://esouqbh.com"); }}
for some reason when I run the app it says on my phone (Im running the APP on my phone as an emulator)
I get this error
Web page not available
The web page at http://esouqbh.com could not be loaded as:
net::ERR_CACHE_MISS
note that my website functions perfectly with no problem if I open it from my computer browser or phone.
EDIT:: Found solution by adding
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
in androidmanifest.xml!
NO, it´s not a folder, by adding :
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
into you AndroidManifest.xml means that your application will load the page from the internet into your WebView
INTERNET permission: allows applications to open network sockets.
I want to get the content to be displayed in my android application as soon as it is updated on a particular website.
how can I do that? I'm new to android development....please help
sure use WebView - Android documentation here: http://developer.android.com/reference/android/webkit/WebView.html
You can also consider using PhoneGap and make a hybrid native html5 app.
Hi you can use WebView for this purpose please have alook as the example is given below
res/layout/main.xml –
<?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"
/>
the WebViewActivity.java is as follows
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
}
do not forget to have permission at manifest
<uses-permission android:name="android.permission.INTERNET" />
You can display the web content using these ways,
Through cross platforms, such as PhoneGap with jQuery Mobile or Sencha touch etc,.
Through Android WebView class.
If you try to display a web content using Android WebView class, refer this sample tutorial.
I have an Android webView which loads a remote web page. As the code of this page is controlled by a colleague I can ask him to change it but I would prefer to find a solotion totally on client side.
For reasons regarding traffic and performance we want to store our css and javascript files locally on the client and not load them from the server.
What I came up with are two ideas but none of them worked out so far.
Store all the files in the asset folder and have the html refer to them
Problem: The webview seems to be not allowed to access "file:///..." urls
Question: Is there a way to work around this issue?
Just ignore all those references in the html and just inject all those files after loading them in the webview
Question: Just how do I add those files (.css / .js) to my already loaded html?
You can build a local WebView in the following way
The Activity (LocalWebviewActivity.java)
The Layout (activity_localwebview.xml)
The Assets folder (in the root of "assets" folder, create the folder "css" and place "style.css" in here)
You refer to JS files the same way you refer to CSS StyleSheets
LocalWebviewActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class LocalWebviewActivity extends Activity {
WebView myWebView;
StringBuilder mySBcontent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_localwebview);
myWebView = (WebView) findViewById(R.id.webkit);
mySBcontent = new StringBuilder();
mySBcontent.append("<html>");
mySBcontent.append("<head>");
mySBcontent.append("<link type='text/css' rel='stylesheet' href='css/style.css'>");
mySBcontent.append("</head>");
mySBcontent.append("<body>");
mySBcontent.append("<h1>My Heading</h1>");
mySBcontent.append("<p>My HTML content</p>");
mySBcontent.append("<p><img style='width:150px;' src='myImg.png' /></p>");
mySBcontent.append("</body>");
mySBcontent.append("</html>");
myWebView.loadDataWithBaseURL("file:///android_asset/", mySBcontent.toString(), "text/html", "UTF-8", "");
}
}
activity_localwebview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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/webkit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
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 ?