Thursday, June 11, 2020

android kotlin - Popup menu example

MainActivity.kt

package com.example.jetpack

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import android.widget.PopupMenu
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

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

        // button click listener
        button.setOnClickListener {
            showPopupMenu()
        }
    }


    // method to show popup menu
    private fun showPopupMenu(){
        PopupMenu(this,button).apply {

            // inflate the popup menu
            menuInflater.inflate(R.menu.popup_menu,menu)

            // popup menu item click listener
            setOnMenuItemClickListener {
                when(it.itemId){
                    R.id.red->{
                        constraintLayout.background = ColorDrawable(Color.RED)
                    }R.id.green->{
                        constraintLayout.background = ColorDrawable(Color.GREEN)
                    }R.id.yellow->{
                        constraintLayout.background = ColorDrawable(Color.YELLOW)
                    }R.id.gray->{
                        constraintLayout.background = ColorDrawable(Color.GRAY)
                    }
                }

                false
            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                // show icon on popup menu
                this.setForceShowIcon(true)
            }

            // finally, show the popup menu
            show()
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="Show Popup Menu"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
res/menu/popup_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/red"
        android:title="Red"
        android:icon="@drawable/ic_cloud_upload"
        android:iconTint="#F44336"
        android:iconTintMode="src_in"
        />
    <item
        android:id="@+id/green"
        android:title="Green"
        android:icon="@drawable/ic_file_copy"
        android:iconTint="#4CAF50"
        android:iconTintMode="src_in"
        />
    <item
        android:id="@+id/yellow"
        android:title="Yellow"
        android:icon="@drawable/ic_settings"
        android:iconTint="#FFEB3B"
        android:iconTintMode="src_in"
        />
    <item
        android:id="@+id/gray"
        android:title="Gray"
        android:icon="@drawable/ic_clear"
        android:iconTint="#707070"
        android:iconTintMode="src_in"
        />
</menu>