How to check internet availability in app | Android App Development | Java
In this post, you will learn how to check the internet availability access in your application. Checking the internet access is important when working with API's, fetching or uploading data from servers etc...
Implementation:
First, in the activity_main.xml file, add the following code:
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.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:id="@+id/refreshLayout" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<Button | |
android:id="@+id/newActivity" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="32dp" | |
android:layout_marginEnd="32dp" | |
android:text="New Activity" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintHorizontal_bias="0.5" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
Then, create a new activity (NewActivity), and in its xml file (activity_new.xml), add the following code:
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.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" | |
tools:context=".NewActivity"> | |
<TextView | |
android:id="@+id/textView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Network available" | |
android:textSize="30sp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintHorizontal_bias="0.5" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<RelativeLayout | |
android:id="@+id/networkLayout" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:visibility="gone"> | |
<ImageView | |
android:id="@+id/img" | |
android:layout_width="400dp" | |
android:layout_height="400dp" | |
android:layout_centerHorizontal="true" | |
android:layout_marginTop="150dp" | |
android:src="@drawable/no_network"/> | |
<TextView | |
android:layout_below="@+id/img" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerHorizontal="true" | |
android:textSize="40sp" | |
android:text="No network..."/> | |
</RelativeLayout> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
Now in the MainActivity.java file, add the onClickListener to the button to navigate to the NewActivity. To do this, add the following code:
import android.content.Intent; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import androidx.appcompat.app.AppCompatActivity; | |
public class MainActivity extends AppCompatActivity { | |
Button newActivityButton; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
newActivityButton = findViewById(R.id.newActivity); | |
newActivityButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
startActivity(new Intent(MainActivity.this, NewActivity.class)); | |
} | |
}); | |
} | |
} |
After this, create a new Java class to check the network state. In this class (CheckNetwork.java), add the following code:
import android.content.Context; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
public class CheckNetwork { | |
public static boolean isInternetAvailable(Context context){ | |
NetworkInfo info = (NetworkInfo)((ConnectivityManager) | |
context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); | |
if (info==null){ | |
return false; | |
} | |
else{ | |
if(info.isConnected()){ | |
return true; | |
} | |
else{ | |
//Internet connection available | |
return true; | |
} | |
} | |
} | |
} |
Now, in the NewActivity.java file, add the following code:
import androidx.appcompat.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.RelativeLayout; | |
import android.widget.TextView; | |
public class NewActivity extends AppCompatActivity { | |
RelativeLayout relativeLayout; | |
TextView txt; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_new); | |
relativeLayout = findViewById(R.id.networkLayout); | |
txt = findViewById(R.id.textView); | |
if(CheckNetwork.isInternetAvailable(this)){ | |
relativeLayout.setVisibility(View.GONE); | |
txt.setVisibility(View.VISIBLE); | |
}else{ | |
relativeLayout.setVisibility(View.VISIBLE); | |
txt.setVisibility(View.GONE); | |
} | |
} | |
} |
Finally, add the Internet and Access Network State permissions in the AndroidManifest.xml file
<uses-permission android:name="android.permission.INTERNET"/> | |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> |
And that's it...Now try running your app. To learn more about reading the Network States in Android, refer this : https://developer.android.com/training/basics/network-ops/reading-network-state?authuser=1
Happy Learning... :-)
Comments
Post a Comment