Rohith Rohith - 7 months ago 69
Java Question

how to add custom error page when no interent is detected in android web app

iam begineer in nadroid development , i tried this logic

ConnectivityManager cManager = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
NetworkInfo nInfo = cManager.getActiveNetworkInfo();
if(nInfo!=null && nInfo.isConnected()) {


to detect internet and display a error
Toast
but when i implemented this by disabling my internet, iam getting a white page while showing toast and after displaying the toast iam getting a white page with loading symbol . i would like to change that white page while displaying the
Toast
and i would like to fix the issue that iam getting a white page with loading symbol after displaying the toast.Please help me fixing this issue .

Here is my MainActivity.java Source :

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ShareActionProvider;
import android.widget.Toast;


public class MainActivity extends Activity {

private WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);

ConnectivityManager cManager = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
NetworkInfo nInfo = cManager.getActiveNetworkInfo();
if(nInfo!=null && nInfo.isConnected()) {




mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("https://mobile-tech2dsk.blogspot.in/");
mWebView.setWebViewClient(new com.example.tech2dsk.tech2dsk.MyAppWebViewClient(){

@Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.progressBar1).setVisibility(View.GONE);
//show webview
findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
}});


} else {
Toast.makeText(this, "Network is not available", Toast.LENGTH_LONG).show();
}





}

@Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}


private ShareActionProvider mShareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {

/** Inflating the current activity's menu with res/menu/items.xml */
getMenuInflater().inflate(R.menu.menu_main, menu);

/** Getting the actionprovider associated with the menu item whose id is share */
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();

/** Setting a share intent */
mShareActionProvider.setShareIntent(getDefaultShareIntent());

return super.onCreateOptionsMenu(menu);

}

/** Returns a share intent */
private Intent getDefaultShareIntent(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Convert Website to Android Application");
intent.putExtra(Intent.EXTRA_TEXT," Vist www.AndroidWebViewApp.com if you Want to Convert your Website or Blog to Android Application");
return intent;

}


}

Answer

Put this in your activity_main.xml file

<LinearLayout
android:id="@+id/ringtone_layout_retry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"
android:visibility="gone">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check your connection and try again." />

<Button
android:id="@+id/ringtone_btnRetry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Retry" />
</LinearLayout>

and instead of showing the Toast using this code:

LinearLayout ly = (LinearLayout)_view.findViewById(R.id.ringtone_layout_retry);
findViewById(R.id.progressBar1).setVisibility(View.GONE);
ly.setVisibility(View.VISIBLE);