Mad Programs-1
Mad Programs-1
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Button 1"
android:layout_x="50dp"
android:layout_y="50dp" />
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_x="200dp"
android:layout_y="150dp" />
</AbsoluteLayout>
</LinearLayout>
Main_activity.java
package com.example.myapplication;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
2)
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">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textStyle="bold" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter your name" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textStyle="bold" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter your email" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_alignParentRight="true" />
<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_toLeftOf="@id/btnSubmit"
android:layout_alignParentRight="true" />
</RelativeLayout>
</RelativeLayout>
Java file
package com.example.myapplication;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
3)xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">
</LinearLayout>
Java file
package com.example.myapplication;
import android.os.Bundle;
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);
staticText = findViewById(R.id.staticText);
userInput = findViewById(R.id.userInput);
</LinearLayout>
Java file
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ToggleButton;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
imageButton = findViewById(R.id.imageButton);
toggleButton = findViewById(R.id.toggleButton);
5)xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="24dp"
android:background="#ffffff">
<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="24sp"
android:textStyle="bold"
android:layout_marginBottom="24dp" />
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="textPersonName"
android:layout_marginBottom="16dp" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:layout_marginBottom="16dp" />
<CheckBox
android:id="@+id/rememberMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember Me"
android:layout_marginBottom="24dp" />
<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
</LinearLayout>
Java file
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameEditText = findViewById(R.id.editTextUsername);
passwordEditText = findViewById(R.id.editTextPassword);
loginButton = findViewById(R.id.buttonLogin);
rememberMeCheckBox = findViewById(R.id.rememberMe);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username =
usernameEditText.getText().toString().trim();
String password =
passwordEditText.getText().toString().trim();
if (rememberMeCheckBox.isChecked()) {
Toast.makeText(MainActivity.this, "Credentials Saved",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
6) xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">
Java file
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
termsCheckBox = findViewById(R.id.termsCheckBox);
submitButton = findViewById(R.id.submitButton);
7)xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">
Java file
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
radioGroup = findViewById(R.id.radioGroup);
submitButton = findViewById(R.id.submitButton);
8)xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp"
android:gravity="center">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="0" />
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Progress"
android:layout_marginTop="20dp"/>
</LinearLayout>
Java file
package com.example.myapplication;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.widget.ProgressBar;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Link to XML layout
progressBar = findViewById(R.id.progressBar);
startButton = findViewById(R.id.startButton);
startButton.setOnClickListener(view -> {
progressStatus = 0;
new Thread(() -> {
while (progressStatus < 100) {
progressStatus += 1;
11)
Xml
<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">
<Button
android:id="@+id/pickDateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Date"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="32dp"
android:onClick="showDatePickerDialog" />
android:id="@+id/pickTimeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Time"
app:layout_constraintTop_toBottomOf="@+id/pickDateButton"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="16dp"
android:onClick="showTimePickerDialog" />
<TextView
android:id="@+id/selectedDateTimeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="@+id/pickTimeButton"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="32dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Java
package com.example.datetimepicker;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.text.SimpleDateFormat;
import java.util.Calendar;
Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectedDateTimeTextView = findViewById(R.id.selectedDateTimeTextView);
datePickerDialog.show();
}
public void showTimePickerDialog(android.view.View view) {
timePickerDialog.show();
13)Bluetooth connectivity
Xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<Button
android:id="@+id/btnEnableBluetooth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enable Bluetooth"/>
<Button
android:id="@+id/btnShowDevices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Paired Devices"/>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Java
package com.example.myapplication;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Set;
BluetoothAdapter bluetoothAdapter;
ListView listView;
Button btnEnable, btnShow;
btnEnable = findViewById(R.id.btnEnableBluetooth);
btnShow = findViewById(R.id.btnShowDevices);
listView = findViewById(R.id.listView);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
btnEnable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bluetoothAdapter == null) {
Toast.makeText(getApplicationContext(), "Bluetooth not
supported", Toast.LENGTH_SHORT).show();
} else if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
} else {
Toast.makeText(getApplicationContext(), "Bluetooth
already enabled", Toast.LENGTH_SHORT).show();
}
}
});
btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bluetoothAdapter.isEnabled()) {
Set<BluetoothDevice> pairedDevices =
bluetoothAdapter.getBondedDevices();
ArrayAdapter<String> adapter = new
ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1);
listView.setAdapter(adapter);
} else {
Toast.makeText(getApplicationContext(), "Enable Bluetooth
first", Toast.LENGTH_SHORT).show();
}
}
});
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="24dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/sensorTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sensor Data"
android:textSize="24sp"/>
</LinearLayout>
Java
package com.example.myapplication;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorTextView = findViewById(R.id.sensorTextView);
@Override
protected void onResume() {
super.onResume();
if (accelerometer != null) {
sensorManager.registerListener(this, accelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Not used here
}
}