android kotlin – Popup menu with icons example

android kotlin – Popup menu with icons example

MainActivity.kt


package com.cfsuman.kotlinexamples

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import kotlinx.android.synthetic.main.activity_main.*
import android.widget.PopupMenu


class MainActivity : AppCompatActivity() {

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

        // Set the image view image source
        image_view.setImageResource(R.drawable.crow)

        // Initialize a new popup menu instance
        val popupMenu = PopupMenu(this,image_view)

        // Inflate the popup menu
        popupMenu.inflate(R.menu.popup_menu)

        // Set popup menu item click listener
        popupMenu.setOnMenuItemClickListener {
            when(it.itemId){
                R.id.center->{
                    image_view.scaleType = ImageView.ScaleType.CENTER
                    text_view.text = "Image scale type: Center"
                    true
                }
                R.id.center_crop->{
                    image_view.scaleType = ImageView.ScaleType.CENTER_CROP
                    text_view.text = "Image scale type: Center Crop"
                    true
                }
                R.id.center_inside->{
                    image_view.scaleType = ImageView.ScaleType.CENTER_INSIDE
                    text_view.text = "Image scale type: Center Inside"
                    true
                }
                R.id.fit_center->{
                    image_view.scaleType = ImageView.ScaleType.FIT_CENTER
                    text_view.text = "Image scale type: Fit Center"
                    true
                }
                R.id.fit_end->{
                    image_view.scaleType = ImageView.ScaleType.FIT_END
                    text_view.text = "Image scale type: Fit End"
                    true
                }
                R.id.fit_start->{
                    image_view.scaleType = ImageView.ScaleType.FIT_START
                    text_view.text = "Image scale type: Fit Start"
                    true
                }
                R.id.fit_xy->{
                    image_view.scaleType = ImageView.ScaleType.FIT_XY
                    text_view.text = "Image scale type: Fit XY"
                    true
                }
                R.id.matrix->{
                    image_view.scaleType = ImageView.ScaleType.MATRIX
                    text_view.text = "Image scale type: Matrix"
                    true
                }
                else-> false
            }
        }


        // Set a click listener for image view
        image_view.setOnClickListener{
            // Display the popup menu with icons
            try {
                val popup = PopupMenu::class.java.getDeclaredField("mPopup")
                popup.isAccessible = true
                val menu = popup.get(popupMenu)
                menu.javaClass
                        .getDeclaredMethod("setForceShowIcon", Boolean::class.java)
                        .invoke(menu, true)
            } catch (e: Exception){
                e.printStackTrace()
            } finally {
                popupMenu.show()
            }

            // Or simply show the popup menu without icons
            //popupMenu.show()
        }
    }
}

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e8f7e8"
    android:orientation="vertical"
    android:padding="16dp"
    >
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="450dp"
        android:background="#2eafff"
        />
    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:layout_gravity="center"
        android:textStyle="bold"
        android:padding="10dp"
        android:textColor="#ea287f"
        android:textSize="20sp"
        />
</LinearLayout>

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/center"
        android:title="Center"
        android:icon="@drawable/ic_action_copy"
        />
    <item
        android:id="@+id/center_crop"
        android:title="Center Crop"
        android:icon="@drawable/ic_action_image"
        />
    <item
        android:id="@+id/center_inside"
        android:title="Center Inside"
        android:icon="@drawable/ic_action_scale"
        />
    <item
        android:id="@+id/fit_center"
        android:title="Fit Center"
        android:icon="@drawable/ic_action_image"
        />
    <item
        android:id="@+id/fit_end"
        android:title="Fit End"
        android:icon="@drawable/ic_action_cut"
        />
    <item
        android:id="@+id/fit_start"
        android:title="Fit Start"
        android:icon="@drawable/ic_action_paste"
        />
    <item
        android:id="@+id/fit_xy"
        android:title="Fit XY"
        android:icon="@drawable/ic_action_scale"
        />
    <item
        android:id="@+id/matrix"
        android:title="Matrix"
        android:icon="@drawable/ic_action_new"
        />
</menu>

Original Article

Leave a Reply

Your email address will not be published. Required fields are marked *