activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout
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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</layout>
MainActivity.kt
package com.androindian.chekinginternet
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.androindian.chekinginternet.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
var binding: ActivityMainBinding? = null
var connectionChecking: ConnectionChecking? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(
this@MainActivity, R.layout.activity_main
)
connectionChecking = ConnectionChecking()
binding?.button?.setOnClickListener {
if (connectionChecking!!.isConnectingToInternet(this@MainActivity)) {
Toast.makeText(this@MainActivity, "Net is there", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this@MainActivity, "No Net", Toast.LENGTH_SHORT).show()
}
}
}
}
ConnectionChecking.kt
package com.androindian.chekinginternet
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo
class ConnectionChecking {
fun isConnectingToInternet(context: Context): Boolean {
val connectivity = context.getSystemService(
Context.CONNECTIVITY_SERVICE
) as ConnectivityManager
if (connectivity != null) {
val info = connectivity.allNetworkInfo
if (info != null) for (i in info.indices) if (info[i].state == NetworkInfo.State.CONNECTED) {
return true
}
}
return false
}
}
build.gradle
plugins {
id 'com.android.application'
}
android {
namespace 'com.androindian.chekinginternet'
compileSdk 33
defaultConfig {
applicationId "com.androindian.chekinginternet"
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dataBinding{
enabled=true
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.ChekingInternet"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>