Latest News

Check Network Status

Check Network Status – Android Studio Tutorial

In this tutorial we will check internet connection like:
  • No internet connection
  • Connected with Wifi
  • Connected with Mobile Data
    Check Network Status
    Check Network Status


    Step 1: Create a new project OR Open your project

    Step 2: Add 3 Images to show networks status icons

    Step 3: Add ACCESS_NETWORK_STATE permission in AndroidMenifest.xml

    Step 4: Code

    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.blogspot.atifsoftwares.networconnectionstatus"><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><applicationandroid:allowBackup="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>

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><!--network status icon--><ImageViewandroid:id="@+id/conStatusIv"android:layout_width="120dp"android:layout_height="120dp"android:layout_centerHorizontal="true"android:layout_marginTop="20dp"android:scaleType="fitXY"/><!--network status text--><TextViewandroid:id="@+id/conStatusTv"android:textAlignment="center"android:textColor="#000"android:textSize="20sp"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:layout_width="wrap_content"android:layout_height="wrap_content" /><!--button to check network status--><Buttonandroid:id="@+id/conStatusBtn"style="@style/Base.Widget.AppCompat.Button.Colored"android:text="Check Network"android:layout_centerHorizontal="true"android:layout_alignParentBottom="true"android:layout_width="wrap_content"android:layout_height="wrap_content" /></RelativeLayout>

    MainActivity.java

    package com.blogspot.atifsoftwares.networconnectionstatus;import android.content.Context;import android.media.Image;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;public class MainActivity extends AppCompatActivity {//ViewsImageView mConStatusIv;TextView mConStatusTv;Button mConStatusBtn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//link views with xmlmConStatusIv = findViewById(R.id.conStatusIv);mConStatusTv = findViewById(R.id.conStatusTv);mConStatusBtn = findViewById(R.id.conStatusBtn);//button click to check network statusmConStatusBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//function call to check network connection statuscheckNetworkConnectionStatus();}});}private void checkNetworkConnectionStatus() {boolean wifiConnected;boolean mobileConnected;ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();if (activeInfo != null && activeInfo.isConnected()){ //connected with either mobile or wifiwifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;if (wifiConnected){ //wifi connectedmConStatusIv.setImageResource(R.drawable.ic_action_wifi);mConStatusTv.setText("Connected with Wifi");}else if (mobileConnected){ //mobile data connectedmConStatusIv.setImageResource(R.drawable.ic_action_mobile);mConStatusTv.setText("Connected with Mobile Data Connection");}}else { //no internet connectionmConStatusIv.setImageResource(R.drawable.ic_action_no);mConStatusTv.setText("No internet connection");}}}

    Step 5: Run Project

    Output
    Check Network Status

    0 Response to "Check Network Status"