0% found this document useful (0 votes)
18 views2 pages

Practical 09

The document provides a practical example of creating a toggle button in an Android application to control the visibility of an image representing Bluetooth status. It includes XML code for the layout, featuring an ImageView and a ToggleButton, as well as Java code for the functionality that toggles the image's visibility based on the button's state. The button displays 'ON' when checked and 'OFF' when unchecked, showing or hiding the image accordingly.

Uploaded by

harshdpatil445
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)
18 views2 pages

Practical 09

The document provides a practical example of creating a toggle button in an Android application to control the visibility of an image representing Bluetooth status. It includes XML code for the layout, featuring an ImageView and a ToggleButton, as well as Java code for the functionality that toggles the image's visibility based on the button's state. The button displays 'ON' when checked and 'OFF' when unchecked, showing or hiding the image accordingly.

Uploaded by

harshdpatil445
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/ 2

Practical 09

1) Write a program to create a toggle button to display ON/OFF Bluetooth on the display
screen.
XML CODE:-
<?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">

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="89dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/img" />

<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:text="ToggleButton"
android:checked="true"
android:textOff="OFF"
android:textOn="ON"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>

JAVA CODE:-
package com.example.bluetooth;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {


ToggleButton button1;
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = findViewById(R.id.toggleButton);
img = findViewById(R.id.imageView);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(button1.isChecked())
{
img.setVisibility(View.VISIBLE);
}else{
img.setVisibility(View.INVISIBLE);
}
}
});
}
}

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