Show a progressbar on top while loading webview app - java

I'm trying to show a progressbar on top top the app like google chrome for android shows loading bar while loading a website.
I want to show this progress bar when user clicks on any internal links of the webview app...just like google chrome shows.
Here is what I've tried, this code not working, app stops working even it doesn't start.
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
private ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setBackgroundDrawable(null);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(100);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.main_webview);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
progressBar.setProgress(100);
if (mWebView.getProgress() == 100) {
// show webview
mWebView.setVisibility(View.VISIBLE);
// hide splash
findViewById(R.id.splash_screen).setVisibility(View.GONE);
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().contains("www.example.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
view.loadUrl("file:///android_asset/offline.html");
}
});
mWebView.loadUrl("http://www.example.com/?m=1");
}
}
main activity:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/splash_screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#mipmap/splash_logo"
android:visibility="visible" />
<WebView
android:id="#+id/main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<ProgressBar
android:id="#+id/progressBar"
android:minHeight="2dip"
android:maxHeight="2dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#android:style/Widget.ProgressBar.Horizontal" />
</RelativeLayout>

You create MyWebChromeClient
public class MyWebChromeClient extends WebChromeClient {
private ProgressListener mListener;
public MyWebChromeClient(ProgressListener listener) {
mListener = listener;
}
#Override
public void onProgressChanged(WebView view, int newProgress) {
mListener.onUpdateProgress(newProgress);
super.onProgressChanged(view, newProgress);
}
public interface ProgressListener {
public void onUpdateProgress(int progressValue);
}
}
in Your MainActivity
public class MainActivity extends AppCompatActivity implements MyWebChromeClient.ProgressListener{
private WebView mWebView;
private ProgressBar mProgressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// add progress bar
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
mWebView.setWebChromeClient(new MyWebChromeClient(this));
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
mProgressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mProgressBar.setVisibility(View.GONE);
}
});
}
#Override
public void onUpdateProgress(int progressValue) {
mProgressBar.setProgress(progressValue);
if (progressValue == 100) {
mProgressBar.setVisibility(View.INVISIBLE);
}
}
}
in activity_main.xml
<RelativeLayout
android:id="#+id/relative_web_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="#dimen/progress_bar_height"
android:progressDrawable="#drawable/bg_progress_bar_webview" />
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/progressBar" />
</RelativeLayout>
in drawable create bg_progress_bar_webview.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#android:id/background"
android:drawable="#android:color/transparent"/>
<item android:id="#android:id/secondaryProgress">
<scale
android:drawable="#color/progress_bar_second"
android:scaleWidth="100%" />
</item>
<item android:id="#android:id/progress">
<scale
android:drawable="#color/progress_bar_runing"
android:scaleWidth="100%" />
</item>
</layer-list>
Hope !it helps you

Related

How to add no internet connection page in my web view application?

I am trying to add a no internet connection page to my webview application but I can't. I am not much experienced on it just I am creating this application by following some youtube videos. I have added progressbar, swiprefresh, exit dialogue popup and want to add no internet connection page too along with these. I have attached my code below -Kindly help me please!
activity_main.xml
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
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"
android:id="#+id/swipe"
tools:context=".MainActivity">
<WebView
android:id="#+id/myWebview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
<ProgressBar
android:id="#+id/progress_Bar"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="8dp"
android:progress="20"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/relativeLayout">
<ImageView
android:layout_width="240dp"
android:layout_height="240dp"
android:src="#drawable/ic_no_internet"
android:layout_centerHorizontal="true"
android:id="#+id/noInternet"
android:layout_marginTop="100dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No Internet Connection"
android:layout_below="#+id/noInternet"
android:textAlignment="center"
android:textSize="30sp"
android:id="#+id/txtNoConnection"
android:layout_marginTop="20dp"/>
<Button
android:layout_width="140dp"
android:layout_height="65dp"
android:text="Retry"
android:background="#color/teal_700"
android:textColor="#ffffff"
android:textSize="22sp"
android:layout_below="#+id/txtNoConnection"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:id="#+id/btnNoInternet"/>
</RelativeLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
And MainActivity.java
public class MainActivity extends AppCompatActivity {
WebView webView;
private String webUrl="http://godigitalzone.in/";
ProgressBar progressBar;
ProgressDialog progressDialog;
SwipeRefreshLayout swipe;
RelativeLayout relativeLayout;
Button btnNoInternet;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipe = findViewById(R.id.swipe);
webView=findViewById(R.id.myWebview);
progressBar=findViewById(R.id.progress_Bar);
progressDialog=new ProgressDialog(this);
progressDialog.setMessage("Please wait");
btnNoInternet = (Button) findViewById(R.id.btnNoInternet);
relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
webView.loadUrl(webUrl);
webView.getSettings().setJavaScriptEnabled(true);
checkConnection();
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
webView.reload();
}
});
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
swipe.setRefreshing(false);
super.onPageFinished(view, url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.setWebChromeClient(new WebChromeClient(){
#Override
public void onProgressChanged(WebView view, int newProgress){
progressBar.setVisibility(View.VISIBLE);
progressDialog.show();
if (newProgress==100){
progressBar.setVisibility(View.GONE);
setTitle(view.getTitle());
progressDialog.dismiss();
}
super.onProgressChanged(view, newProgress);
}
});
}
#Override
public void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
webView.saveState(outState);
}
#Override
public void onBackPressed() {
if (webView.canGoBack()){
webView.goBack();
}
else{
AlertDialog.Builder builder=new AlertDialog.Builder(this);;
builder.setMessage("Are you sure want to Exit?")
.setCancelable(false)
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
})
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
MainActivity.super.onBackPressed();
}
});
AlertDialog alertDialog=builder.create();
alertDialog.show();
}
}
public void checkConnection(){
ConnectivityManager connectivityManager = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isConnected()){
webView.setVisibility(View.VISIBLE);
relativeLayout.setVisibility(View.GONE);
}
else if (mobile.isConnected()){
webView.setVisibility(View.VISIBLE);
relativeLayout.setVisibility(View.GONE);
}
else {
webView.setVisibility(View.GONE);
relativeLayout.setVisibility(View.VISIBLE);
}
}
}
I have used the following in my projects:
import android.content.Context;
import android.net.ConnectivityManager;
public class DetectConnection {
public static boolean checkInternetConnection(Context context) {
ConnectivityManager con_manager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
return (con_manager.getActiveNetworkInfo() != null
&& con_manager.getActiveNetworkInfo().isAvailable()
&& con_manager.getActiveNetworkInfo().isConnected());
}
}
import android.content.Context;
import android.net.ConnectivityManager;
public class DetectConnection {
public static boolean checkInternetConnection(Context context) {
ConnectivityManager con_manager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
return (con_manager.getActiveNetworkInfo() != null
&& con_manager.getActiveNetworkInfo().isAvailable()
&& con_manager.getActiveNetworkInfo().isConnected());
}
}
if (!DetectConnection.checkInternetConnection(this)) {
Toast.makeText(getApplicationContext(), "No Internet!", Toast.LENGTH_SHORT).show();
} else {
wv = (WebView) findViewById(R.id.donate_webView1);
c = new CustomWebViewClient();
wv.setWebViewClient(c);
wv.clearCache(true);
wv.clearHistory();
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
wv.getSettings().setBuiltInZoomControls(true);
wv.loadUrl("http://www.google.com");
}
// Function to load all URLs in same webview
private class CustomWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!DetectConnection.checkInternetConnection(this)) {
Toast.makeText(getApplicationContext(), "No Internet!", Toast.LENGTH_SHORT).show();
} else {
view.loadUrl(url);
}
return true;
}
}
Update the Manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Custom NoInternet In Andriod Studio But image and layout not scale to fit

I used this code to make custom No internet dialog but when i successfully build then the problem is half screen show my custom image but half show the no connection errorenter image description here
In Activity_main.xml
enter image description here
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout 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"
android:id="#+id/swipeRefreshLayout"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ProgressBar
android:id="#+id/ProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="9dp"
android:layout_marginTop="-2dp"
android:progress="20"
android:visibility="gone" />
<ImageView
android:id="#+id/splashloading01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/todo"
android:src="#mipmap/splash_loading"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="gone">
</ImageView>
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" >
</WebView>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/relativeLayout">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/internet"
android:layout_centerHorizontal="true"
android:id="#+id/noConnectionLogo"
android:contentDescription="#string/todo" />
<Button
android:layout_width="140dp"
android:layout_height="55dp"
android:text="Join"
android:id="#+id/btnNoConnection">
</Button>
</RelativeLayout>
</LinearLayout>
In MainActivity.xml my code is
package www.foodmartshop.com;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.content.Context;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
RelativeLayout relativeLayout;
Button btnNoInternetConnection;
ProgressBar progressBarWeb;
SwipeRefreshLayout swipeRefreshLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
//Button No Internet//
btnNoInternetConnection = (Button) findViewById(R.id.btnNoConnection);
relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout); //No Internet//
webSettings.setJavaScriptEnabled(true);
progressBarWeb = (ProgressBar) findViewById(R.id.ProgressBar);
//Swipe//
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
mWebView.reload();
}
}); //Swipe//
mWebView.loadUrl("https://www.foodmartshop.com");
mWebView.setWebViewClient(new www.foodmartshop.MyAppWebViewClient() {
//Hide The Loading Page
#Override
public void onPageFinished(WebView view, String url) {
findViewById(R.id.splashloading01).setVisibility(View.GONE);
findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
swipeRefreshLayout.setRefreshing(false);
}
});
mWebView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
progressBarWeb.setVisibility(View.VISIBLE);
progressBarWeb.setProgress(newProgress);
if (newProgress == 100) {
progressBarWeb.setVisibility(View.GONE);
}
super.onProgressChanged(view, newProgress);
}
});
}
#Override
public void onBackPressed() {
if(mWebView.canGoBack()){
mWebView.goBack();
}
else{
super.onBackPressed();
}
}
private class MyAppWebViewClient extends WebViewClient {
}
public void checkConnection() {
ConnectivityManager connectivityManager = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobileNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isConnected()) {
mWebView.setVisibility(View.VISIBLE);
relativeLayout.setVisibility(View.GONE);
}
else if (mobileNetwork.isConnected()){
mWebView.setVisibility(View.VISIBLE);
relativeLayout.setVisibility(View.GONE);
}
else{
mWebView.setVisibility(View.GONE);
relativeLayout.setVisibility(View.VISIBLE);
}
}
}
In Mamifest my code is :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="www.foodmartshop.com">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:usesCleartextTraffic="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
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>
Plz help me i didnt understand the problem
try this---->
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hdyt);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
//Button No Internet//
btnNoInternetConnection = (Button) findViewById(R.id.btnNoConnection);
relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout); //No Internet//
webSettings.setJavaScriptEnabled(true);
progressBarWeb = (ProgressBar) findViewById(R.id.ProgressBar);
checkConnection();
// mWebView.loadUrl("https://www.foodmartshop.com");
//Swipe//
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
checkConnection();
mWebView.reload();
}
}); //Swipe//
// mWebView.loadUrl("https://www.foodmartshop.com");
mWebView.setWebViewClient(new MyAppWebViewClient() {
//Hide The Loading Page
#Override
public void onPageFinished(WebView view, String url) {
findViewById(R.id.splashloading01).setVisibility(View.GONE);
findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
swipeRefreshLayout.setRefreshing(false);
}
});
mWebView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
checkConnection();
progressBarWeb.setVisibility(View.VISIBLE);
progressBarWeb.setProgress(newProgress);
if (newProgress == 100) {
progressBarWeb.setVisibility(View.GONE);
}
else
{
checkConnection();
}
super.onProgressChanged(view, newProgress);
}
});
}
#Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
private class MyAppWebViewClient extends WebViewClient {
}
public void checkConnection() {
ConnectivityManager connectivityManager = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobileNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (!wifi.isConnected()) {
progressBarWeb.setVisibility(View.GONE);
mWebView.setVisibility(View.GONE);
relativeLayout.setVisibility(View.VISIBLE);
} else if (!mobileNetwork.isConnected()) {
progressBarWeb.setVisibility(View.GONE);
mWebView.setVisibility(View.VISIBLE);
relativeLayout.setVisibility(View.GONE);
} else {
progressBarWeb.setVisibility(View.GONE);
mWebView.loadUrl("https://www.foodmartshop.com");
mWebView.setVisibility(View.VISIBLE);
relativeLayout.setVisibility(View.GONE);
}
}
}
later you must be noticing progressbar loads continuously even if internet is not there then you must use this-->
just an example -->
webView.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(WebView view, String url) {
progressDialog.hide();
progressBar.setVisibility(View.GONE);
}
});

error: reached end of file while parsing in Android Studio

This is my 1st time using Java and Android, I’m trying to launch a simple application with WebView script.
Everything was okay but I am facing issue when I’m trying to add loading bar for my web view page. The problem:
error: reached end of file while parsing
I’m using this code for Mainactivity
package com.example.last;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
private WebView webView;
private ProgressBar mProgressbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView =(WebView) findViewById(R.id.WebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mProgressbar=findViewById(R.id.progressBar);
mProgressbar.setMax(100);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://beko963.000webhostapp.com");
webView.setWebViewClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
mProgressbar.setProgress(newProgress);
}
#Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
}
#Override
public void onReceivedIcon(WebView view, Bitmap icon) {
super.onReceivedIcon(view, icon);
}
}}
#Override
public void onBackPressed () {
if (webView.canGoBack()) {
webView.goBack();
} else
super.onBackPressed();
}}
And using this for main XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:padding="16dp"
tools:context=".MainActivity">
<ProgressBar
android:id="#+id/progressBar"
style="#style/Base.Widget.AppCompat.ProgressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true" />
<WebView
android:id="#+id/WebView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
I just want to make a simple application with loading icon, could you please help my and fix my code?
As John and Others Says
webView.setWebViewClient(new WebChromeClient() {}} where is the closing )
//replace last } with );
webView.setWebViewClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
mProgressbar.setProgress(newProgress);
}
#Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
}
#Override
public void onReceivedIcon(WebView view, Bitmap icon) {
super.onReceivedIcon(view, icon);
}
});
Correct your code
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
mProgressbar.setProgress(newProgress);
}
#Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
}
#Override
public void onReceivedIcon(WebView view, Bitmap icon) {
super.onReceivedIcon(view, icon);
}
})

show splash screen while Webview is loading in background

I'm using below code to populate my Main activity with Webview, but as it takes a certain time to load the page and it appears white blank. So I will like to show splash screen for my Webview before page load finishes. I'm not using any webview from Layout activity.
package com.faraksoch.sagar.facebook;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
private WebView mWebview = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview.loadUrl("http://www.google.com//");
setContentView(mWebview);
}
}
I know that this code should work but I when I combine them..it won't work
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");
Any help will be appreciated. Thanks in advance.
Make sure webview is visibility is not GONE before it is loading.
Please refer below code.
public class WebViewActivity extends AppCompatActivity {
private WebView mWebView;
private ImageView mSplashView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
mWebView = (WebView) findViewById(R.id.webview);
mSplashView = (ImageView) findViewById(R.id.splash_view);
mWebView.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mSplashView.setVisibility(View.GONE);
mWebView.setVisibility(View.VISIBLE);
Toast.makeText(getBaseContext(), "Page Loaded.", Toast.LENGTH_SHORT).show();
}
});
mWebView.loadUrl("http://yoururlhere.com");
mWebView.setVisibility(View.GONE);
mSplashView.setVisibility(View.VISIBLE);
}
}
Layout:
<?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/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
/>
<ImageView
android:id="#+id/splash_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#mipmap/ic_launcher"
android:visibility="gone"
/>
</android.support.constraint.ConstraintLayout>

Background image selecting in the app

What I wanted.
I'm currently having a problem setting the background for my app. What I wanted is a page with selections of wallpaper/background for the app user to choose. Once they chose the background the wanted by clicking the Image view, the whole app should use the image as their background.That's all.
What I've done.
I've created an activity that has 2 ImageView as the choices of wallpaper available and assigns the image view to set the background image when the user click on them. The problem now is I don't know how to save the setting and apply it to all other activity in my project.
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: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"
android:id="#+id/settingscreen"
tools:context="com.example.naris.auin.SettingsActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Background Selector"
android:id="#+id/textViewBackgroundSelector"
android:layout_below="#+id/switchNightMode"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageViewBackground1"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp"
android:src="#drawable/rateicon"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageViewBackground2"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/textViewBackgroundSelector"
android:layout_toEndOf="#+id/textViewBackgroundSelector"
android:layout_marginLeft="36dp"
android:layout_marginStart="36dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
Settings Activity
public class SettingsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
final RelativeLayout Settingscreen = (RelativeLayout) findViewById(R.id.settingscreen);
ImageView ImageViewBackground1 = (ImageView) findViewById(R.id.imageViewBackground1);
ImageView ImageViewBackground2 = (ImageView) findViewById(R.id.imageViewBackground2);
ImageViewBackground1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Settingscreen.setBackgroundResource(R.drawable.faqicon);
}
});
ImageViewBackground2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Settingscreen.setBackgroundResource(R.drawable.rateicon);
}
});
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == android.view.KeyEvent.KEYCODE_BACK)
{
startActivity(new Intent(SettingsActivity.this,
MainActivity.class));
finish();
}
return false;
};
}
Thanks in advance.
public class SettingsActivity extends AppCompatActivity {
private static final String PREF_NAME = "nextage_quiz";
private static final int PRIVATE_MODE = 0;
SharedPreferences getPrefs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
getPrefs = this.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
final RelativeLayout Settingscreen = (RelativeLayout) findViewById(R.id.settingscreen);
ImageView ImageViewBackground1 = (ImageView) findViewById(R.id.imageViewBackground1);
ImageView ImageViewBackground2 = (ImageView) findViewById(R.id.imageViewBackground2);
ImageViewBackground1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Settingscreen.setBackgroundResource(R.drawable.faqicon);
getPrefs.edit().putInt("id", R.drawable.faqicon).apply();
}
});
ImageViewBackground2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Settingscreen.setBackgroundResource(R.drawable.rateicon);
getPrefs.edit().putInt("id", R.drawable.faqicon).apply();
}
});
}
MainActivity:
public class MainActivity extends AppCompatActivity {
private static final String PREF_NAME = "nextage_quiz";
private static final int PRIVATE_MODE = 0;
SharedPreferences getPrefs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getPrefs = this.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
ImageView background= (ImageView) findViewById(R.id.background);
if(getPrefs.getInt("id",0) != 0)
background.setBackgroundResource(getPrefs.getInt("id",0));
}
Upadate: Use these variables
private static final String PREF_NAME = "nextage_quiz";
private static final int PRIVATE_MODE = 0;
SharedPreferences getPrefs;
You should do that using Preferences.
see the link for more details...
https://developer.android.com/guide/topics/ui/settings.html

Categories