Practical 8 :
Que: 1:
Ans :
a. Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="16dp"
android:hint="Select Subject" />
</RelativeLayout>
b. MainActivity.java
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private String[] subjects = {"Subject 1", "Subject 2", "Subject 3", "Subject 4", "Subject 5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AutoCompleteTextView autoCompleteTextView = findViewById(R.id.autoCompleteTextView);
// Create an ArrayAdapter with the subjects and set it to the AutoCompleteTextView
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, subjects);
autoCompleteTextView.setAdapter(adapter);
// Optional: Set a threshold for the number of characters to start filtering
autoCompleteTextView.setThreshold(1);
// You can add additional listeners or actions as needed
}
Practical 9 :
Que: 1 :
Ans :
a. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Bluetooth Off"
android:textOn="Bluetooth On"
android:layout_centerInParent="true" />
</RelativeLayout>
b. MainActivity.java
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.ToggleButton;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private BluetoothAdapter bluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the default Bluetooth adapter
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
// Device does not support Bluetooth
Toast.makeText(this, "Bluetooth is not supported on this device",
Toast.LENGTH_SHORT).show();
finish();
ToggleButton toggleButton = findViewById(R.id.toggleButton);
toggleButton.setChecked(bluetoothAdapter.isEnabled());
toggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleBluetooth();
});
private void toggleBluetooth() {
if (bluetoothAdapter.isEnabled()) {
// Bluetooth is currently enabled, so disable it
bluetoothAdapter.disable();
Toast.makeText(this, "Bluetooth turned off", Toast.LENGTH_SHORT).show();
} else {
// Bluetooth is currently disabled, so enable it
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
// Note: You can replace "1" with any positive integer as a request code
}
Practical 10 :
Que: 1:
Ans:
a. activity_main.xml
<!-- res/layout/activity_login.xml -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextUsername"
android:layout_marginTop="16dp"
android:inputType="textPassword"
android:hint="Password" />
<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextPassword"
android:layout_marginTop="16dp"
android:text="Login"
android:onClick="onLoginClick" />
<TextView
android:id="@+id/textViewRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonLogin"
android:layout_marginTop="16dp"
android:text="Don't have an account? Register here."
android:textColor="@android:color/holo_blue_dark"
android:onClick="onRegisterClick" />
</RelativeLayout>
b. MainActivity.java
package com.example.yourappname;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editTextUsername;
private EditText editTextPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
editTextUsername = findViewById(R.id.editTextUsername);
editTextPassword = findViewById(R.id.editTextPassword);
}
public void onLoginClick(View view) {
String username = editTextUsername.getText().toString();
String password = editTextPassword.getText().toString();
if (isValidCredentials(username, password)) {
Toast.makeText(this, "Login successful", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, HomeActivity.class);
startActivity(intent);
finish(); // Close the login activity
} else {
Toast.makeText(this, "Invalid credentials", Toast.LENGTH_SHORT).show();
public void onRegisterClick(View view) {
Intent intent = new Intent(this, RegisterActivity.class);
startActivity(intent);
private boolean isValidCredentials(String username, String password) {
return username.equals("demo") && password.equals("password");
}
Que: 2:
Ans:
a. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextUsername"
android:layout_marginTop="16dp"
android:inputType="textPassword"
android:hint="Password" />
<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextPassword"
android:layout_marginTop="16dp"
android:text="Login"
android:onClick="onLoginClick" />
</RelativeLayout>
b. MainActivity.java
package com.example.yourappname;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editTextUsername;
private EditText editTextPassword;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
editTextUsername = findViewById(R.id.editTextUsername);
editTextPassword = findViewById(R.id.editTextPassword);
public void onLoginClick(View view) {
String username = editTextUsername.getText().toString();
String password = editTextPassword.getText().toString();
if (isValidCredentials(username, password)) {
Toast.makeText(this, "Login successful", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, StudentDashboardActivity.class);
startActivity(intent);
finish(); // Close the login activity
} else {
Toast.makeText(this, "Invalid credentials", Toast.LENGTH_SHORT).show();
private boolean isValidCredentials(String username, String password) {
return username.equals("student") && password.equals("password"); } }
Practical 11:
Que: 1:
Ans:
a. acitivity_main.xml
<!-- res/layout/activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<CheckBox
android:id="@+id/checkBoxInbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inbox 1"/>
<CheckBox
android:id="@+id/checkBoxInbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inbox 2"/>
<CheckBox
android:id="@+id/checkBoxInbox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inbox 3"/>
<CheckBox
android:id="@+id/checkBoxInbox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inbox 4"/>
<CheckBox
android:id="@+id/checkBoxInbox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inbox 5"/>
</LinearLayout>
b. MainActivity.java
// src/main/java/com.example.yourappname/MainActivity.java
package com.example.yourappname;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// You can perform additional initialization here if needed
public void onCheckboxClicked(View view) {
// Handle checkbox click events
CheckBox checkBox = (CheckBox) view;
String inboxName = checkBox.getText().toString();
// You can perform additional actions based on the checkbox state
if (checkBox.isChecked()) {
Toast.makeText(this, "Inbox " + inboxName + " selected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Inbox " + inboxName + " deselected",
Toast.LENGTH_SHORT).show();
}
Practical 12:
Que: 1:
Ans:
a. activity_main.xml
<!-- res/layout/activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<!-- Heading for checkboxes -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Five Inboxes"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginBottom="16dp"/>
<!-- Checkboxes -->
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inbox 1"/>
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inbox 2"/>
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inbox 3"/>
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inbox 4"/>
<CheckBox
android:id="@+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inbox 5"/>
<!-- Heading for radio buttons -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Gender"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"/>
<!-- Radio Group with Male and Female radio buttons -->
<RadioGroup
android:id="@+id/radioGroupGender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioButtonMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="@+id/radioButtonFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
</LinearLayout>
b. MainActivity.xml
// src/main/java/com.example.yourappname/MainActivity.java
package com.example.yourappname;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Checkbox listeners
CheckBox checkBox1 = findViewById(R.id.checkBox1);
CheckBox checkBox2 = findViewById(R.id.checkBox2);
CheckBox checkBox3 = findViewById(R.id.checkBox3);
CheckBox checkBox4 = findViewById(R.id.checkBox4);
CheckBox checkBox5 = findViewById(R.id.checkBox5);
checkBox1.setOnCheckedChangeListener(new CheckboxListener());
checkBox2.setOnCheckedChangeListener(new CheckboxListener());
checkBox3.setOnCheckedChangeListener(new CheckboxListener());
checkBox4.setOnCheckedChangeListener(new CheckboxListener());
checkBox5.setOnCheckedChangeListener(new CheckboxListener());
// Radio Group listener
RadioGroup radioGroupGender = findViewById(R.id.radioGroupGender);
radioGroupGender.setOnCheckedChangeListener(new RadioGroupListener());
private class CheckboxListener implements CompoundButton.OnCheckedChangeListener {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Handle checkbox state changes
String inboxName = buttonView.getText().toString();
if (isChecked) {
// Checkbox is checked, perform action
// (e.g., add the selected inbox to a list)
} else {
// Checkbox is unchecked, perform action
// (e.g., remove the unselected inbox from the list)
private class RadioGroupListener implements RadioGroup.OnCheckedChangeListener {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// Handle radio button state changes
RadioButton radioButton = findViewById(checkedId);
String gender = radioButton.getText().toString();
// Use the selected gender (e.g., "Male" or "Female")