Write a Program to create a toggle button to display the ON/OFF Bluetooth on the display screen.

Xml Code

<?xml version="1.0" encoding="utf-8"?>

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".Main19Activity">

    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"

        android:layout_x="33dp"

        android:layout_y="104dp"

        android:fontFamily="monospace"

        android:text="BLUETOOTH"

        android:textColor="#000"

        android:textSize="30dp"

        android:textStyle="bold" />

    <ToggleButton

        android:id="@+id/tb1"

      android:layout_width="wrap_content"        android:layout_height="wrap_content"

        android:layout_x="274dp"

        android:layout_y="100dp"

        android:text="ToggleButton"

        android:textStyle="bold"

        android:textSize="20dp"

        android:textOff="Off"

        android:textOn="On"/>

</AbsoluteLayout>










Java Code:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.CompoundButton;

import android.widget.Toast;

import android.widget.ToggleButton;

public class Main19Activity extends AppCompatActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main19);

        ToggleButton t1=findViewById(R.id.tb1);

        t1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if(isChecked)

            {               Toast.makeText(Main19Activity.this, "Bluetooth is turned on", Toast.LENGTH_SHORT).show();

            }

            else

            {               Toast.makeText(Main19Activity.this, "Bluetooth is turned off", Toast.LENGTH_SHORT).show();

            }

            }

        });

    }}

Output:

 


Comments

Popular posts from this blog

Write a Program to accept and display the student information

Write a program to create a first display screen on any search engine using auto complete text View