0% found this document useful (0 votes)
22 views4 pages

Prac 18

The document contains an XML layout for an Android application featuring two SwitchCompat widgets for Bluetooth and WiFi settings. The MainActivity.kt file includes the logic to handle the state changes of these switches, displaying a Toast message indicating whether Bluetooth or WiFi is ON or OFF. This setup is part of a Kotlin-based Android project.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

Prac 18

The document contains an XML layout for an Android application featuring two SwitchCompat widgets for Bluetooth and WiFi settings. The MainActivity.kt file includes the logic to handle the state changes of these switches, displaying a Toast message indicating whether Bluetooth or WiFi is ON or OFF. This setup is part of a Kotlin-based Android project.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical-18

 Android_main.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:androidx="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.appcompat.widget.SwitchCompat
android:id="@+id/b_s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text="@string/bluetooth"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.512" />

<androidx.appcompat.widget.SwitchCompat
android:id="@+id/w_s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="@string/wifi"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/b_s"
app:layout_constraintVertical_bias="0.079" />
</androidx.constraintlayout.widget.ConstraintLayout>

 MainActivity.kt
package com.example.kotlin_mad

import android.os.Bundle
import android.widget.Button
import android.widget.CompoundButton
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SwitchCompat

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) // Set the content view to your layout

val bluetoothSwitch: SwitchCompat = findViewById(R.id.b_s)


val wifiSwitch: SwitchCompat = findViewById(R.id.w_s)

// Bluetooth switch listener


bluetoothSwitch.setOnCheckedChangeListener { _: CompoundButton, isChecked:
Boolean ->
val state = if (isChecked) "ON" else "OFF"
Toast.makeText(this, "Bluetooth is $state", Toast.LENGTH_SHORT).show()
}

// WiFi switch listener


wifiSwitch.setOnCheckedChangeListener { _: CompoundButton, isChecked: Boolean ->
val state = if (isChecked) "ON" else "OFF"
Toast.makeText(this, "WiFi is $state", Toast.LENGTH_SHORT).show()
}
}
}

You might also like