0% found this document useful (0 votes)
28 views27 pages

Pract 17 To 25

The document outlines various practical exercises for Android development, including creating user interfaces with XML, handling user input, and implementing features like dialing a phone number, managing a SQLite database, and displaying available sensors. Each practical includes the XML layout and corresponding Java code for functionality. The exercises cover a range of topics from basic UI components to more advanced concepts like content providers and services.

Uploaded by

Deep Patel
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)
28 views27 pages

Pract 17 To 25

The document outlines various practical exercises for Android development, including creating user interfaces with XML, handling user input, and implementing features like dialing a phone number, managing a SQLite database, and displaying available sensors. Each practical includes the XML layout and corresponding Java code for functionality. The exercises cover a range of topics from basic UI components to more advanced concepts like content providers and services.

Uploaded by

Deep Patel
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/ 27

Practical 17:

activity_main.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"

android:padding="20dp">

<TextView

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Hello, World!"

android:textSize="24sp"

android:textStyle="bold"/>

</LinearLayout>

Java code:

package com.example.helloworld;

import android.os.Bundle;

import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "LifecycleEvent";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Log.d(TAG, "onCreate: Activity Created");

@Override

protected void onStart() {

super.onStart();

Log.d(TAG, "onStart: Activity Started");

}
@Override

protected void onResume() {

super.onResume();

Log.d(TAG, "onResume: Activity Resumed");

@Override

protected void onPause() {

super.onPause();

Log.d(TAG, "onPause: Activity Paused");

@Override

protected void onStop() {

super.onStop();

Log.d(TAG, "onStop: Activity Stopped");

@Override

protected void onRestart() {

super.onRestart();

Log.d(TAG, "onRestart: Activity Restarted");

@Override

protected void onDestroy() {

super.onDestroy();

Log.d(TAG, "onDestroy: Activity Destroyed");

Output:-
Practical - 18

activity_main.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="20dp"

android:gravity="center">

<EditText

android:id="@+id/editTextUrl"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Enter URL (e.g., www.google.com)"

android:textSize="18sp"/>

<Button

android:id="@+id/btnNavigate"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Navigate"

android:layout_marginTop="20dp"/>

</LinearLayout>

Java Code:

package com.example.webnavigator;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


private EditText editTextUrl;

private Button btnNavigate;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

editTextUrl = findViewById(R.id.editTextUrl);

btnNavigate = findViewById(R.id.btnNavigate);

btnNavigate.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String url = editTextUrl.getText().toString().trim();

if (!url.isEmpty()) {

if (!url.startsWith("http://") && !url.startsWith("https://")) {

url = "https://" + url;}

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

startActivity(intent);

} else {

Toast.makeText(MainActivity.this, "Please enter a valid URL", Toast.LENGTH_SHORT).show();

});

}
Write a program to create a button "start dialer". when you click on this button it
should open the phone dialer.

activity_main.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/btnStartDialer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Dialer"
android:padding="10dp"/>

</LinearLayout>

Java code:

package com.example.dialerapp;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button btnStartDialer = findViewById(R.id.btnStartDialer);

btnStartDialer.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = new Intent(Intent.ACTION_DIAL); // Opens dialer without calling


intent.setData(Uri.parse("tel:")); // Empty to open dialer without a preset number

startActivity(intent);

});

}
Practical 19

activity_main.xml:
<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="20dp">

<EditText android:id="@+id/editTextName" android:hint="Enter Name"/>

<EditText android:id="@+id/editTextAge" android:hint="Enter Age"/>

<Button android:id="@+id/btnInsert" android:text="Insert Student"/>

<Button android:id="@+id/btnRetrieve" android:text="Retrieve Students"/>

</LinearLayout>

Define the Contract Class


package com.example.studentprovider;

import android.net.Uri;

import android.provider.BaseColumns;

public class StudentContract {

public static final String AUTHORITY = "com.example.studentprovider";

public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + AUTHORITY);

public static final String PATH_STUDENTS = "students";

public static final class StudentEntry implements BaseColumns {

public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI,


PATH_STUDENTS);

public static final String TABLE_NAME = "students";

public static final String COLUMN_NAME = "name";

public static final String COLUMN_AGE = "age";

}
Creating the SQLite Database Helper
package com.example.studentprovider;

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

public class StudentDatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "students.db";

private static final int DATABASE_VERSION = 1;

public StudentDatabaseHelper(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION);

@Override

public void onCreate(SQLiteDatabase db) {

String CREATE_TABLE = "CREATE TABLE " + StudentContract.StudentEntry.TABLE_NAME + " ("

+ StudentContract.StudentEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "

+ StudentContract.StudentEntry.COLUMN_NAME + " TEXT NOT NULL, "

+ StudentContract.StudentEntry.COLUMN_AGE + " INTEGER NOT NULL);";

db.execSQL(CREATE_TABLE);

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("DROP TABLE IF EXISTS " + StudentContract.StudentEntry.TABLE_NAME);

onCreate(db);

Implement the Content Provider


package com.example.studentprovider;

import android.content.ContentProvider;

import android.content.ContentUris;

import android.content.ContentValues;

import android.content.UriMatcher;

import android.database.Cursor;

import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import android.net.Uri;

public class StudentProvider extends ContentProvider {

private static final int STUDENTS = 1;

private static final int STUDENT_ID = 2;

private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

static {

uriMatcher.addURI(StudentContract.AUTHORITY, StudentContract.PATH_STUDENTS, STUDENTS);

uriMatcher.addURI(StudentContract.AUTHORITY, StudentContract.PATH_STUDENTS + "/#",


STUDENT_ID);

private StudentDatabaseHelper dbHelper;

@Override

public boolean onCreate() {

dbHelper = new StudentDatabaseHelper(getContext());

return true;

@Override

public Uri insert(Uri uri, ContentValues values) {

SQLiteDatabase db = dbHelper.getWritableDatabase();

long id;

switch (uriMatcher.match(uri)) {

case STUDENTS:

id = db.insert(StudentContract.StudentEntry.TABLE_NAME, null, values);

if (id > 0) {

return ContentUris.withAppendedId(StudentContract.StudentEntry.CONTENT_URI, id);

throw new SQLException("Failed to insert row into " + uri);

default:

throw new IllegalArgumentException("Unknown URI: " + uri);

@Override

public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
{
SQLiteDatabase db = dbHelper.getReadableDatabase();

Cursor cursor;

switch (uriMatcher.match(uri)) {

case STUDENTS:

cursor = db.query(StudentContract.StudentEntry.TABLE_NAME, projection, selection,


selectionArgs, null, null, sortOrder);

break;

default:

throw new IllegalArgumentException("Unknown URI: " + uri);

return cursor;

@Override

public int delete(Uri uri, String selection, String[] selectionArgs) {

throw new UnsupportedOperationException("Delete operation not supported");

@Override

public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {

throw new UnsupportedOperationException("Update operation not supported");

@Override

public String getType(Uri uri) {

return null;

}
Practical 20

activity_main.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/btnStartWifi"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Start Wi-Fi"

android:padding="10dp"/>

</LinearLayout>

Creating the WiFi service:


package com.example.wifiservice;

import android.app.Service;

import android.content.Intent;

import android.net.wifi.WifiManager;

import android.os.IBinder;

import android.util.Log;

public class WifiService extends Service {

private static final String TAG = "WifiService";

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

WifiManager wifiManager = (WifiManager)


getApplicationContext().getSystemService(WIFI_SERVICE);

if (wifiManager != null) {

if (!wifiManager.isWifiEnabled()) {

wifiManager.setWifiEnabled(true); // Enable Wi-Fi

Log.d(TAG, "Wi-Fi turned ON");

} else {
Log.d(TAG, "Wi-Fi is already ON");

return START_NOT_STICKY; // Service stops after execution

@Override

public IBinder onBind(Intent intent) {

return null;

Creatiing the Main Activity (MainActivity.java)


package com.example.wifiservice;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button btnStartWifi = findViewById(R.id.btnStartWifi);

btnStartWifi.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = new Intent(MainActivity.this, WifiService.class);

startService(intent);

});

}
Practical 21
Main Activity
package com.example.custom_broadcast_intent;

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

public class MainActivity extends Activity

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button b1 = (Button)findViewById(R.id.button1);

b1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

broadcastintent();

});

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is


present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

public void broadcastintent(){

Intent intent = new Intent();

intent.setAction("com.example.custom_broadcast_intent.MyReceiver");

sendBroadcast(intent);

}
Activity_Main.xml:

<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"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity" >

<Button

android:id="@+id/button1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_marginTop="190dp"

android:text="BroadCast Intent" />

</RelativeLayout>
Practical 22
Activity_main.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="20dp">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Available Sensors"

android:textSize="20sp"

android:textStyle="bold"

android:paddingBottom="10dp"/>

<ListView

android:id="@+id/listViewSensors"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

</LinearLayout>

MainActivity.java
package com.example.sensordisplay;

import android.hardware.Sensor;

import android.hardware.SensorManager;

import android.os.Bundle;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

import java.util.List;
public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

ListView listView = findViewById(R.id.listViewSensors);

SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);

ArrayList<String> sensorNames = new ArrayList<>();

for (Sensor sensor : sensorList) {

sensorNames.add(sensor.getName());

ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,


sensorNames);

listView.setAdapter(adapter);

}
Practical 23
Activity_main.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">

<ImageView

android:id="@+id/imageView"

android:layout_width="250dp"

android:layout_height="250dp"

android:scaleType="centerCrop"

android:background=

"@android:color/darker_gray"/>

<Button

android:id="@+id/btnCapture"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Capture Image"

android:padding="10dp"

android:layout_marginTop="20dp"/>

</LinearLayout>

MainActivity.java:
package com.example.cameracapture;

import android.content.Intent;

import android.graphics.Bitmap;

import android.os.Bundle;

import android.provider.MediaStore;

import android.view.View;

import android.widget.Button;

mport android.widget.ImageView;
import androidx.annotation.Nullable;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private static final int CAMERA_REQUEST = 100;

private ImageView imageView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

imageView = findViewById(R.id.imageView);

Button btnCapture = findViewById(R.id.btnCapture);

btnCapture.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(cameraIntent, CAMERA_REQUEST);

});

@Override

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {

Bitmap photo = (Bitmap) data.getExtras().get("data");

imageView.setImageBitmap(photo);

}
Practical 24
Activity_main.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/btnTurnOn"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Turn ON Bluetooth"

android:padding="10dp"/>

<Button

android:id="@+id/btnVisible"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Make Device Visible"

android:padding="10dp"

android:layout_marginTop="10dp"/>

<Button

android:id="@+id/btnListDevices"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="List Paired Devices"

android:padding="10dp"

android:layout_marginTop="10dp"/>

<Button

android:id="@+id/btnTurnOff"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Turn OFF Bluetooth"


android:padding="10dp"

android:layout_marginTop="10dp"/>

<ListView

android:id="@+id/listViewDevices"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"/>

</LinearLayout>

MainActivity.java:
package com.example.bluetoothapp;

import android.bluetooth.BluetoothAdapter;

import android.bluetooth.BluetoothDevice;

import android.content.Intent;

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.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

import java.util.Set;

public class MainActivity extends AppCompatActivity {

private BluetoothAdapter bluetoothAdapter;

private ListView listViewDevices;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

listViewDevices = findViewById(R.id.listViewDevices);

Button btnTurnOn = findViewById(R.id.btnTurnOn);

Button btnVisible = findViewById(R.id.btnVisible);

Button btnListDevices = findViewById(R.id.btnListDevices);


Button btnTurnOff = findViewById(R.id.btnTurnOff);

// Turn ON Bluetooth

btnTurnOn.setOnClickListener(v -> {

if (bluetoothAdapter == null) {

showToast("Bluetooth is not supported on this device");

} else if (!bluetoothAdapter.isEnabled()) {

Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enableIntent, 1);

} else {

showToast("Bluetooth is already ON");

});

// Make Device Visible

btnVisible.setOnClickListener(v -> {

Intent visibleIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

visibleIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);

startActivity(visibleIntent);

});

// List Paired Devices

btnListDevices.setOnClickListener(v -> {

if (bluetoothAdapter.isEnabled()) {

Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();

ArrayList<String> deviceList = new ArrayList<>();

for (BluetoothDevice device : pairedDevices) {

deviceList.add(device.getName() + " - " + device.getAddress());

if (deviceList.isEmpty()) {

showToast("No paired devices found");

} else {

ArrayAdapter<String> adapter = new ArrayAdapter<>(this,


android.R.layout.simple_list_item_1, deviceList);

listViewDevices.setAdapter(adapter);

} else {

showToast("Turn ON Bluetooth first");


}

});

// Turn OFF Bluetooth

btnTurnOff.setOnClickListener(v -> {

if (bluetoothAdapter.isEnabled()) {

bluetoothAdapter.disable();

showToast("Bluetooth Turned OFF");

} else {

showToast("Bluetooth is already OFF");

});

private void showToast(String message) {

Toast.makeText(this, message, Toast.LENGTH_SHORT).show();

}
Practical 24
Activity_main.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">

<ImageView

android:id="@+id/imageView"

android:layout_width="250dp"

android:layout_height="250dp"

android:src="@drawable/sample_image"

android:scaleType="centerInside"

android:background="@android:color/darker_gray"/>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:gravity="center"

android:layout_marginTop="20dp">

<Button

android:id="@+id/btnRotateClockwise"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Rotate CW"

android:padding="10dp"/>

<Button

android:id="@+id/btnRotateAntiClockwise"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Rotate ACW"

android:padding="10dp"
android:layout_marginStart="10dp"/>

</LinearLayout>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:gravity="center"

android:layout_marginTop="10dp">

<Button

android:id="@+id/btnZoomIn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Zoom In"

android:padding="10dp"/>

<Button

android:id="@+id/btnZoomOut"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Zoom Out"

android:padding="10dp"

android:layout_marginStart="10dp"/>

</LinearLayout>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:gravity="center"

android:layout_marginTop="10dp">

<Button

android:id="@+id/btnFadeIn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Fade In"

android:padding="10dp"/>
<Button

android:id="@+id/btnFadeOut"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Fade Out"

android:padding="10dp"

android:layout_marginStart="10dp"/>

</LinearLayout>

</LinearLayout>

MainActivity.java:

package com.example.imagerotation;

import android.os.Bundle;

import android.view.animation.AlphaAnimation;

import android.view.animation.ScaleAnimation;

import android.widget.Button;

import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private ImageView imageView;

private float rotationAngle = 0f; // Track rotation angle

private float scaleFactor = 1f; // Track scale factor

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

imageView = findViewById(R.id.imageView);

Button btnRotateCW = findViewById(R.id.btnRotateClockwise);

Button btnRotateACW = findViewById(R.id.btnRotateAntiClockwise);

Button btnZoomIn = findViewById(R.id.btnZoomIn);

Button btnZoomOut = findViewById(R.id.btnZoomOut);

Button btnFadeIn = findViewById(R.id.btnFadeIn);

Button btnFadeOut = findViewById(R.id.btnFadeOut);


// Rotate Clockwise

btnRotateCW.setOnClickListener(v -> {

rotationAngle += 15; // Increase rotation

imageView.setRotation(rotationAngle);

});

// Rotate Anticlockwise

btnRotateACW.setOnClickListener(v -> {

rotationAngle -= 15; // Decrease rotation

imageView.setRotation(rotationAngle);

});

// Zoom In

btnZoomIn.setOnClickListener(v -> {

scaleFactor += 0.2f; // Increase size

imageView.setScaleX(scaleFactor);

imageView.setScaleY(scaleFactor);

});

// Zoom Out

btnZoomOut.setOnClickListener(v -> {

if (scaleFactor > 0.5f) { // Prevent excessive shrinking

scaleFactor -= 0.2f;

imageView.setScaleX(scaleFactor);

imageView.setScaleY(scaleFactor);

});

// Fade In

btnFadeIn.setOnClickListener(v -> {

AlphaAnimation fadeIn = new AlphaAnimation(0, 1);

fadeIn.setDuration(500);

imageView.startAnimation(fadeIn);

imageView.setAlpha(1f);

});

// Fade Out

btnFadeOut.setOnClickListener(v -> {
AlphaAnimation fadeOut = new AlphaAnimation(1, 0);

fadeOut.setDuration(500);

imageView.startAnimation(fadeOut);

imageView.setAlpha(0f);

});

You might also like