



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

    <Spinner
        android:id="@+id/shapeSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <com.example.cie3_custom_shapes.MyCanvasView

        android:id="@+id/myCanvas"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
</LinearLayout>


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

package com.example.cie3_custom_shapes

import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner

class MainActivity : Activity() {

    lateinit var canvasView: MyCanvasView

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

        canvasView = findViewById(R.id.myCanvas)
        val spinner = findViewById<Spinner>(R.id.shapeSpinner)

        val shapes = arrayOf("Select Shape", "Circle", "Square", "Triangle")
        val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, shapes)
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        spinner.adapter = adapter

        spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
                if (position > 0) {
                    canvasView.selectedShape = shapes[position]
                    canvasView.invalidate()  // redraw the canvas
                }
            }

            override fun onNothingSelected(parent: AdapterView<*>) {}
        }
    }
}

=============================================
MyCanvasView.xml
=============================================


package com.example.cie3_custom_shapes

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View

class MyCanvasView(context: Context, attrs: AttributeSet) : View(context, attrs) {

    var selectedShape: String = ""

    private val paint = Paint().apply {
        color = Color.BLUE
        style = Paint.Style.FILL
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        when (selectedShape) {
            "Circle" -> {
                canvas.drawCircle(width / 2f, height / 2f, 150f, paint)
            }
            "Square" -> {
                canvas.drawRect(
                    width / 2f - 150f, height / 2f - 150f,
                    width / 2f + 150f, height / 2f + 150f, paint
                )
            }
            "Triangle" -> {
                val path = Path()
                val centerX = width / 2f
                val centerY = height / 2f
                path.moveTo(centerX, centerY - 150f) // top
                path.lineTo(centerX - 130f, centerY + 130f) // bottom-left
                path.lineTo(centerX + 130f, centerY + 130f) // bottom-right
                path.close()
                canvas.drawPath(path, paint)
            }
        }
    }
}