Skip to main content

Posts

Showing posts with the label menu

Android Kotlin: How to create a menu with checkable menu items

Introduction This code demonstrates creating an Android app (written in Kotlin) with a menu containing checkable menu items. These menu items allow users to change the text color and formatting (bold, italic, underline) of a TextView within the app. Breakdown The code consists of three main parts: MainActivity.kt: This file contains the main Activity class responsible for handling the UI and user interaction. It inflates the menu layout ( options_menu.xml ) and defines functionality for both creating the menu and handling user selections. activity_main.xml: This file defines the layout of the main activity screen. It includes a single MaterialTextView element that displays text and allows users to change its color and formatting. res/menu/options_menu.xml: This file defines the structure of the menu displayed in the app. It contains six menu items: three for changing text color (red, green, yellow) and three for text formatting (bold, italic, underline). The formatt...

Android Kotlin: How to create a popup menu with icons

This code demonstrates how to create a popup menu with icons in an Android application written in Kotlin. The code utilizes functionalities from the AppCompat library and offers solutions for different Android API levels. Key functionalities explained: The MainActivity.kt file handles the core logic. It displays a text view with a click listener that triggers the showPopupMenu() function. The showPopupMenu() function inflates a popup menu from a resource file ( popup_menu.xml ). It sets a listener to handle user selections on the menu items. There are two approaches for displaying icons on the menu items: For API level 29 (Android Q) and above, a direct method ( setForceShowIcon(true) ) is available. For lower API levels, reflection is used to access a hidden method and achieve the same result. Summary This code provides a well-structured and informative example for implementing a popup menu with icons in an Android Kotlin application. It showcases handling different API...