Last active
April 2, 2018 15:10
-
-
Save alitamoor65/7661a4dfd01e147496c5f59da28e3386 to your computer and use it in GitHub Desktop.
Load Web View with progress bar in activity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<FrameLayout 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="5dp" | |
android:paddingLeft="5dp" | |
android:paddingRight="5dp" | |
android:paddingTop="5dp" | |
android:orientation="vertical" | |
android:gravity="center_horizontal|center_vertical" | |
tools:context="com.jovial.livematchscores.MainActivity"> | |
<WebView | |
android:id="@+id/webView" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" /> | |
<LinearLayout | |
android:id="@+id/layoutProgress" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:orientation="vertical" | |
android:gravity="center_horizontal|center_vertical" > | |
<TextView | |
android:id="@+id/textLoading" | |
android:text="@string/label_loading" | |
android:textSize="20sp" | |
android:textStyle="bold" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" /> | |
<View | |
android:layout_width="fill_parent" | |
android:layout_height="20dp" /> | |
<ProgressBar | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/progressBar" | |
style="?android:attr/progressBarStyleLarge" | |
android:layout_gravity="center_vertical" /> | |
</LinearLayout> | |
</FrameLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jovial.livematchscores; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.webkit.WebSettings; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
import android.widget.LinearLayout; | |
import android.widget.ProgressBar; | |
import android.widget.Toast; | |
public class MainActivity extends AppCompatActivity { | |
private WebView webView; | |
private ProgressBar progressBar; | |
private LinearLayout layoutProgress; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
webView = (WebView) findViewById(R.id.webView); | |
progressBar = (ProgressBar) findViewById(R.id.progressBar); | |
layoutProgress = (LinearLayout) findViewById(R.id.layoutProgress); | |
webView.setVisibility(View.GONE); | |
WebSettings settings = webView.getSettings(); | |
settings.setJavaScriptEnabled(true); | |
settings.setBuiltInZoomControls(true); | |
settings.setSupportZoom(true); | |
settings.setDisplayZoomControls(false); | |
webView.setWebViewClient(new WebViewClient() { | |
@Override | |
public void onPageFinished(WebView view, String url) { | |
//webView.loadUrl(); | |
webView.setVisibility(View.VISIBLE); | |
layoutProgress.setVisibility(View.GONE); | |
progressBar.setIndeterminate(false); | |
super.onPageFinished(view, url); | |
webView.loadUrl("javascript:window.HTMLOUT.processHTML('<html>'+document.getElementsByTagName('a')[0].innerHTML+'</html>');"); | |
} | |
}); | |
if(isOnline()) { | |
webView.loadUrl("https://http-www-espncricinfo-com.0.freebasics.com/ci/engine/match/index.html?view=live"); | |
} else { | |
String summary = "<html><body><font color='red'>No Internet Connection</font></body></html>"; | |
webView.loadData(summary, "text/html", null); | |
toast("No Internet Connection."); | |
} | |
} | |
private void toast(String message) { | |
Toast.makeText(this, message, Toast.LENGTH_LONG).show(); | |
} | |
private boolean isOnline() { | |
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo netInfo = cm.getActiveNetworkInfo(); | |
return (netInfo != null && netInfo.isConnected()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.jovial.livematchscores"> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment