other code
other code
webView.getSettings().setBuiltInZoomControls(true);
Write a program to convert temperature from celcius to farenhite and vice versa using Toggle
button. (Design UI as per your choice. Write XML and java file)
Here's a simple Android app that converts temperature between Celsius and Fahrenheit using
a ToggleButton.
When the ToggleButton is ON, it converts from Celsius to Fahrenheit.
When it's OFF, it converts from Fahrenheit to Celsius.
<EditText
android:id="@+id/editTemp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Temperature"
android:inputType="numberDecimal" />
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="C to F"
android:textOff="F to C" />
<Button
android:id="@+id/btnConvert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Convert" />
<TextView
android:id="@+id/textResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result will show here"
android:textSize="18sp"
android:paddingTop="20dp" />
</LinearLayout>
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
EditText editTemp;
ToggleButton toggleButton;
Button btnConvert;
TextView textResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTemp = findViewById(R.id.editTemp);
toggleButton = findViewById(R.id.toggleButton);
btnConvert = findViewById(R.id.btnConvert);
textResult = findViewById(R.id.textResult);
btnConvert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String input = editTemp.getText().toString();
if (input.isEmpty()) {
textResult.setText("Please enter a temperature");
return;
}
if (toggleButton.isChecked()) {
// Celsius to Fahrenheit
result = (temp * 9 / 5) + 32;
textResult.setText("Fahrenheit: " + result);
} else {
// Fahrenheit to Celsius
result = (temp - 32) * 5 / 9;
textResult.setText("Celsius: " + result);
}
}
});
}
}
Let me know if you'd like the same app using Kotlin or with custom styling!
Write a program to implement Android Activity Life Cycle. Use toast messages to display
message through life cycle.
Here's a simple Android program that demonstrates the Android Activity Life Cycle using
Toast messages.
Whenever an activity lifecycle method like onCreate, onStart, etc. is called, a Toast message
will show.
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, "onCreate Called", Toast.LENGTH_SHORT).show();
}
@Override
protected void onStart() {
super.onStart();
Toast.makeText(this, "onStart Called", Toast.LENGTH_SHORT).show();
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(this, "onResume Called", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPause() {
super.onPause();
Toast.makeText(this, "onPause Called", Toast.LENGTH_SHORT).show();
}
@Override
protected void onStop() {
super.onStop();
Toast.makeText(this, "onStop Called", Toast.LENGTH_SHORT).show();
}
@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(this, "onRestart Called", Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
Toast.makeText(this, "onDestroy Called", Toast.LENGTH_SHORT).show();
}
}
<TextView
android:text="Activity Life Cycle Demo"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
🧪 Try It
Design UI using table layout to display buttons with 0 – 9 numbers on it. Even display submit and
clear button. When user clicks on particular buttons and later when clicks on submit button, it
should display the numbers clicked.
Sure! Here's the simplest version of the app:
<TableRow>
<Button android:id="@+id/btn1" android:text="1"/>
<Button android:id="@+id/btn2" android:text="2"/>
<Button android:id="@+id/btn3" android:text="3"/>
</TableRow>
<TableRow>
<Button android:id="@+id/btn4" android:text="4"/>
<Button android:id="@+id/btn5" android:text="5"/>
<Button android:id="@+id/btn6" android:text="6"/>
</TableRow>
<TableRow>
<Button android:id="@+id/btn7" android:text="7"/>
<Button android:id="@+id/btn8" android:text="8"/>
<Button android:id="@+id/btn9" android:text="9"/>
</TableRow>
<TableRow>
<Button android:id="@+id/btn0" android:text="0"/>
<Button android:id="@+id/btnSubmit" android:text="Submit"/>
<Button android:id="@+id/btnClear" android:text="Clear"/>
</TableRow>
<TableRow>
<TextView
android:id="@+id/txtResult"
android:text="Clicked Numbers:"
android:textSize="18sp" />
</TableRow>
</TableLayout>
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
findViewById(R.id.btnSubmit).setOnClickListener(v ->
txtResult.setText("Clicked Numbers: " + number)
);
findViewById(R.id.btnClear).setOnClickListener(v -> {
number = "";
txtResult.setText("Clicked Numbers:");
});
}
}
This version is made as easy as possible for learning. Would you like me to also explain how it
works step by step?
Explain property animation method to animate the properties of view object with example
alpha (transparency)
rotation
x, y position
scale
✅ How to Use:
ObjectAnimator anim = ObjectAnimator.ofFloat(view, "property", start, end);
anim.setDuration(1000); // time in milliseconds
anim.start();
Sure! Here's the simplest answer to show the user's car (current location) on a map.
GoogleMap map;
FusedLocationProviderClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
client = LocationServices.getFusedLocationProviderClient(this);
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
return;
}
map.setMyLocationEnabled(true);
client.getLastLocation().addOnSuccessListener(location -> {
if (location != null) {
LatLng carLocation = new LatLng(location.getLatitude(),
location.getLongitude());
map.addMarker(new
MarkerOptions().position(carLocation).title("My Car"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(carLocation,
16));
}
});
}
}
✅ This will show a marker on Google Map at your current (car) location.
Sure! Here's the full and simple XML layout to show Google Map in your app.
No problem! Here's the full employee registration form XML with the necessary UI
components. No Java code is needed for the UI layout part.
</LinearLayout>
✅ Explanation:
This is the UI layout for the Employee Registration form. Let me know if you'd like help
handling form submission or any further features!
Sure! Here's a simple example of how to create a rectangular progress bar in Android.
/>
</LinearLayout>
✅ Explanation:
ProgressBar: A horizontal rectangular progress bar with a max value of 100 and initial
progress set to 50.
indeterminate="false": This makes the progress bar show the actual progress.
max="100": The maximum value for the progress bar.
progress="50": The initial progress of the bar (50%).
You can update the progress of the bar in Java like this:
ProgressBar progressBar = findViewById(R.id.progressBar);
progressBar.setProgress(70); // Set progress to 70
This is a very simple rectangular progress bar. You can change the progress dynamically
based on your needs.
Develop a program to implement : i) List view of 5 items ii) iii) Grid view of 4 × 4 items Image
view.
/>
</LinearLayout>
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.GridView;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView.setImageResource(R.drawable.ic_launcher_foreground); // Change
image when clicked
}
});
}
}
3. Explanation:
1. ListView:
o Displays 5 items using an ArrayAdapter.
o Items: "Item 1", "Item 2", "Item 3", "Item 4", and "Item 5".
2. GridView:
o Displays 16 items in a 4x4 grid (4 columns and 4 rows).
o Items: "Item 1" to "Item 16".
o The grid layout is controlled by the numColumns attribute.
3. ImageView:
o Displays an image (ic_launcher_foreground by default).
o When an item in the GridView is clicked, it will change the image displayed in
the ImageView.
4. Images:
Make sure you have a sample image (like ic_launcher_foreground) in your res/drawable
folder. You can replace it with any image you want.
This is a simple program that shows a ListView, GridView, and ImageView. Let me know if
you need any additional modifications or explanations!
Write a program to find the direction from user’s current location to MSBTE, Bandra. (Write only
.Java and manifest file)
Here’s a simple program that shows the user’s current location and a marker for MSBTE,
Bandra using Google Maps.
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY_HERE" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2. MainActivity.java
package com.example.directions;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.tasks.OnSuccessListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fusedLocationClient =
LocationServices.getFusedLocationProviderClient(this);
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
return;
}
fusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
LatLng userLocation = new
LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new
MarkerOptions().position(userLocation).title("Your Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 14));
3. Explanation:
This is a simple solution showing the user's location and MSBTE on Google Maps.
Here’s how you can develop a simple calculator app using RelativeLayout in Android:
This layout will use RelativeLayout to position buttons and the text view.
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_toEndOf="@id/button1"
android:layout_alignBaseline="@id/button1"
android:layout_marginStart="10dp" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:layout_toEndOf="@id/button2"
android:layout_alignBaseline="@id/button2"
android:layout_marginStart="10dp" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:layout_below="@id/button1"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:layout_toEndOf="@id/button4"
android:layout_alignBaseline="@id/button4"
android:layout_marginStart="10dp" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:layout_toEndOf="@id/button5"
android:layout_alignBaseline="@id/button5"
android:layout_marginStart="10dp" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:layout_below="@id/button4"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:layout_toEndOf="@id/button7"
android:layout_alignBaseline="@id/button7"
android:layout_marginStart="10dp" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:layout_toEndOf="@id/button8"
android:layout_alignBaseline="@id/button8"
android:layout_marginStart="10dp" />
<Button
android:id="@+id/button0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_below="@id/button7"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<Button
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:layout_toEndOf="@id/button0"
android:layout_alignBaseline="@id/button0"
android:layout_marginStart="10dp" />
<Button
android:id="@+id/buttonEquals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:layout_below="@id/button0"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<Button
android:id="@+id/buttonClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:layout_toEndOf="@id/buttonEquals"
android:layout_alignBaseline="@id/buttonEquals"
android:layout_marginStart="10dp" />
</RelativeLayout>
Now, let's write the logic for handling the button clicks and performing calculations.
package com.example.simplecalculator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = findViewById(R.id.result);
// Number buttons
Button button1 = findViewById(R.id.button1);
Button button2 = findViewById(R.id.button2);
Button button3 = findViewById(R.id.button3);
Button button4 = findViewById(R.id.button4);
Button button5 = findViewById(R.id.button5);
Button button6 = findViewById(R.id.button6);
Button button7 = findViewById(R.id.button7);
Button button8 = findViewById(R.id.button8);
Button button9 = findViewById(R.id.button9);
Button button0 = findViewById(R.id.button0);
// Operator buttons
Button buttonAdd = findViewById(R.id.buttonAdd);
// Other buttons
Button buttonEquals = findViewById(R.id.buttonEquals);
Button buttonClear = findViewById(R.id.buttonClear);
switch (operator) {
case "+":
resultValue = firstOperand + secondOperand;
break;
// You can add more operations like -, *, / here
}
result.setText(String.valueOf(resultValue));
currentInput = String.valueOf(resultValue); // For continued
calculation
}
}
Develop a program to add “Hello World” marker at (10,10) co-ordinates. Write only . java file.
Here's a simpler version of how to add "Hello World" at coordinates (10,10) in an Android app
using a custom view:
package com.example.helloworldapp;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Color;
import android.view.View;
When you run the app, you will see the text "Hello World" displayed at coordinates (10, 10) on
the screen.
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="16dp"
android:stretchColumns="1">
To develop an application that displays an analog time picker and shows the selected time, you
can use Android's TimePicker widget. Below is a simple example to achieve this using
TimePickerDialog to select the time and display it.
In the XML layout, we'll use a TimePicker widget and a TextView to show the selected time.
</RelativeLayout>
package com.example.analogtimepicker;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectedTime = findViewById(R.id.selectedTime);
Button timePickerButton = findViewById(R.id.timePickerButton);
timePickerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the current time to set the default values in the
TimePickerDialog
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
// Create a TimePickerDialog
TimePickerDialog timePickerDialog = new TimePickerDialog(
MainActivity.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int
hourOfDay, int minute) {
// Display the selected time in the TextView
selectedTime.setText("Selected Time: " +
hourOfDay + ":" + String.format("%02d", minute));
}
},
hour, minute, true // true for 24-hour format, false
for 12-hour format
);
timePickerDialog.show();
}
});
}
}
Explanation:
1. XML Layout:
o We have a Button that triggers the TimePickerDialog.
o We have a TextView to display the selected time.
2. MainActivity:
o When the "Pick Time" button is clicked, a TimePickerDialog is shown with the
current time as the default.
o The OnTimeSetListener listens for the user selecting a time and updates the
TextView with the selected time in the format HH:MM.
o The true parameter in the TimePickerDialog constructor specifies using a 24-
hour clock. If you prefer a 12-hour format, use false instead.
1. When you run the app, you will see a button to "Pick Time."
2. Upon clicking it, an analog time picker will appear where the user can select the time.
3. After selecting the time, the TextView below the button will display the selected time in
the format HH:MM.
This is a simple and easy-to-understand way to implement an analog time picker and display the
selected time in Android. Let me know if you need any additional help!
Here's a simple program that performs basic arithmetic operations (addition, subtraction,
multiplication, and division) of two numbers and displays the result. The program uses Android's
EditText for user input, Button for operations, and a TextView to display the result.
In the XML layout, we will use EditText to input two numbers, Button for each operation, and
a TextView to display the result.
<Button
android:id="@+id/subtractButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subtract"
android:layout_below="@id/addButton"
android:layout_marginTop="10dp" />
<Button
android:id="@+id/multiplyButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Multiply"
android:layout_below="@id/subtractButton"
android:layout_marginTop="10dp" />
<Button
android:id="@+id/divideButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Divide"
android:layout_below="@id/multiplyButton"
android:layout_marginTop="10dp" />
In MainActivity.java, we will implement the logic to perform the arithmetic operations and
display the result.
package com.example.simplecalculator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switch (operation) {
case "add":
result = number1 + number2;
break;
case "subtract":
result = number1 - number2;
break;
case "multiply":
result = number1 * number2;
break;
case "divide":
if (number2 != 0) {
result = number1 / number2;
} else {
resultText.setText("Error: Cannot divide by zero");
return;
}
break;
}
Explanation:
1. XML Layout:
o EditText: For entering the first and second numbers.
o Button: For performing the arithmetic operations (add, subtract, multiply,
divide).
o TextView: To display the result.
2. MainActivity:
o The onCreate method initializes the views and sets OnClickListener for each
button.
o The performOperation method is called when any operation button is clicked. It
retrieves the numbers from the EditText fields, performs the selected operation,
and displays the result in the TextView.
o We check for division by zero to avoid a crash.
This is a simple and easy-to-understand way to implement a calculator app with basic arithmetic
operations. Let me know if you need any further assistance!
Here's a simplified version of how to display a Google Map in your Android app.
1. Get your Google Maps API key from Google Cloud Console.
2. Add the API key to res/values/google_maps_api.xml:
<resources>
<string name="google_maps_key">YOUR_API_KEY</string>
</resources>
Add the following permissions and metadata for the API key:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapexample">
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="Google Maps"
android:theme="@style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.example.mapexample;
import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
dependencies {
implementation 'com.google.android.gms:play-services-maps:17.0.0'
}
This setup will get a basic map running with a marker. Let me know if you need further
clarification!
Develop a program to TURN ON and OFF bluetooth. Write .java file and
permission tags
Here is a simple Android app to turn Bluetooth ON and OFF with minimal code.
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.DayNight">
<activity
android:name=".MainActivity"
android:label="Bluetooth Control"
android:theme="@style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
This Java file contains the logic to turn Bluetooth ON and OFF.
package com.example.bluetoothapp;
import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get BluetoothAdapter
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// Enable Bluetooth
btnEnableBluetooth.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!bluetoothAdapter.isEnabled()) {
bluetoothAdapter.enable();
Toast.makeText(MainActivity.this, "Bluetooth ON",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Bluetooth is already
ON", Toast.LENGTH_SHORT).show();
}
}
});
// Disable Bluetooth
btnDisableBluetooth.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bluetoothAdapter.isEnabled()) {
bluetoothAdapter.disable();
Toast.makeText(MainActivity.this, "Bluetooth OFF",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Bluetooth is already
OFF", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Design a simple layout with two buttons for turning Bluetooth ON and OFF.
Explanation:
Run this app on a physical Android device (since Bluetooth doesn't work on emulators).
The app will display two buttons:
o Turn ON Bluetooth: Turns Bluetooth on if it is off.
o Turn OFF Bluetooth: Turns Bluetooth off if it is on.
That's it! This is the simplest way to control Bluetooth in an Android app. Let me know if you
need more help!