I am using a WebView to render a page from my website. The page contains audio and visual contents. I tried to render this link on my app but WebView shows blank data. If I use any URL of "YouTube" video or "Vimeo" then I video played successfully in app. I don't know why my website page is not rendered properly in WebView.
Video page rended but video not played and I am not able to below content of page also.
WebView Picture when Loads Page:
Original Page of Website
Code:
myWebView = (WebView) findViewById(R.id.webView);
mWebChromeClient = new MyWebChromeClient();
myWebView.setWebChromeClient(mWebChromeClient);
myWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("https://demourl.com/demo/index.html?cpath=embedded-player&ocode=frff&pcode=fff&signup=https%3A%2F%2Forder.dddd.com%2Fcheckout%2Frp%2Fmonthly%2F30d%3Fcpath%3Dembedded-player#/playlists/mp.45666");
}
public class MyWebChromeClient extends WebChromeClient {
FrameLayout.LayoutParams LayoutParameters = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mContentView = (RelativeLayout) findViewById(R.id.activity_main);
mContentView.setVisibility(View.GONE);
mCustomViewContainer = new FrameLayout(Demo.this);
mCustomViewContainer.setLayoutParams(LayoutParameters);
mCustomViewContainer.setBackgroundResource(android.R.color.black);
view.setLayoutParams(LayoutParameters);
mCustomViewContainer.addView(view);
mCustomView = view;
mCustomViewCallback = callback;
mCustomViewContainer.setVisibility(View.VISIBLE);
setContentView(mCustomViewContainer);
}
Please Help
Related
When I tried to load a website using webview, the page does load. However, the page doesn't load everything. I got php part and a Youtube vĂdeo but It didn't appear. This is my code:
public class ChatFragment extends Fragment {
public ChatFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_chat, container, false);
String url = "mywebpath/index.php"; // It has the http part
WebView webView = (WebView) view.findViewById(R.id.chat);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
if (Build.VERSION.SDK_INT >= 19) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
else {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
WebSettings webSettings = webView.getSettings();
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
return view;
}
This is the website: http://i.imgur.com/Tk8UIvL.jpg
And this is the webview result: http://i.imgur.com/reDPlx1.png
Thank so much for your attention
P.S. The chat starts with php part and then appear a div.
Oh ! You must create your website for version mobile
EG:
vsersion Browser: for Browser
version Mobile: for mobile
Try it!!!
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_chat, container, false);
String url = "mywebpath/index.php"; // It has the http part
WebView mWebView = (WebView) view.findViewById(R.id.chat);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().getPluginState();
if (Build.VERSION.SDK_INT < 8) {
mWebView.getSettings().setPluginsEnabled(true);
} else {
mWebView.getSettings().setPluginState(PluginState.ON);
}
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
mWebView.loadUrl(url);
return view;
}
in AndroidMainfest
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
if it does not work. Try this. (replace mWebview.loadUrl)
mWebview.loadData(url, "text/html", "utf-8");
Or
mWebview.loadDataWithBaseURL("", url, "text/html", "UTF-8", "");
I have created one wordpress client app, where I am also showing a video(In the webview loading embed code) on the top of the AppBarLayout and below its content like this Post view
How do I add a option to make it go fullscreen?
First, you need to create a custom WebChromeClient class, which implements the onShowCustomView and onHideCustomView methods.
private class MyWebChromeClient extends WebChromeClient {
FrameLayout.LayoutParams LayoutParameters = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mContentView = (RelativeLayout) findViewById(R.id.activity_main);
mContentView.setVisibility(View.GONE);
mCustomViewContainer = new FrameLayout(MainActivity.this);
mCustomViewContainer.setLayoutParams(LayoutParameters);
mCustomViewContainer.setBackgroundResource(android.R.color.black);
view.setLayoutParams(LayoutParameters);
mCustomViewContainer.addView(view);
mCustomView = view;
mCustomViewCallback = callback;
mCustomViewContainer.setVisibility(View.VISIBLE);
setContentView(mCustomViewContainer);
}
#Override
public void onHideCustomView() {
if (mCustomView == null) {
return;
} else {
// Hide the custom view.
mCustomView.setVisibility(View.GONE);
// Remove the custom view from its container.
mCustomViewContainer.removeView(mCustomView);
mCustomView = null;
mCustomViewContainer.setVisibility(View.GONE);
mCustomViewCallback.onCustomViewHidden();
// Show the content view.
mContentView.setVisibility(View.VISIBLE);
setContentView(mContentView);
}
}
}
Basically, what is happening here is when the full screen button gets pressed, we're creating a new view to hold the video and hiding the main view. And then when full screen is closed, we do the opposite - get rid of the new view and display the original view.
You'll need to also add all those properties to your activity class:
private MyWebChromeClient mWebChromeClient = null;
private View mCustomView;
private RelativeLayout mContentView;
private FrameLayout mCustomViewContainer;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
And you probably want to make it close the fullscreen video when the back button is pressed:
#Override
public void onBackPressed() {
if (mCustomViewContainer != null)
mWebChromeClient.onHideCustomView();
else if (myWebView.canGoBack())
myWebView.goBack();
else
super.onBackPressed();
}
Then it's just a matter of using your new class when you create your webview:
myWebView = (WebView) findViewById(R.id.webView1);
mWebChromeClient = new WMWebChromeClient();
myWebView.setWebChromeClient(mWebChromeClient);
I hope this works for you. :)
If you like this then do accept my answer.
I tried some ways to solve the speed problem but the site is loading very very slowly on webview even it opens in 3 seconds in Chrome. What I try is, adding these two lines on my code;
mWebview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
mWebview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
and adding
android:hardwareAccelerated="true"
to my Manifest file as I found these solutions there. But it's not working. So what is wrong ? Thanks in advice.
onCreateView part of my code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
mPbar = (ProgressBar) rootView.findViewById(R.id.web_view_progress);
mView = (FrameLayout) rootView.findViewById(R.id.web_view);
mView.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
mImageView = new ImageView(getActivity());
mImageView.setBackgroundColor(getResources().getColor(android.R.color.white));
mImageView.setImageResource(R.drawable.big_image_loading);
mImageView.setScaleType(ScaleType.CENTER_INSIDE);
mImageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
mImageView.post(new Runnable() {
#Override
public void run() {
((AnimationDrawable) mImageView.getDrawable()).start();
}
});
mView.addView(mImageView);
mWebview = new WebView(getActivity());
mWebview.setVisibility(View.GONE);
mWebview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
if(internetAccess()){
if (mUrl != null) {
mWebview.setWebChromeClient(new MyWebChromeClient());
mWebview.getSettings().setPluginState(PluginState.ON);
mWebview.getSettings().setUseWideViewPort(true);
mWebview.getSettings().setDefaultZoom(ZoomDensity.FAR);
mWebview.getSettings().setBuiltInZoomControls(true);
mWebview.getSettings().setSupportZoom(true);
mWebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebview.getSettings().setAllowFileAccess(true);
mWebview.getSettings().setDomStorageEnabled(true);
mWebview.getSettings().setJavaScriptEnabled(true);
mWebview.getSettings().setAppCacheEnabled(true);
mWebview.setWebViewClient(new MyWebViewClient());
if (Build.VERSION.SDK_INT >= 17)
mWebview.getSettings().setMediaPlaybackRequiresUserGesture(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
mWebview.getSettings().setDisplayZoomControls(false);
mWebview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
mWebview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebview.loadUrl(mUrl);
}
mView.addView(mWebview);
mBackBtn = (ImageButton) rootView.findViewById(R.id.web_view_btn_back);
mFowardBtn = (ImageButton) rootView.findViewById(R.id.web_view_btn_forward);
mRefreshBtn = (ImageButton) rootView.findViewById(R.id.web_view_btn_refresh);
mRefreshPbar = (ProgressBar) rootView.findViewById(R.id.loading);
// mShareBtn = (ImageButton) rootView.findViewById(R.id.web_view_btn_share);
mBackBtn.setOnClickListener(this);
mFowardBtn.setOnClickListener(this);
mRefreshBtn.setOnClickListener(this);
updateActionView();
return rootView;
}
}
OK, I solved the problem. Problem is not about WebSettings. view.setVisibility(View.VISIBLE); part is on my onPageFinished function and thats why site is loading so slow. I moved it to onPageStarted function and it works so fast.
Using Navigation Drawer calling webpage inside webview. But when moving through the menu its reloading whole website and again and again. I just want to save the state of webpage from where user left not from start of the webpage.
Thanks for any Help.
code:-
public class Facebook extends Fragment {
// this Fragment will be called from MainActivity
public Facebook(){}
private WebView webView ;
private Bundle webViewBundle;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.facebook, container, false);
webView = (WebView) rootView.findViewById(R.id.facebook);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setLoadWithOverviewMode(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setRenderPriority(RenderPriority.HIGH);
if (webViewBundle == null) {
try{
webView.loadUrl("http://www.facebook.com");
}catch (Exception e){
e.printStackTrace();}
} else {
webView.restoreState(webViewBundle);
}
return rootView;
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPause() {
super.onPause();
webViewBundle = new Bundle();
webView.saveState(webViewBundle);
}
}
Have you tried setting the Cache Mode to be LOAD_CACHE_ONLY after you loaded the website for the first time?
WebSettings webSettings = webView.getSettings();
webSettings.setCacheMode(LOAD_CACHE_ONLY);
Before getting frustrated or pulling your hair out add this next to viewpager.
ViewPager.setOffscreenPageLimit(3);// three is my number of fragments.
YouTube Video not playing in WebView.
It my first app ,I Want to do a webview.
When I open YouTube the video not playing, is loading but not playing.
Is loading all the time.
Thank you so much for helping.
Java:
public class MainActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
mWebView.setWebViewClient(new HelloWebViewClient());
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.loadUrl(url);
return true;
}}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
} }
Xml:
<?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"
/>
Seems like duplicate of play youtube video in WebView and YouTube Video not playing in WebView - Android
To make it work via WebView, I used the following two steps (version 4.2.2/Nexus 4):
On shouldOverrideUrlLoading I added webview.setWebChromeClient(new WebChromeClient());
In AndroidManifest.xml for activity tag I added android:hardwareAccelerated="true"
AndroidManifest.xml should also contain Internet permission.
While exact steps with playing video via WebView can be specific to device and Android version, there are also other alternatives to WebView, like VideoView or using Youtube Android Player API.
EDIT1
As for pause and full screen, this is actually a second link I mentioned in the beginning. To make it easier, I am posting code that worked on my Nexus.
In activity_main.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<FrameLayout
android:id="#+id/fullscreen_custom_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF000000"/>
<LinearLayout
android:id="#+id/linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</RelativeLayout>
In MainActivity class:
public class MainActivity extends Activity {
private WebView mWebView;
private LinearLayout mContentView;
private FrameLayout mCustomViewContainer;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContentView = (LinearLayout) findViewById(R.id.linearlayout);
mWebView = (WebView) findViewById(R.id.webView);
mCustomViewContainer = (FrameLayout) findViewById(R.id.fullscreen_custom_content);
WebSettings webSettings = mWebView.getSettings();
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
mWebView.loadUrl("http://www.google.com");
mWebView.setWebViewClient(new HelloWebViewClient());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.setWebChromeClient(new WebChromeClient() {
private View mCustomView;
#Override
public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
{
// if a view already exists then immediately terminate the new one
if (mCustomView != null)
{
callback.onCustomViewHidden();
return;
}
// Add the custom view to its container.
mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
mCustomView = view;
mCustomViewCallback = callback;
// hide main browser view
mContentView.setVisibility(View.GONE);
// Finally show the custom view container.
mCustomViewContainer.setVisibility(View.VISIBLE);
mCustomViewContainer.bringToFront();
}
});
webview.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
EDIT2 - adding back button support
public class MainActivity extends Activity {
private WebView mWebView;
private LinearLayout mContentView;
private FrameLayout mCustomViewContainer;
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
private WebChromeClient mWebChromeClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContentView = (LinearLayout) findViewById(R.id.linearlayout);
mWebView = (WebView) findViewById(R.id.webView);
mCustomViewContainer = (FrameLayout) findViewById(R.id.fullscreen_custom_content);
mWebChromeClient = new WebChromeClient() {
#Override
public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
{
// if a view already exists then immediately terminate the new one
if (mCustomView != null)
{
callback.onCustomViewHidden();
return;
}
// Add the custom view to its container.
mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
mCustomView = view;
mCustomViewCallback = callback;
// hide main browser view
mContentView.setVisibility(View.GONE);
// Finally show the custom view container.
mCustomViewContainer.setVisibility(View.VISIBLE);
mCustomViewContainer.bringToFront();
}
#Override
public void onHideCustomView()
{
if (mCustomView == null)
return;
// Hide the custom view.
mCustomView.setVisibility(View.GONE);
// Remove the custom view from its container.
mCustomViewContainer.removeView(mCustomView);
mCustomView = null;
mCustomViewContainer.setVisibility(View.GONE);
mCustomViewCallback.onCustomViewHidden();
// Show the content view.
mContentView.setVisibility(View.VISIBLE);
}
};
WebSettings webSettings = mWebView.getSettings();
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
mWebView.loadUrl("http://www.google.com");
mWebView.setWebViewClient(new HelloWebViewClient());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.setWebChromeClient(mWebChromeClient);
webview.loadUrl(url);
return true;
}
}
#Override
protected void onStop() {
super.onStop();
if (mCustomView != null)
{
if (mCustomViewCallback != null)
mCustomViewCallback.onCustomViewHidden();
mCustomView = null;
}
}
#Override
public void onBackPressed() {
super.onBackPressed();
if (mCustomView != null)
{
mWebChromeClient.onHideCustomView();
} else
{
finish();
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Adding webchrome client solves the issue,
web = (WebView) vi.findViewById(R.id.offer_webView1);
web.loadUrl(getResources().getString(R.string.videolink));
web.getSettings().setJavaScriptEnabled(true);
// web.getSettings().setDomStorageEnabled(true);
web.getSettings().setAllowContentAccess(true);
WebSettings webSettings = web.getSettings();
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
web.canGoBack();
web.setWebChromeClient(new WebChromeClient() {});