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

code mad

The document contains multiple Android application examples demonstrating different functionalities, including managing the activity life cycle, enabling and disabling Bluetooth, capturing an image using the camera, and displaying a rectangular progress bar. Each example includes corresponding XML layout and Java code for implementation. The applications showcase basic UI elements and interactions in Android development.

Uploaded by

mpari8998
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

code mad

The document contains multiple Android application examples demonstrating different functionalities, including managing the activity life cycle, enabling and disabling Bluetooth, capturing an image using the camera, and displaying a rectangular progress bar. Each example includes corresponding XML layout and Java code for implementation. The applications showcase basic UI elements and interactions in Android development.

Uploaded by

mpari8998
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Activity life cycle:

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"> // density-independent pixels - dp

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Activity Life Cycle"
android:textSize="24sp"
android:textStyle="bold"
android:layout_centerInParent="true"/>

</RelativeLayout>

Java:
package com.example.activitylifecycle;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(), "Activity Created",
Toast.LENGTH_LONG).show();
}
protected void onStart() {
super.onStart();
Toast.makeText(getApplicationContext(), "Activity Started",
Toast.LENGTH_LONG).show();
}
protected void onResume() {
super.onResume();
Toast.makeText(getApplicationContext(), "Activity Resumed",
Toast.LENGTH_LONG).show();
}
protected void onPause() {
super.onPause();
Toast.makeText(getApplicationContext(), "Activity Paused",
Toast.LENGTH_LONG).show();
}
protected void onStop() {
super.onStop();
Toast.makeText(getApplicationContext(), "Activity Stopped",
Toast.LENGTH_LONG).show();
}
protected void onRestart() {
super.onRestart();
Toast.makeText(getApplicationContext(), "Activity Restarted",
Toast.LENGTH_LONG).show();
}
protected void onDestroy() {
super.onDestroy();
Toast.makeText(getApplicationContext(), "Activity Destroyed",
Toast.LENGTH_LONG).show();
}
}
providing Bluetooth connectivity.
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:gravity="center"
android:padding="20dp">
<Button
android:id="@+id/btnEnable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enable Bluetooth" />
<Button
android:id="@+id/btnDisable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Disable Bluetooth"
android:layout_marginTop="10dp"/>
</LinearLayout>

Java:
package com.example.bluetoothcontrol;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {


private BluetoothAdapter bluetoothAdapter;
private Button btnEnable, btnDisable;
private static final int BLUETOOTH_PERMISSION_REQUEST = 1;

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

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
btnEnable = findViewById(R.id.btnEnable);
btnDisable = findViewById(R.id.btnDisable);

if (bluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not supported on this device",
Toast.LENGTH_LONG).show();
return;
}

// Enable Bluetooth
btnEnable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!bluetoothAdapter.isEnabled()) {
// Check Bluetooth permission for Android 12+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.BLUETOOTH_CONNECT},
BLUETOOTH_PERMISSION_REQUEST);
return;
}
}
try {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(enableBtIntent);
} catch (SecurityException e) {
Toast.makeText(MainActivity.this, "Permission Denied!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "Bluetooth is already enabled",
Toast.LENGTH_SHORT).show();
}
}
});

// Disable Bluetooth
btnDisable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bluetoothAdapter.isEnabled()) {
try {
bluetoothAdapter.disable();
Toast.makeText(MainActivity.this, "Bluetooth Disabled",
Toast.LENGTH_SHORT).show();
} catch (SecurityException e) {
Toast.makeText(MainActivity.this, "Permission Denied!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "Bluetooth is already off",
Toast.LENGTH_SHORT).show();
}
}
});
}

// Handle permission result


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == BLUETOOTH_PERMISSION_REQUEST) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Bluetooth Permission Granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Bluetooth Permission Denied", Toast.LENGTH_SHORT).show();
}
}
}
}
capture an image using camera and display it.
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:gravity="center"
android:padding="20dp">
<Button
android:id="@+id/photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture Photo"
android:padding="10dp"/>
<ImageView
android:id="@+id/image"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginTop="20dp"
android:scaleType="centerCrop"
android:background="@android:color/darker_gray"/>
</LinearLayout>

Java:
package com.example.ifcdiv;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final int CAMERA_REQUEST = 1;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = findViewById(R.id.photo);
imageView = findViewById(R.id.image);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (i.resolveActivity(getPackageManager()) != null) {
startActivityForResult(i, CAMERA_REQUEST);
} else {
Toast.makeText(MainActivity.this, "No Camera App Found",
Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK && data !=
null) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap image = (Bitmap) extras.get("data");
imageView.setImageBitmap(image);
}
} else {
Toast.makeText(this, "Failed to capture image", Toast.LENGTH_SHORT).show();
}
}
}
rectangular progress bar
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:gravity="center"
android:orientation="vertical">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="300dp"
android:layout_height="20dp"
android:max="100"
android:progress="0"
/>
<TextView
android:id="@+id/progressText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0%"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>

Java:
package com.example.myapplication2;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
import android.widget.ProgressBar;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;
private TextView textView;
private Handler handler = new Handler();
private int progressState = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progressBar);
textView = findViewById(R.id.progressText);
new Thread(new Runnable() {

public void run() {


while (progressState < 100) {
progressState += 1;
try {
Thread.sleep(50); // Simulate progress delay
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
@Override
public void run() {
progressBar.setProgress(progressState);
textView.setText(progressState + "%");
}
});
}
}
}).start(); // Start the thread
}
}

You might also like