Unable to implement progress-bar along with webview in fragment - android

I want show a ProgressBar for my WebView and it should stop once the loading is finished ,here is my code , but when i call ProgressBar it showing Cannot resolve method findViewById
package com.fb.jaisonjoseph.facebookbasic;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
/**
* A simple {#link Fragment} subclass.
*/
public class Home_Fragment extends Fragment {
public WebView mwebView;
public Home_Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_home_, null);
WebView view=(WebView) rootView.findViewById(R.id.webView);
view.loadUrl("https://mbasic.facebook.com");
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyWebViewClient());
return rootView;
}
private class MyWebViewClient extends WebViewClient {
ProgressBar bar=(ProgressBar)findViewById(R.id.progressBar);
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
#Override
public void onPageStarted(final WebView view, final String url, final Bitmap favicon) {
bar.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
bar.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
}
}
fragment_home_.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"
tools:context="com.fb.jaisonjoseph.facebookbasic.Home_Fragment">
<!-- TODO: Update blank fragment layout -->
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:animationResolution="#integer/abc_max_action_buttons"
android:clickable="false"
android:theme="#style/Base.Theme.AppCompat"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

1.) Your WebView and ProgressBar are inside Fragment layout and you are using inflater to initialize and return your fragment layout
2.) So every view of your fragment layout is inside the rootView returned by inflater and you have to use that rootView to initialize your other views which belong to your Fragment
3.) Use the mwebView variable instead of creating the local variable view
4.) shouldOverrideUrlLoading is doing nothing , so add view.load and return true
public class Home_Fragment extends Fragment {
public WebView mwebView;
ProgressBar bar;
// ^^^ declare bar
public Home_Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_home_, null);
bar = (ProgressBar) rootView.findViewById(R.id.progressBar);
// initialize bar
mwebView = (WebView) rootView.findViewById(R.id.webView);
mwebView.loadUrl("https://mbasic.facebook.com");
mwebView.getSettings().setJavaScriptEnabled(true);
mwebView.setWebViewClient(new MyWebViewClient());
return rootView;
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(final WebView view, final String url, final Bitmap favicon) {
bar.setVisibility(View.VISIBLE);
// ^^^ use it as it is
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
bar.setVisibility(View.GONE);
// ^^^ use it as it is
super.onPageFinished(view, url);
}
}
}

Related

Add loading spinner to webview Android app

I am trying to add a loading spinner to show while the webpage loads.
Here is my code:
package com.wEgyptianpost;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.TextView;
import java.util.Random;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.wEgyptianpost.R;
public class MainActivity extends Activity {
WebView view;
SwipeRefreshLayout mySwipeRefreshLayout;
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView adView = (AdView) findViewById(R.id.admob_id);
AdRequest adRequest = new AdRequest.Builder()
.setRequestAgent("android_studio:ad_template").build();
adView.loadAd(adRequest);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipelayout);
final WebView mWebView = (WebView) findViewById(R.id.activity_main_webview);
swipeRefreshLayout.setColorSchemeResources(R.color.refresh,R.color.refresh1,R.color.refresh2);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
mWebView.reload();
swipeRefreshLayout.setRefreshing(true);
(new Handler()).postDelayed(new Runnable() {
#Override
public void run() {
mWebView.stopLoading();
swipeRefreshLayout.setRefreshing(false);
}
},10000);
}
});
// Force links and redirects to open in the WebView instead of in a browser
//mWebView.setWebViewClient(new WebViewClient());
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
// Use remote resource
mWebView.loadUrl("http:google.com");
// Stop local links and redirects from opening in browser instead of WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
// Use local resource
//mWebView.loadUrl("file:android_asset/web/google.html");
}
// Prevent the back-button from closing the app
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is my activity xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipelayout"
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">
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout >
<com.google.android.gms.ads.AdView android:id="#+id/admob_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-1851250777225639/8300259410"
android:layout_gravity="center_horizontal|bottom" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar2"
android:layout_gravity="center" />
</FrameLayout>
I added progress bar to the activity xml but I dont know how to link it to the main activity code.
You can add it with setting a webclient
for
SET
mWebView.setWebViewClient(new WebViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(final WebView view, String url) {
progressBar.setVisibility(View.GONE);
}
} );
To avoid Ajax calls
Call this in activity
MyClient client=new MyClient();
mWebView.setWebViewClient(client);
client.setFirsttime();
and create a class in activity like this
public void MyClient extends WebViewClient(){
private boolean oneTime=false;
public void setFirsttime(){
oneTime=true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(oneTime){
progressBar.setVisibility(View.VISIBLE);
onetime=false;
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(final WebView view, String url) {
progressBar.setVisibility(View.GONE);
}
}
On Ajax call onPageStarted called multiple times hence progressBar will not get hidden.
Note: you have to call client.setFirsttime(); every time when you are using mWebView.loadUrl();
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
progressBar2.setProgress(newProgress);
//newProgress maybe is 1~100
super.onProgressChanged(view, newProgress);
}
you can try this,
public class MainActivity extends Activity {
WebView view;
SwipeRefreshLayout mySwipeRefreshLayout;
private ProgressBar progressBar;
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView adView = (AdView) findViewById(R.id.admob_id);
progressBar= (ProgressBar)findViewById(R.id.progressBar2);
progressBar.setVisibility(View.VISIBLE);
AdRequest adRequest = new AdRequest.Builder()
.setRequestAgent("android_studio:ad_template").build();
adView.loadAd(adRequest);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipelayout);
final WebView mWebView = (WebView) findViewById(R.id.activity_main_webview);
swipeRefreshLayout.setColorSchemeResources(R.color.refresh,R.color.refresh1,R.color.refresh2);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
mWebView.reload();
swipeRefreshLayout.setRefreshing(true);
(new Handler()).postDelayed(new Runnable() {
#Override
public void run() {
mWebView.stopLoading();
swipeRefreshLayout.setRefreshing(false);
}
},10000);
}
});
// Force links and redirects to open in the WebView instead of in a browser
//mWebView.setWebViewClient(new WebViewClient());
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
// Use remote resource
mWebView.loadUrl("http:google.com");
// Stop local links and redirects from opening in browser instead of WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
// Use local resource
//mWebView.loadUrl("file:android_asset/web/google.html");
progressBar.setVisibility(View.GONE);
}

Web page not loading on WebView

I'm a beginner in Android Development. So I'm searching online for tutorials to learn about.I got this code that I was trying on my computer, when I run the code and view the App, it says that the webpage is not available,so I added the manifest file, but it still says the same thing
The code is given below:
package com.paresh.webviewclientdemo;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewClientDemoActivity extends Activity {
WebView web;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
web = (WebView) findViewById(R.id.webview01);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("https://www.google.com");
}
public class myWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1" />
</LinearLayout>
Here's the manifest file:
<users-permission android:name="permission.INTERNET" />
But even after this,the Android web view webpage does not load, I cannot find the error. Help me find it. Do I need to add something else, or is there an error in the existing files that I have given
try these codes instead.
class:
public class WebViewClientDemoActivity extends Activity {
private WebView webView;
private String url = "https://www.google.com";
private ProgressBar progressBar;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new myWebClient());
webView.loadUrl(url);
}
public class myWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
progressBar.setVisibility(View.GONE);
}
}, 10000);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:indeterminate="false"/>
</RelativeLayout>

Webview in Fragment causes crash

Ok so I've been working on an app in Android Studio for school. I have a webview inside of a fragment. But in the app, when ever I open the fragment on my phone, the app crashes and my phone says "Unfortunately, LARKer App has stopped."
Here's the Fragment class:
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
/**
* Created by Andrew on 12/29/2014.
*/
public class menu4_Fragment extends Fragment {
private WebView mWebView;
View rootview;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.menu4_layout,container,false);
mWebView = (WebView) getView().findViewById(R.id.webView);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("foo");
mWebView.setWebViewClient(new MyWebViewClient());
return rootview;
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().contentEquals("foo")) {
// This is my website, so do not override; ler my WebView load the page
return false;
}
//Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
And here's the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="LARK: Login"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:textSize="50dp" />
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webView"
android:layout_below="#+id/textView"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Here's an image of what I got on my phone:
Change this code
mWebView = (WebView) getView().findViewById(R.id.webView);
Like this, it will work
mWebView = (WebView) rootview.findViewById(R.id.webView);
You have to get the view's from your View Group.otherwise it will return error.
change this on your oncreateview() methed of your fragmanet
mWebView = (WebView) rootview.findViewById(R.id.webView);
Try this code,
WebView webViewInfo = (WebView) findViewById(R.id.webview);
webViewInfo.getSettings().setJavaScriptEnabled(true);
webViewInfo.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webViewInfo.setWebViewClient(new MYWEBCLIENT());
webViewInfo.loadData("LOAD_YOUR_URL", "text/html", "UTF-8");
And WebViewClient class implement on webview method.
private class MYWEBCLIENT extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
On keyboard back handle,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webViewInfo.canGoBack()) {
webViewInfo.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
I hope this code help for you...!!

Android WebView Fragment GoBack();

Here's my webview drawer app..
It's making me crazy, i can't understand how to insert a "goBack" function like in normal WebView. Here's my code:
package com.my.app;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Retrieving the currently selected item number
int position = getArguments().getInt("position");
String url = getArguments().getString("url");
// List of rivers
String[] menus = getResources().getStringArray(R.array.menus);
// Creating view corresponding to the fragment
View v = inflater.inflate(R.layout.fragment_layout, container, false);
// Updating the action bar title
getActivity().getActionBar().setTitle(menus[position]);
//Initializing and loading url in webview
WebView webView = (WebView)v.findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient());
return v;
}
}
I've already checked more than 20+ answers here on Stack, but without results.
Hope someone can explain me a bit how to use the function GoBack and help me with this stressing thing..
Thanks in advance,
Davide
Inside of the your OnCreateView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.your_layout, container, false);
mWebView = (WebView) view.findViewById(R.id.webView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new MyWebViewClient());
mWebView.loadUrl(expertsUrl);
return view;
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Ok! In your MainActivity you will write it:
#Override
public void onBackPressed() {
if (WebViewFragment.mWebView!=null) {
if (WebViewFragment.mWebView.canGoBack()) {
WebViewFragment.mWebView.goBack();
}
}
}
WebViewFragment= This is your nameFragment that content the Webview
mWebView= The name of your WebView (id)
Now. Inside of your Fragment you have to declare static global variable
public static Webview mWebView;
And that's it! Good luck!
Since you are using a fragment, you have to override your activity onbackpressed() method and then test to see what fragment you are using. If you are using the fragment with the webview in it when the user tries to go back then see if the webview can go back. The code would look something like this.
#override
public void onBackPressed()
{
//get a reference to your webviewfragment before this line
//perhaps find the fragment by tag
if(webviewFragment.isVisible())
{
WebView mWebView = webviewFragment.getWebView();
if(mWebView.canGoBack())
{
mWebView.goBack();
return;
}
}
super.onBackPressed();
}
something like that, not exactly like it but close.

Splash screen while loading a url in a webview in android app

I've got an app, that has 2 activity, the first one launch the second to load a url into a webview.
It works, but while the url is loading , the webview appear empty... then i want to make a splash screen or something like this, to show it while the url is loading, I did that in a new activity, but i don't know what can i do to close the third activity when the url is loaded... Please can anybody help me?
This is my code...Thank you!
public class Visor extends Activity {
WebView mWebView;
int Result;
#Override
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.visor);
Bundle extras=getIntent().getExtras();
String s= extras.getString("url");
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.loadUrl(s);
mWebView.setWebViewClient(new VisorClient());
mWebView.getSettings().setBuiltInZoomControls(true);
}
private class VisorClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
lanzarIntro();
}
#Override
public void onPageFinished(WebView view, String url) {
mWebView.loadUrl(url);
}
}
public void lanzarIntro(){
Intent i=new Intent (this, Intro.class);
startActivity(i);
}
}
I do it by initially showing an ImageView and then once the WebView has loaded, swapping their visibility like this
WebView wv = (WebView) findViewById(R.id.webView1);
wv.getSettings().setJavaScriptEnabled(true);
wv.setWebViewClient(new WebViewClient() {
...
#Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.webView1).setVisibility(View.VISIBLE);
}
});
wv.loadUrl("http://yoururlhere.com");
And my xml layout looks like this
<ImageView android:id="#+id/imageLoading1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:visibility="visible"
android:src="#drawable/vert_loading"
/>
<WebView android:id="#+id/webView1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:visibility="gone"
/>
I have one activity. 1 xml file and 1 java class. Inside xml file i have:
WebView
ImageView, logo of my application,
ProgressBar and
TextView, app version .
Code of main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="fill_parent"
a:layout_height="fill_parent"
a:background="#aaaaaa"
a:orientation="vertical" >
<WebView
a:id="#+id/webView1"
a:layout_width="fill_parent"
a:layout_height="fill_parent" />
<ImageView
a:id="#+id/imageView1"
a:layout_width="wrap_content"
a:layout_height="wrap_content"
a:layout_alignParentTop="true"
a:layout_centerHorizontal="true"
a:layout_marginTop="46dp"
a:src="#drawable/logo" />
<ProgressBar
a:id="#+id/progressBar1"
a:layout_width="wrap_content"
a:layout_height="wrap_content"
a:layout_below="#+id/imageView1"
a:layout_centerHorizontal="true" />
<TextView
a:id="#+id/textView1"
a:layout_width="wrap_content"
a:layout_height="wrap_content"
a:layout_alignParentBottom="true"
a:layout_alignParentRight="true"
a:layout_marginBottom="13dp"
a:layout_marginRight="13dp"
a:text="version 1.0"
a:textAppearance="?android:attr/textAppearanceSmall"
a:textColor="#444444" />
</RelativeLayout>
Code of NovcanikActivity.java:
package zm.Nocanik;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class NovcanikActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webview = (WebView) findViewById(R.id.webView1);
WebSettings websettings = webview.getSettings();
websettings.setJavaScriptEnabled(true);
websettings.setSaveFormData(false);
websettings.setSavePassword(false);
webview.loadUrl("http://m.novcanik.net/?appvers=1.0");
webview.setHorizontalScrollBarEnabled(false);
webview.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
webview.setBackgroundColor(128);
webview.setWebViewClient(new NovcanikWebViewClient());
webview.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
}
public void visible(){
WebView webview = (WebView) findViewById(R.id.webView1);
ImageView logo = (ImageView) findViewById(R.id.imageView1);
ProgressBar bar = (ProgressBar) findViewById(R.id.progressBar1);
TextView version = (TextView) findViewById(R.id.textView1);
webview.setVisibility(10);
logo.setVisibility(0);
bar.setVisibility(0);
version.setVisibility(0);
}
public void unvisible(){
WebView webview = (WebView) findViewById(R.id.webView1);
ImageView logo = (ImageView) findViewById(R.id.imageView1);
ProgressBar bar = (ProgressBar) findViewById(R.id.progressBar1);
TextView version = (TextView) findViewById(R.id.textView1);
webview.setVisibility(0);
logo.setVisibility(10);
bar.setVisibility(10);
version.setVisibility(10);
}
private class NovcanikWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url){
webview.loadUrl(url);
return true;
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// TODO Auto-generated method stub
view.loadUrl("file:///android_asset/noconnection.html");
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
visible();
}
#Override
public void onPageFinished(WebView view, String url) {
unvisible();
}
}
}
Sorry for no description. If there would be need for description, i will describe in detail the entire code.
Just use mWebView.setBackgroundColor(getResources().getColor(R.color.colorPrimary)); for your mWebView

Categories

Resources