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

Android Menus

In Android, menus are common user interface components used to present a list of options to the user. Android supports several types of menus:

  1. Options Menu:

    • It appears when the user presses the "Menu" button (if available on the device) or when they tap the three vertical dots icon (also known as the overflow icon) in the app bar.
    • It's typically used for actions that have a global impact on the app, such as settings, search, etc.
  2. Context Menu:

    • Appears when the user long-presses an element or performs a specified gesture.
    • It's often used to provide actions that are related to a specific item or context.
  3. Popup Menu:

    • A floating list of menu items that appears in a view, similar to the options menu but can be invoked in specific locations.
    • It's used to provide a list of actions that are related to specific UI controls.

Options Menu Example:

  1. 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>
    
  2. 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)
        }
    }
    

Context Menu Example:

  1. Register a View for Context Menu:

    registerForContextMenu(myView)
    
  2. 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)
        }
    }
    

Popup Menu Example:

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()

Additional Notes:

  • 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.

  1. 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
    
  2. 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
    
  3. 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
    }
    
  4. 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()
    }
    
  5. 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
    
  6. 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
    }
    
  7. 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")
    
  8. 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
    
  9. 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
    }