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.
Related
I am using fragment with WebView. Webview opens up correctly. My intention is as follows: Visitor clicks on any of the links within WebView inside the Fragment. Clicked link opens up in new WebView within new Activity. Tricky part - all links are from the same web site, there are no external links. Tricky for me, at least. Many thanks for your help in advance! :)
Here is my WebView code from Fragment:
public class HomeFragment extends Fragment {
#SuppressLint("SetJavaScriptEnabled")
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_naslovna, container, false);
WebView webView = view.findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://mywebpage.com/");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
return view;
}
}
Please try this:
WebViewClient webClient = new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if( url.equals("http://test.com") ){
// do whatever you want e.g. open up activity with web view and pass the url
}
return true;
}
}
It took 6 more hours u puzzle work, but this is how it got fixed at the end.
public class HomeFragment extends Fragment {
WebView webView;
SwipeRefreshLayout swipeToRefreshLayout;
#SuppressLint("SetJavaScriptEnabled")
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_naslovna, container, false);
webView = view.findViewById(R.id.webView1);
final String pageUrl = "https://mywebpage.com/";
swipeToRefreshLayout = (SwipeRefreshLayout)view.findViewById(R.id.swiperefresh);
swipeToRefreshLayout.setOnRefreshListener(new RtvTkSwipeToRefreshListener(webView, swipeToRefreshLayout));
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != pageUrl) {
startNewActivity(url);
}
return true;
}
});
webView.loadUrl(pageUrl);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
return view;
}
private void startNewActivity(String url) {
Intent myIntent = new Intent(getActivity(), NewActivity.class);
myIntent.putExtra("url", url);
getActivity().startActivity(myIntent);
}
}
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
At my navigationDrawer from menu , by clicking search it opens a specific webview in a fragment . Works fine, but when I press back on that webview - instead of getting the last visited webscreen it comes back to my main fragment. how can i solve this? I'm providing my fragment java file here. please mention any other codes to need (if needed) to solve my problem
public class GoogleFragment extends Fragment {
public WebView mWebView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(fragment_google, container, false);
mWebView = (WebView) v.findViewById(webview);
mWebView.loadUrl("https://google.com");
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//improve webview performance
mWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setEnableSmoothTransition(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
return v;
}
}
I think when you press back button from fragment than we need to handle it inside of Activity,
You need to first Override onBackPressed() method in your main activity which having fragment and global assign Webview with public in your google fragment
NOTE : when you are adding or replacing fragment keep adding a TAG, it can be helpful for getting instance of fragment.
#Override
public void onBackPressed() {
Fragment fragment =
getFragmentManager().findFragmentByTag("google_fragment");
if (fragment instanceof GoogleFragment) {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}else{
super.onBackPressed();
}
}
public class GoogleFragment extends Fragment {
public WebView mWebView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(fragment_google, container, false);
mWebView = (WebView) v.findViewById(webview);
mWebView.loadUrl("https://google.com");
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//improve webview performance
mWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setEnableSmoothTransition(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
v.setFocusableInTouchMode(true);
v.requestFocus();
v.setOnKeyListener( new View.OnKeyListener()
{
#Override
public boolean onKey( View v1, int keyCode, KeyEvent event )
{
if( keyCode == KeyEvent.KEYCODE_BACK ) {
if(mWebView.canGoBack())
{
wView.goBack();
wView.getUrl();
}
return true;
}
return false;
}
} );
return v1;
}
}
Please try this code .it may help you.
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", "");
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.