0

I have been trying to use a custom Image that is to be shown to a user when he is offline and when the user clicks on the image, the activity should be reloaded.

P.s I am using Blogger API

Joy Dey
  • 563
  • 1
  • 5
  • 17
  • need to make a common xml layout file for no internet connection and include in all activity's xml so you can easily handle click event to check is internet availble or not that's it. – Jaydip Radadiya Jun 28 '18 at 06:12

2 Answers2

1

first add this permission to manifest

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

thin in your activty

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

and use like this

if(isNetworkAvailable()){
//internt connect
}else{
// no network 
//you can show image here by adding layout and set visibility gone and when no connection set visible
}
menasoft
  • 135
  • 1
  • 2
  • 12
  • How to refresh the activity when the user clicks on the image? – Joy Dey Jun 27 '18 at 20:26
  • you can set on click on image again this code ` if(isNetworkAvailable()){ //internt connect }else{ // no network //you can show image here by adding layout and set visibility gone and when no connection set visible }` – menasoft Jun 27 '18 at 20:27
0

You can use Custom Dialog of full Screen to check for internet. So, you can write,

if(isNetworkAvailable()){
        //Your Logic for Internet Connection
 }else {
    val noInternetDialog = Dialog(this, android.R.style.Theme)
    noInternetDialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    noInternetDialog.setContentView(R.layout.no_internet_layout)
    val window = noInternetDialog.window
    window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
    // What ever you have widget, can use them here.
    //for example
    val button_try_again = noInternetDialog.findViewById(R.id.buttonId)
    val image_to_display = noInternetDialog.findViewById(R.id.imageId)
    // listeners for image

    noInternetDialog.show
 }

isNetworkAvaibale() is,

fun isNetworkAvailable(): Boolean {

        val connectivityManager = ApplicationInit.getAppContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val activeNetworkInfo = connectivityManager.activeNetworkInfo
        return activeNetworkInfo != null && activeNetworkInfo.isConnected
    } 

PS : Do not forget to add Internet Permissions

Himanshu
  • 91
  • 1
  • 2