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

Prac9 1

The document provides instructions for creating an Android application with a toggle button to display the ON/OFF status of Bluetooth. It includes the XML layout for the user interface and the Java code for the main activity that handles the toggle button's state changes. When the button is toggled, a Toast message indicates whether Bluetooth is turned on or off.

Uploaded by

Kolekar Yashraj
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)
10 views4 pages

Prac9 1

The document provides instructions for creating an Android application with a toggle button to display the ON/OFF status of Bluetooth. It includes the XML layout for the user interface and the Java code for the main activity that handles the toggle button's state changes. When the button is toggled, a Toast message indicates whether Bluetooth is turned on or off.

Uploaded by

Kolekar Yashraj
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 no.

9:
1.Write a program to create a toggle Button to
display ON/OFF Bluetooth on the display screen.
▪ Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 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/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bluetooth"
android:layout_x="30dp"
android:layout_y="120dp"
android:textSize="40dp"
android:textStyle="bold"
/>
<ToggleButton
android:id="@+id/tb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_y="120dp"
android:layout_x="220dp"
android:text="ToggleButton"
android:textStyle="bold"
android:textSize="20dp"
android:textOff="Off"
android:textOn="On"
/>

</AbsoluteLayout>
▪ MainActivity.java
package com.example.toggle;

import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ToggleButton t1 = findViewById(R.id.tb1);
t1.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (isChecked) {
Toast.makeText(MainActivity.this,
"Bluetooth is turned on",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this,
"Bluetooth is turned off",
Toast.LENGTH_SHORT).show();
}
}
});
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy