Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
In Android, menus are common user interface components used to present a list of options to the user. Android supports several types of menus:
Options Menu:
Context Menu:
Popup Menu:
XML (menu_main.xml in the res/menu
directory):
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/action_settings" android:title="@string/action_settings" android:orderInCategory="100" android:showAsAction="never" /> </menu>
Kotlin/Java (within an Activity):
override fun onCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.menu_main, menu) return true } override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.action_settings -> { // Handle settings action return true } else -> return super.onOptionsItemSelected(item) } }
Register a View for Context Menu:
registerForContextMenu(myView)
Override methods in the Activity:
override fun onCreateContextMenu(menu: ContextMenu, v: View, menuInfo: ContextMenuInfo?) { super.onCreateContextMenu(menu, v, menuInfo) menuInflater.inflate(R.menu.context_menu, menu) } override fun onContextItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.context_option_1 -> { // Handle option 1 return true } // ... handle other menu options else -> return super.onContextItemSelected(item) } }
val popupMenu = PopupMenu(this, viewAnchor) popupMenu.inflate(R.menu.popup_menu) popupMenu.setOnMenuItemClickListener { item -> when (item.itemId) { R.id.popup_option_1 -> { // Handle option 1 true } // ... handle other menu options else -> false } } popupMenu.show()
Always consider the usability implications when using menus. Too many options can confuse users.
With the introduction of the Android Material Design guidelines, there's a trend towards using other UI elements, like the FloatingActionButton
, in place of traditional menus for important actions.
NavigationView
, which is often used in conjunction with a DrawerLayout
, is another way to present a list of options to the user, especially for navigation-related actions or switching between major sections of an app.
ListView in Android using Kotlin example:
Create a ListView in your layout XML file:
<ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content"/>
In your Kotlin code, initialize and populate the ListView:
val listView: ListView = findViewById(R.id.listView) val data = arrayOf("Item 1", "Item 2", "Item 3") val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, data) listView.adapter = adapter
Android ArrayAdapter with ListView example in Kotlin:
Use ArrayAdapter to populate a ListView with an array of data:
val listView: ListView = findViewById(R.id.listView) val data = arrayOf("Item 1", "Item 2", "Item 3") val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, data) listView.adapter = adapter
Custom ListView in Android with Kotlin:
Create a custom layout for each row in your ListView
. Create a custom adapter by extending BaseAdapter
or ArrayAdapter
and override methods like getView
to inflate the custom layout.
// CustomAdapter.kt class CustomAdapter : BaseAdapter() { // Override getView to inflate your custom layout }
Android ListView onItemClick event in Kotlin:
Set an OnItemClickListener
to handle item clicks:
listView.setOnItemClickListener { parent, view, position, id -> // Handle item click val selectedItem = parent.getItemAtPosition(position) as String Toast.makeText(applicationContext, "Clicked: $selectedItem", Toast.LENGTH_SHORT).show() }
Populating ListView from SQLite database in Android with Kotlin:
Use a database helper class to query data from SQLite and populate the ListView:
// Assuming you have a DatabaseHelper class val dbHelper = DatabaseHelper(this) val data = dbHelper.getData() // Fetch data from SQLite val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, data) listView.adapter = adapter
Multiple columns in ListView in Android with Kotlin:
Create a custom layout for each row with multiple columns. Modify your adapter to inflate this custom layout.
// CustomAdapter.kt class CustomAdapter : BaseAdapter() { // Override getView to inflate your custom layout with multiple columns }
Filtering data in ListView in Android using Kotlin:
Implement Filterable
in your custom adapter and override getFilter
to enable filtering:
val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, data) listView.adapter = adapter adapter.filter.filter("FilterText")
Sorting ListView in Android with Kotlin:
Sort your data before setting it to the adapter:
Arrays.sort(data) val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, data) listView.adapter = adapter
Android ListView with custom adapter in Kotlin:
Create a custom adapter by extending BaseAdapter
or ArrayAdapter
and override methods like getView
to inflate a custom layout for each row.
// CustomAdapter.kt class CustomAdapter : BaseAdapter() { // Override getView to inflate your custom layout }