0% found this document useful (0 votes)
2 views

output part 1 - Google Docs

The document contains multiple Java and XML code snippets for Android applications, including a circular progress bar, activity lifecycle logging, and an AutoCompleteTextView for user input. Each code section demonstrates different functionalities such as Bluetooth toggling, displaying progress, and handling user interactions. The examples are structured to show both the Java logic and the corresponding XML layout for the user interface.

Uploaded by

Dank Mukund
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)
2 views

output part 1 - Google Docs

The document contains multiple Java and XML code snippets for Android applications, including a circular progress bar, activity lifecycle logging, and an AutoCompleteTextView for user input. Each code section demonstrates different functionalities such as Bluetooth toggling, displaying progress, and handling user interactions. The examples are structured to show both the Java logic and the corresponding XML layout for the user interface.

Uploaded by

Dank Mukund
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/ 21

‭_________________________________________________________________________‬

‭ age: 66‬
P
‭JAVA CODE:‬

‭package com.example.circularprogressbar;‬

i‭mport android.os.Bundle;‬
‭import android.os.Handler;‬
‭import android.widget.ProgressBar;‬
‭import android.widget.TextView;‬
‭import androidx.appcompat.app.AppCompatActivity;‬

‭public class MainActivity extends AppCompatActivity {‬


‭private ProgressBar progressBar;‬
‭private TextView progressText;‬
‭private int progressStatus = 0;‬
‭private Handler handler = new Handler();‬

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

‭ rogressBar = findViewById(R.id.progressBar);‬
p
‭progressText = findViewById(R.id.progressText);‬

‭new Thread(new Runnable() {‬


‭@Override‬
‭public void run() {‬
‭while (progressStatus < 100) {‬
‭progressStatus += 1;‬
‭handler.post(new Runnable() {‬
‭@Override‬
‭public void run() {‬
‭progressBar.setProgress(progressStatus);‬
‭progressText.setText(progressStatus + "%");‬
‭}‬
‭});‬
‭try {‬
‭Thread.sleep(100);‬
‭} catch (InterruptedException e) {‬
‭e.printStackTrace();‬
‭}‬
‭}‬
‭}‬
‭}).start();‬
‭}‬
‭}‬

‭XML CODE:‬

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

‭_________________________________________________________________________‬
‭_________________________________________________________________________‬

‭<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"‬
‭android:layout_width="fill_parent"‬
‭android:layout_height="fill_parent"‬
‭android:paddingLeft="16dp"‬
‭android:paddingRight="16dp" >‬

‭<ProgressBar‬
‭android:id="@+id/progressBar"‬
‭android:layout_width="wrap_content"‬
‭android:layout_height="wrap_content"‬
‭android:layout_centerInParent="true"‬
‭style="?android:attr/progressBarStyleLarge"‬
‭android:max="100"/>‬

‭<TextView‬
‭android:id="@+id/progressText"‬
‭android:layout_width="wrap_content"‬
‭android:layout_height="wrap_content"‬
‭android:text="0%"‬
‭android:textSize="20sp"‬
‭android:layout_centerInParent="true"/>‬

‭</RelativeLayout>‬

‭OUTPUT:‬

‭_________________________________________________________________________‬
‭_________________________________________________________________________‬

‭Page: 91‬

J‭ AVA CODE:‬
‭package com.example.demo;‬

i‭mport android.os.Bundle;‬
‭import android.util.Log;‬
‭import androidx.appcompat.app.AppCompatActivity;‬

‭public class MainActivity extends AppCompatActivity {‬


‭private static final String TAG = "Lifecycle";‬

‭ Override‬
@
‭protected void onCreate(Bundle savedInstanceState) {‬
‭super.onCreate(savedInstanceState);‬
‭setContentView(R.layout.activity_main);‬
‭Log.d(TAG, "onCreate: Activity is being created");‬
‭}‬

‭ Override‬
@
‭protected void onStart() {‬
‭super.onStart();‬
‭Log.d(TAG, "onStart: Activity is becoming visible");‬
‭}‬

‭ Override‬
@
‭protected void onResume() {‬
‭super.onResume();‬
‭Log.d(TAG, "onResume: Activity is now interactive");‬
‭}‬

‭ Override‬
@
‭protected void onPause() {‬
‭super.onPause();‬
‭Log.d(TAG, "onPause: Activity is partially obscured");‬
‭}‬

‭ Override‬
@
‭protected void onStop() {‬
‭super.onStop();‬
‭Log.d(TAG, "onStop: Activity is no longer visible");‬
‭}‬

‭ Override‬
@
‭protected void onRestart() {‬
‭super.onRestart();‬
‭Log.d(TAG, "onRestart: Activity is restarting");‬
‭}‬

‭ Override‬
@
‭protected void onDestroy() {‬
‭super.onDestroy();‬

‭_________________________________________________________________________‬
‭_________________________________________________________________________‬

‭Log.d(TAG, "onDestroy: Activity is being destroyed");‬


‭}‬
‭}‬

‭ ML CODE:‬
X
‭<?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"‬
‭tools:context=".MainActivity">‬

‭<TextView‬
‭android:layout_width="wrap_content"‬
‭android:layout_height="wrap_content"‬
‭android:text="Hello World!"‬
‭android:textSize="24sp"‬
‭android:layout_gravity="center"‬
‭android:layout_marginTop="16dp" />‬

‭</LinearLayout>‬

‭OUTPUT:‬

‭_________________________________________________________________________‬
‭_________________________________________________________________________‬

‭Page: 38‬

‭JAVA CODE:‬

‭ ackage com.example.demo;‬
p
‭import androidx.appcompat.app.AppCompatActivity;‬
‭import android.os.Bundle;‬
‭import android.widget.ArrayAdapter;‬
‭import android.widget.AutoCompleteTextView;‬
‭import java.util.ArrayList;‬
‭import java.util.List;‬
‭public class MainActivity extends AppCompatActivity {‬

‭ rivate AutoCompleteTextView searchAutoCompleteTextView;‬


p
‭private List<String> suggestionList;‬
‭private ArrayAdapter<String> adapter;‬

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

/‭/ Initialize the AutoCompleteTextView‬


‭searchAutoCompleteTextView = findViewById(R.id.searchAutoCompleteTextView);‬

/‭/ Create a list of initial suggestions (replace with your actual data)‬
‭suggestionList = new ArrayList<>();‬
‭suggestionList.add("Java Programming");‬
‭suggestionList.add("Android Development");‬
‭suggestionList.add("Python for Data Science");‬
‭suggestionList.add("Web Development Tutorial");‬
‭suggestionList.add("Machine Learning Basics");‬
‭suggestionList.add("Artificial Intelligence Course");‬
‭suggestionList.add("Mobile App Design");‬
‭suggestionList.add("Database Management Systems");‬
‭suggestionList.add("Competitive Programming");‬

/‭/ Create an ArrayAdapter to bind the suggestions to the AutoCompleteTextView‬


‭adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,‬
‭suggestionList);‬

/‭/ Set the adapter to the AutoCompleteTextView‬


‭searchAutoCompleteTextView.setAdapter(adapter);‬

/‭/ You can optionally set a threshold for when suggestions start appearing‬
‭// Default is 1, meaning suggestions appear after the first character is typed.‬
‭// searchAutoCompleteTextView.setThreshold(2);‬
‭} }‬
‭ ML CODE:‬
X

‭ ?xml version="1.0" encoding="utf-8"?>‬


<
‭<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"‬

‭_________________________________________________________________________‬
‭_________________________________________________________________________‬

x‭ mlns: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">‬

‭<TextView‬
‭android:layout_width="wrap_content"‬
‭android:layout_height="wrap_content"‬
‭android:text="Search Engine"‬
‭android:textSize="24sp"‬
‭android:layout_gravity="center_horizontal"‬
‭android:layout_marginBottom="16dp" />‬

‭<AutoCompleteTextView‬
‭android:id="@+id/searchAutoCompleteTextView"‬
‭android:layout_width="match_parent"‬
‭android:layout_height="wrap_content"‬
‭android:hint="Enter your search query"‬
‭android:completionThreshold="1" />‬

‭</LinearLayout>‬

‭OUTPUT:‬

‭_________________________________________________________________________‬
‭_________________________________________________________________________‬

‭Page: 39‬

J‭ AVA CODE:‬
‭package com.example.demo;‬

i‭mport androidx.appcompat.app.AppCompatActivity;‬
‭import android.os.Bundle;‬
‭import android.widget.ArrayAdapter;‬
‭import android.widget.AutoCompleteTextView;‬
‭import java.util.ArrayList;‬
‭import java.util.List;‬
‭public class MainActivity extends AppCompatActivity {‬
‭private AutoCompleteTextView subjectAutoCompleteTextView;‬
‭private List<String> sixthSemesterSubjects;‬
‭private ArrayAdapter<String> adapter;‬
‭@Override‬
‭protected void onCreate(Bundle savedInstanceState) {‬
‭super.onCreate(savedInstanceState);‬
‭setContentView(R.layout.activity_main);‬

/‭/ Initialize the AutoCompleteTextView‬


‭subjectAutoCompleteTextView = findViewById(R.id.subjectAutoCompleteTextView);‬

/‭/ Create a list of sixth semester subjects (replace with your actual subjects)‬
‭sixthSemesterSubjects = new ArrayList<>();‬
‭sixthSemesterSubjects.add("Software Engineering");‬
‭sixthSemesterSubjects.add("Computer Networks");‬
‭sixthSemesterSubjects.add("Database Management Systems");‬
‭sixthSemesterSubjects.add("Operating Systems");‬
‭sixthSemesterSubjects.add("Web Technology");‬
‭sixthSemesterSubjects.add("Data Mining and Warehousing");‬
‭sixthSemesterSubjects.add("Mobile Application Development");‬
‭sixthSemesterSubjects.add("Cryptography and Network Security");‬
‭sixthSemesterSubjects.add("Artificial Intelligence");‬
‭sixthSemesterSubjects.add("Cloud Computing");‬

/‭/ Create an ArrayAdapter to bind the subjects to the AutoCompleteTextView‬


‭adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,‬
‭sixthSemesterSubjects);‬

/‭/ Set the adapter to the AutoCompleteTextView‬


‭subjectAutoCompleteTextView.setAdapter(adapter);‬

/‭/ Optionally set a threshold for when suggestions start appearing‬


‭subjectAutoCompleteTextView.setThreshold(1); // Suggestions start after the first character‬
‭}‬
}‭ ‬
‭XML CODE:‬
‭<?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"‬

‭_________________________________________________________________________‬
‭_________________________________________________________________________‬

‭ ndroid:layout_height="match_parent"‬
a
‭android:orientation="vertical"‬
‭android:padding="16dp"‬
‭tools:context=".MainActivity">‬

‭<TextView‬
‭android:layout_width="wrap_content"‬
‭android:layout_height="wrap_content"‬
‭android:text="Sixth Semester Subjects"‬
‭android:textSize="24sp"‬
‭android:layout_gravity="center_horizontal"‬
‭android:layout_marginBottom="16dp" />‬

‭<AutoCompleteTextView‬
‭android:id="@+id/subjectAutoCompleteTextView"‬
‭android:layout_width="match_parent"‬
‭android:layout_height="wrap_content"‬
‭android:hint="Enter subject name"‬
‭android:completionThreshold="1" />‬

‭</LinearLayout>‬

‭OUTPUT:‬

‭_________________________________________________________________________‬
‭_________________________________________________________________________‬

‭Page: 45‬

J‭ AVA CODE:‬
‭package com.example.bluetoothtoggle;‬

i‭mport androidx.appcompat.app.AppCompatActivity;‬
‭import android.bluetooth.BluetoothAdapter;‬
‭import android.content.Intent;‬
‭import android.os.Bundle;‬
‭import android.widget.CompoundButton;‬
‭import android.widget.TextView;‬
‭import android.widget.ToggleButton;‬

‭public class MainActivity extends AppCompatActivity {‬

‭ rivate ToggleButton bluetoothToggleButton;‬


p
‭private TextView bluetoothStatusTextView;‬
‭private BluetoothAdapter bluetoothAdapter;‬
‭private static final int REQUEST_ENABLE_BT = 1;‬

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

‭ luetoothToggleButton = findViewById(R.id.bluetoothToggleButton);‬
b
‭bluetoothStatusTextView = findViewById(R.id.bluetoothStatusTextView);‬
‭bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();‬

/‭/ Set initial status to "Unknown"‬


‭bluetoothStatusTextView.setText("Bluetooth Status: Unknown");‬

/‭/ Check if Bluetooth is supported on the device‬


‭if (bluetoothAdapter == null) {‬
‭bluetoothStatusTextView.setText("Bluetooth is not supported on this device.");‬
‭bluetoothToggleButton.setEnabled(false);‬
‭} else {‬
‭// Set the initial state of the toggle button and text view‬
‭updateBluetoothState();‬

/‭/ Set a listener for the toggle button‬


‭bluetoothToggleButton.setOnCheckedChangeListener(new‬
‭CompoundButton.OnCheckedChangeListener() {‬
‭public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {‬
‭if (isChecked) {‬
‭// The toggle is enabled, try to turn on Bluetooth‬
‭if (!bluetoothAdapter.isEnabled()) {‬
‭Intent enableBtIntent = new‬
‭Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);‬
‭startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);‬
‭// Note: The state will be updated in onActivityResult‬
‭} else {‬

‭_________________________________________________________________________‬
‭_________________________________________________________________________‬

‭updateBluetoothState(); // Already enabled‬


}‭ ‬
‭} else {‬
‭// The toggle is disabled, try to turn off Bluetooth‬
‭if (bluetoothAdapter.isEnabled()) {‬
‭bluetoothAdapter.disable();‬
‭updateBluetoothState();‬
‭} else {‬
‭updateBluetoothState(); // Already disabled‬
‭}‬
‭}‬
}‭ ‬
‭});‬
‭}‬
‭}‬

‭ Override‬
@
‭protected void onActivityResult(int requestCode, int resultCode, Intent data) {‬
‭super.onActivityResult(requestCode, resultCode, data);‬
‭if (requestCode == REQUEST_ENABLE_BT) {‬
‭if (resultCode == RESULT_OK) {‬
‭// Bluetooth has been enabled‬
‭updateBluetoothState();‬
‭} else if (resultCode == RESULT_CANCELED) {‬
‭// User declined to enable Bluetooth, revert the toggle state‬
‭bluetoothToggleButton.setChecked(false);‬
‭bluetoothStatusTextView.setText("Bluetooth enabling was cancelled.");‬
‭}‬
‭}‬
‭}‬

‭private void updateBluetoothState() {‬


‭if (bluetoothAdapter != null) {‬
‭if (bluetoothAdapter.isEnabled()) {‬
‭bluetoothToggleButton.setChecked(true);‬
‭bluetoothStatusTextView.setText("Bluetooth Status: ON");‬
‭} else {‬
‭bluetoothToggleButton.setChecked(false);‬
‭bluetoothStatusTextView.setText("Bluetooth Status: OFF");‬
‭}‬
‭}‬
‭}‬
‭}‬

‭ ML CODE:‬
X
‭<?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"‬

‭_________________________________________________________________________‬
‭_________________________________________________________________________‬

‭ ndroid:gravity="center"‬
a
‭android:padding="16dp"‬
‭tools:context=".MainActivity">‬

‭<TextView‬
‭android:layout_width="wrap_content"‬
‭android:layout_height="wrap_content"‬
‭android:text="Bluetooth Toggle"‬
‭android:textSize="24sp"‬
‭android:layout_marginBottom="32dp" />‬

‭<ToggleButton‬
‭android:id="@+id/bluetoothToggleButton"‬
‭android:layout_width="wrap_content"‬
‭android:layout_height="wrap_content"‬
‭android:textOff="OFF"‬
‭android:textOn="ON"‬
‭android:textAllCaps="true" />‬

‭<TextView‬
‭android:id="@+id/bluetoothStatusTextView"‬
‭android:layout_width="wrap_content"‬
‭android:layout_height="wrap_content"‬
‭android:layout_marginTop="16dp"‬
‭android:text="Bluetooth Status: Unknown"‬
‭android:textSize="18sp" />‬

‭</LinearLayout>‬

‭OUTPUT:‬

J‭ AVA CODE:‬
‭package com.example.simplecalculator;‬

i‭mport androidx.appcompat.app.AppCompatActivity;‬
‭import android.os.Bundle;‬
‭import android.text.TextUtils;‬
‭import android.view.View;‬
‭import android.widget.Button;‬
‭import android.widget.EditText;‬
‭import android.widget.TextView;‬
‭import android.widget.Toast;‬
‭public class MainActivity extends AppCompatActivity implements View.OnClickListener {‬

‭_________________________________________________________________________‬
‭_________________________________________________________________________‬

‭ rivate EditText editTextFirstNumber;‬


p
‭private EditText editTextSecondNumber;‬
‭private Button buttonAdd;‬
‭private Button buttonSubtract;‬
‭private Button buttonMultiply;‬
‭private Button buttonDivide;‬
‭private TextView textViewResult;‬
‭@Override‬
‭protected void onCreate(Bundle savedInstanceState) {‬
‭super.onCreate(savedInstanceState);‬
‭setContentView(R.layout.activity_main);‬

‭ ditTextFirstNumber = findViewById(R.id.editTextFirstNumber);‬
e
‭editTextSecondNumber = findViewById(R.id.editTextSecondNumber);‬
‭buttonAdd = findViewById(R.id.buttonAdd);‬
‭buttonSubtract = findViewById(R.id.buttonSubtract);‬
‭buttonMultiply = findViewById(R.id.buttonMultiply);‬
‭buttonDivide = findViewById(R.id.buttonDivide);‬
‭textViewResult = findViewById(R.id.textViewResult);‬

‭ uttonAdd.setOnClickListener(this);‬
b
‭buttonSubtract.setOnClickListener(this);‬
‭buttonMultiply.setOnClickListener(this);‬
‭buttonDivide.setOnClickListener(this);‬
‭}‬

‭ Override‬
@
‭public void onClick(View v) {‬
‭String num1Str = editTextFirstNumber.getText().toString();‬
‭String num2Str = editTextSecondNumber.getText().toString();‬

‭if (TextUtils.isEmpty(num1Str) || TextUtils.isEmpty(num2Str)) {‬


‭Toast.makeText(this, "Please enter both numbers", Toast.LENGTH_SHORT).show();‬
‭return;‬
‭}‬

‭ ouble num1 = Double.parseDouble(num1Str);‬


d
‭double num2 = Double.parseDouble(num2Str);‬
‭double result = 0;‬

‭int id = v.getId();‬

‭if (id == R.id.buttonAdd) {‬


‭result = num1 + num2;‬
‭} else if (id == R.id.buttonSubtract) {‬
‭result = num1 - num2;‬
‭} else if (id == R.id.buttonMultiply) {‬
‭result = num1 * num2;‬
‭} else if (id == R.id.buttonDivide) {‬
‭if (num2 == 0) {‬
‭Toast.makeText(this, "Cannot divide by zero", Toast.LENGTH_SHORT).show();‬
‭return;‬

‭_________________________________________________________________________‬
‭_________________________________________________________________________‬

}‭ ‬
‭result = num1 / num2;‬
}‭ ‬
‭textViewResult.setText("Result: " + String.format("%.2f", result)); // Format to 2 decimal places‬
‭}‬
‭}‬

‭ ML CODE:‬
X
‭<?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">‬
‭<TextView‬
‭android:layout_width="wrap_content"‬
‭android:layout_height="wrap_content"‬
‭android:text="Simple Calculator"‬
‭android:textSize="24sp"‬
‭android:layout_gravity="center_horizontal"‬
‭android:layout_marginBottom="16dp" />‬
‭<EditText‬
‭android:id="@+id/editTextFirstNumber"‬
‭android:layout_width="match_parent"‬
‭android:layout_height="wrap_content"‬
‭android:hint="Enter First Number"‬
‭android:inputType="numberDecimal"‬
‭android:layout_marginBottom="8dp" />‬
‭<EditText‬
‭android:id="@+id/editTextSecondNumber"‬
‭android:layout_width="match_parent"‬
‭android:layout_height="wrap_content"‬
‭android:hint="Enter Second Number"‬
‭android:inputType="numberDecimal"‬
‭android:layout_marginBottom="16dp" />‬

‭<LinearLayout‬
‭android:layout_width="match_parent"‬
‭android:layout_height="wrap_content"‬
‭android:orientation="horizontal"‬
‭android:gravity="center">‬
‭<Button‬
‭android:id="@+id/buttonAdd"‬
‭android:layout_width="0dp"‬
‭android:layout_height="wrap_content"‬
‭android:layout_weight="1"‬
‭android:text="+"‬
‭android:layout_marginEnd="4dp" />‬
‭<Button‬
‭android:id="@+id/buttonSubtract"‬

‭_________________________________________________________________________‬
‭_________________________________________________________________________‬

‭ ndroid:layout_width="0dp"‬
a
‭android:layout_height="wrap_content"‬
‭android:layout_weight="1"‬
‭android:text="-"‬
‭android:layout_marginEnd="4dp"‬
‭android:layout_marginStart="4dp" />‬
‭<Button‬
‭android:id="@+id/buttonMultiply"‬
‭android:layout_width="0dp"‬
‭android:layout_height="wrap_content"‬
‭android:layout_weight="1"‬
‭android:text="x"‬
‭android:layout_marginEnd="4dp"‬
‭android:layout_marginStart="4dp" />‬
‭<Button‬
‭android:id="@+id/buttonDivide"‬
‭android:layout_width="0dp"‬
‭android:layout_height="wrap_content"‬
‭android:layout_weight="1"‬
‭android:text="/"‬
‭android:layout_marginStart="4dp" />‬
‭</LinearLayout>‬
‭<TextView‬
‭android:id="@+id/textViewResult"‬
‭android:layout_width="wrap_content"‬
‭android:layout_height="wrap_content"‬
‭android:text="Result: "‬
‭android:textSize="18sp"‬
‭android:layout_marginTop="24dp" />‬
‭ /LinearLayout>‬
<

‭OUTPUT:‬

‭_________________________________________________________________________‬
‭_________________________________________________________________________‬

‭Page: 56‬

J‭ AVA CODE:‬
‭package com.example.checkboxexample;‬

‭import androidx.appcompat.app.AppCompatActivity;‬

i‭mport android.os.Bundle;‬
‭import android.view.View;‬
‭import android.widget.Button;‬
‭import android.widget.CheckBox;‬
‭import android.widget.Toast;‬
‭import java.util.ArrayList;‬
‭import java.util.List;‬

‭public class MainActivity extends AppCompatActivity {‬


‭private CheckBox checkBox1;‬
‭private CheckBox checkBox2;‬
‭private CheckBox checkBox3;‬
‭private CheckBox checkBox4;‬
‭private CheckBox checkBox5;‬
‭private Button submitButton;‬
‭@Override‬
‭protected void onCreate(Bundle savedInstanceState) {‬
‭super.onCreate(savedInstanceState);‬
‭setContentView(R.layout.activity_main);‬
‭checkBox1 = findViewById(R.id.checkBox1);‬
‭checkBox2 = findViewById(R.id.checkBox2);‬
‭checkBox3 = findViewById(R.id.checkBox3);‬
‭checkBox4 = findViewById(R.id.checkBox4);‬
‭checkBox5 = findViewById(R.id.checkBox5);‬
‭submitButton = findViewById(R.id.submitButton);‬
‭submitButton.setOnClickListener(new View.OnClickListener() {‬
‭@Override‬
‭public void onClick(View v) {‬
‭List<String> selectedOptions = new ArrayList<>();‬
‭if (checkBox1.isChecked()) selectedOptions.add("Option 1");‬
‭if (checkBox2.isChecked()) selectedOptions.add("Option 2");‬
‭if (checkBox3.isChecked()) selectedOptions.add("Option 3");‬
‭if (checkBox4.isChecked()) selectedOptions.add("Option 4");‬
‭if (checkBox5.isChecked()) selectedOptions.add("Option 5");‬
‭if (selectedOptions.isEmpty()) {‬
‭Toast.makeText(MainActivity.this, "No options selected",‬
‭Toast.LENGTH_SHORT).show();‬
‭} else {‬
‭Toast.makeText(MainActivity.this, "Selected options: " + String.join(", ", selectedOptions),‬
‭Toast.LENGTH_SHORT).show();‬
‭}‬
‭} }); }}‬

‭_________________________________________________________________________‬
‭_________________________________________________________________________‬

‭ ML CODE:‬
X
‭<?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="wrap_content"‬
‭android:orientation="vertical"‬
‭android:padding="16dp"‬
‭tools:context=".MainActivity">‬
‭<CheckBox‬
‭android:id="@+id/checkBox1"‬
‭android:layout_width="wrap_content"‬
‭android:layout_height="wrap_content"‬
‭android:text="Option 1" />‬
‭<CheckBox‬
‭android:id="@+id/checkBox2"‬
‭android:layout_width="wrap_content"‬
‭android:layout_height="wrap_content"‬
‭android:text="Option 2" />‬
‭<CheckBox‬
‭android:id="@+id/checkBox3"‬
‭android:layout_width="wrap_content"‬
‭android:layout_height="wrap_content"‬
‭android:text="Option 3" />‬
‭<CheckBox‬
‭android:id="@+id/checkBox4"‬
‭android:layout_width="wrap_content"‬
‭android:layout_height="wrap_content"‬
‭android:text="Option 4" />‬
‭<CheckBox‬
‭android:id="@+id/checkBox5"‬
‭android:layout_width="wrap_content"‬
‭android:layout_height="wrap_content"‬
‭android:text="Option 5" />‬
‭<Button‬
‭android:id="@+id/submitButton"‬
‭android:layout_width="wrap_content"‬
‭android:layout_height="wrap_content"‬
‭android:text="Submit"‬
‭android:layout_marginTop="16dp" />‬

‭</LinearLayout>‬

‭OUTPUT:‬

‭_________________________________________________________________________‬
‭_________________________________________________________________________‬

‭ age: 79‬
P
‭JAVA CODE:‬
‭package com.example.toastmessage;‬

‭import androidx.appcompat.app.AppCompatActivity;‬

i‭mport android.os.Bundle;‬
‭import android.view.Gravity;‬
‭import android.view.LayoutInflater;‬
‭import android.view.View;‬
‭import android.view.ViewGroup;‬
‭import android.widget.Button;‬
‭import android.widget.TextView;‬
‭import android.widget.Toast;‬

‭public class MainActivity extends AppCompatActivity {‬

‭private Button showToastButton;‬

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

s‭ howToastButton = findViewById(R.id.showToastButton);‬
‭showToastButton.setOnClickListener(new View.OnClickListener() {‬
‭@Override‬
‭public void onClick(View v) {‬
‭showCustomToast("Message for you:\nYou have got mail!");‬
‭}‬
‭});‬
‭}‬

‭private void showCustomToast(String message) {‬


‭LayoutInflater inflater = getLayoutInflater();‬
‭View layout = inflater.inflate(R.layout.custom_toast,‬
‭(ViewGroup) findViewById(R.id.custom_toast_layout));‬

‭ extView text = layout.findViewById(R.id.toast_text);‬


T
‭text.setText(message);‬

‭ oast toast = new Toast(getApplicationContext());‬


T
‭toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);‬
‭toast.setDuration(Toast.LENGTH_LONG);‬
‭toast.setView(layout);‬
‭toast.show();‬
‭}‬
‭}‬

‭_________________________________________________________________________‬
‭_________________________________________________________________________‬

‭XML CODE:‬

‭ ?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:gravity="center"‬
‭android:orientation="vertical"‬
‭android:padding="16dp"‬
‭tools:context=".MainActivity">‬

‭<TextView‬
‭android:layout_width="wrap_content"‬
‭android:layout_height="wrap_content"‬
‭android:text="Toast Example"‬
‭android:textSize="24sp"‬
‭android:layout_marginBottom="16dp" />‬

‭<Button‬
‭android:id="@+id/showToastButton"‬
‭android:layout_width="wrap_content"‬
‭android:layout_height="wrap_content"‬
‭android:text="Show Toast" />‬

‭</LinearLayout>‬

‭_________________________________________________________________________‬
‭_________________________________________________________________________‬

‭Page: 85‬

J‭ AVA CODE:‬
‭package com.example.datetimepicker;‬

‭import androidx.appcompat.app.AppCompatActivity;‬

i‭mport android.app.DatePickerDialog;‬
‭import android.app.TimePickerDialog;‬
‭import android.os.Bundle;‬
‭import android.view.View;‬
‭import android.widget.Button;‬
‭import android.widget.TextView;‬

i‭mport java.text.SimpleDateFormat;‬
‭import java.util.Calendar;‬
‭import java.util.Locale;‬

‭public class MainActivity extends AppCompatActivity {‬

‭ rivate TextView selectedDateTextView;‬


p
‭private Button selectDateButton;‬
‭private TextView selectedTimeTextView;‬
‭private Button selectTimeButton;‬
‭private Calendar calendar;‬
‭private SimpleDateFormat dateFormatter;‬
‭private SimpleDateFormat timeFormatter;‬

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

s‭ electedDateTextView = findViewById(R.id.selectedDateTextView);‬
‭selectDateButton = findViewById(R.id.selectDateButton);‬
‭selectedTimeTextView = findViewById(R.id.selectedTimeTextView);‬
‭selectTimeButton = findViewById(R.id.selectTimeButton);‬
‭calendar = Calendar.getInstance();‬
‭dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());‬
‭timeFormatter = new SimpleDateFormat("hh:mm a", Locale.getDefault());‬

‭selectDateButton.setOnClickListener(new View.OnClickListener() {‬
‭@Override‬
‭public void onClick(View v) {‬
‭DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this,‬
‭(view, year, monthOfYear, dayOfMonth) -> {‬
‭calendar.set(year, monthOfYear, dayOfMonth);‬
‭selectedDateTextView.setText(dateFormatter.format(calendar.getTime()));‬
‭},‬
‭calendar.get(Calendar.YEAR),‬
‭calendar.get(Calendar.MONTH),‬

‭_________________________________________________________________________‬
‭_________________________________________________________________________‬

‭calendar.get(Calendar.DAY_OF_MONTH));‬
‭datePickerDialog.show();‬
}‭ ‬
‭});‬

‭selectTimeButton.setOnClickListener(new View.OnClickListener() {‬
‭@Override‬
‭public void onClick(View v) {‬
‭TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this,‬
‭(view, hourOfDay, minute) -> {‬
‭calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);‬
‭calendar.set(Calendar.MINUTE, minute);‬
‭selectedTimeTextView.setText(timeFormatter.format(calendar.getTime()));‬
‭},‬
‭calendar.get(Calendar.HOUR_OF_DAY),‬
‭calendar.get(Calendar.MINUTE),‬
‭false); // Set true for 24-hour format, false for 12-hour format‬
‭timePickerDialog.show();‬
‭}‬
‭});‬
‭}‬
‭}‬

‭ ML CODE:‬
X
‭<?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="wrap_content"‬
‭android:orientation="vertical"‬
‭android:padding="16dp"‬
‭tools:context=".MainActivity">‬

‭<LinearLayout‬
‭android:layout_width="match_parent"‬
‭android:layout_height="wrap_content"‬
‭android:orientation="horizontal"‬
‭android:gravity="center_vertical">‬

‭<TextView‬
‭android:layout_width="0dp"‬
‭android:layout_height="wrap_content"‬
‭android:layout_weight="1"‬
‭android:text="Selected Date:"‬
‭android:textSize="18sp" />‬

‭<TextView‬
‭android:id="@+id/selectedDateTextView"‬
‭android:layout_width="0dp"‬
‭android:layout_height="wrap_content"‬
‭android:layout_weight="1"‬
‭android:text=""‬

‭_________________________________________________________________________‬
‭_________________________________________________________________________‬

‭android:textSize="18sp" />‬

‭<Button‬
‭android:id="@+id/selectDateButton"‬
‭android:layout_width="wrap_content"‬
‭android:layout_height="wrap_content"‬
‭android:text="SELECT DATE" />‬
‭</LinearLayout>‬
‭<View‬
‭android:layout_width="match_parent"‬
‭android:layout_height="1dp"‬
‭android:background="@android:color/darker_gray"‬
‭android:layout_marginTop="8dp"‬
‭android:layout_marginBottom="8dp"/>‬
‭<LinearLayout‬
‭android:layout_width="match_parent"‬
‭android:layout_height="wrap_content"‬
‭android:orientation="horizontal"‬
‭android:gravity="center_vertical">‬
‭<TextView‬
‭android:layout_width="0dp"‬
‭android:layout_height="wrap_content"‬
‭android:layout_weight="1"‬
‭android:text="Selected Time:"‬
‭android:textSize="18sp" />‬
‭<TextView‬
‭android:id="@+id/selectedTimeTextView"‬
‭android:layout_width="0dp"‬
‭android:layout_height="wrap_content"‬
‭android:layout_weight="1"‬
‭android:text=""‬
‭android:textSize="18sp" />‬
‭<Button‬
‭android:id="@+id/selectTimeButton"‬
‭android:layout_width="wrap_content"‬
‭android:layout_height="wrap_content"‬
‭android:text="SELECT TIME" />‬
‭</LinearLayout>‬

‭</LinearLayout>‬

‭_________________________________________________________________________‬

You might also like