right click on com.example.libtary (inside java folder (1st folder)) -> new ->activity -> empty activity (name as SecondActivity.kt)


============================================
activity_main.xml
============================================

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="16dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="Select a Book:"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ListView
        android:id="@+id/bookListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

============================================
activity_second.xml
============================================

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/resultTextView"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

============================================
MainActivity.kt
============================================


package com.example.cie3_library

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.*

class MainActivity : Activity() {

    private val books = arrayOf("Unix", "OOMD", "Mobile Application Development", "Introduction to Algorithms")
    private val departments = arrayOf("CSE", "ISE", "AI & ML", "CS IOT")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val bookListView = findViewById<ListView>(R.id.bookListView)
        val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, books)
        bookListView.adapter = adapter

        bookListView.setOnItemClickListener { _, view, position, _ ->
            val selectedBook = books[position]
            showPopup(view, selectedBook)
        }
    }

    private fun showPopup(view: View, selectedBook: String) {
        val popupMenu = PopupMenu(this, view)
        departments.forEach { dept ->
            popupMenu.menu.add(dept)
        }

        popupMenu.setOnMenuItemClickListener { item ->
            val selectedDept = item.title.toString()
            val intent = Intent(this, SecondActivity::class.java)
            intent.putExtra("BOOK", selectedBook)
            intent.putExtra("DEPT", selectedDept)
            startActivity(intent)
            true
        }

        popupMenu.show()
    }
}


============================================
SecondActivity.kt
============================================

package com.example.cie3_library

import android.app.Activity
import android.os.Bundle
import android.widget.TextView

class SecondActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        val book = intent.getStringExtra("BOOK")
        val dept = intent.getStringExtra("DEPT")

        val resultTextView = findViewById<TextView>(R.id.resultTextView)
        resultTextView.text = "Book: $book\nDepartment: $dept"
    }
}