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

Practical NO - 9

The document provides a practical implementation of a user interface in Android featuring a Button, Image Button, and Toggle Button. It includes the XML layout for the main activity and the Java code for handling the Toggle Button's state changes, displaying Toast messages based on whether Bluetooth is turned on or off. The layout utilizes a ConstraintLayout to position the TextView and ToggleButton appropriately.
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)
11 views4 pages

Practical NO - 9

The document provides a practical implementation of a user interface in Android featuring a Button, Image Button, and Toggle Button. It includes the XML layout for the main activity and the Java code for handling the Toggle Button's state changes, displaying Toast messages based on whether Bluetooth is turned on or off. The layout utilizes a ConstraintLayout to position the TextView and ToggleButton appropriately.
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.

09

Develop a program to Implement Button,Image Button and Toggle Button

• activity_main.xml

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


<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/bluetoothLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:text="BLUETOOTH"
android:textColor="#000"
android:textSize="30dp"
android:textStyle="bold"
android:layout_marginStart="32dp"
android:layout_marginTop="100dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<ToggleButton
android:id="@+id/toggleBluetooth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="20dp"
android:textOff="Off"
android:textOn="On"
app:layout_constraintStart_toEndOf="@id/bluetoothLabel"
app:layout_constraintTop_toTopOf="@id/bluetoothLabel"
android:layout_marginStart="16dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

• MainActivity.java

package com.example.practical9;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initialize the ToggleButton


ToggleButton toggleButton = findViewById(R.id.toggleBluetooth);

// Set an OnCheckedChangeListener
toggleButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
// When toggle is ON
Toast.makeText(MainActivity.this, "Bluetooth is turned on",
Toast.LENGTH_SHORT).show();
} else {
// When toggle is OFF
Toast.makeText(MainActivity.this, "Bluetooth is turned off",
Toast.LENGTH_SHORT).show();
}
});
}
}

You might also like