1 of 12

Context Menus, AlertDialog, Custom Dialog, Toast, and Snackbar

2 of 12

1. Menus in Android�Android provides different types of menus:

a) Options Menu (Top-right three-dot menu)

  • Appears when the user taps the menu icon.
  • menu_main.xml

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.menu_main, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

if (item.getItemId() == R.id.m1) {

Toast.makeText(this, "Settings clicked", Toast.LENGTH_SHORT).show();

}

return true;

}

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/m1" android:title="Settings"/>

<item android:id="@+id/m2" android:title="About"/>

</menu>

3 of 12

b) Context Menu (Long press menu)�Used when the user long-clicks an item.�

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

TextView tv = findViewById(R.id.textView);

registerForContextMenu(tv);

}

@Override

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

getMenuInflater().inflate(R.menu.context_menu, menu);

}

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/copy" android:title="Copy"/>

<item android:id="@+id/delete" android:title="Delete"/>

</menu>

context_menu.xml:

4 of 12

c) Popup Menu (Like Telegram message menu)�Used for quick actions when tapping on a button.�

PopupMenu popup = new PopupMenu(MainActivity.this, view);

popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());

popup.show();

5 of 12

2. Dialogs in Android�A dialog is a window that appears above the current Activity.

  • a) AlertDialog
  • Used to show warnings, confirmations, or important messages.

new AlertDialog.Builder(this)

.setTitle("Delete")

.setMessage("Are you sure you want to delete?")

.setPositiveButton("Yes", (dialog, which) -> {

Toast.makeText(this, "Deleted", Toast.LENGTH_SHORT).show();

})

.setNegativeButton("No", null)

.show();

6 of 12

b) Custom Dialog�You can create your own layout for a dialog.�custom_dialog.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical" android:padding="20dp">

<EditText android:id="@+id/etName" android:hint="Enter your name"/>

<Button android:id="@+id/btnOk" android:text="OK"/>

</LinearLayout>

Dialog dialog = new Dialog(this);

dialog.setContentView(R.layout.custom_dialog);

EditText et = dialog.findViewById(R.id.etName);

Button btn = dialog.findViewById(R.id.btnOk);

btn.setOnClickListener(v -> {

Toast.makeText(this, "Hello " + et.getText(), Toast.LENGTH_SHORT).show();

dialog.dismiss();

});

dialog.show();

7 of 12

3. Toast Message

Toast shows a short temporary message at the bottom.

Toast.makeText(this, "Saved successfully", Toast.LENGTH_SHORT).show();

8 of 12

4. Snackbar�Snackbar is similar to Toast but has an action button.

  • Advantages of Snackbar:
  • It can have a button (“UNDO”, “RETRY”)
  • Material Design appearance
  • Appears from the bottom and pushes elements upward
  • More functional than Toast
  • Examples:
  • “Item deleted — UNDO”
  • “No Internet — RETRY”

9 of 12

1. OPTIONS MENU (3 nuqta menyu)� step 1: menu/my_menu.xml�

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item

android:id="@+id/menu_settings"

android:title="Settings" />

<item

android:id="@+id/menu_logout"

android:title="Logout" />

</menu>

10 of 12

step 2: MainActivity.java

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.my_menu, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(@NonNull MenuItem item) {

switch (item.getItemId()) {

case R.id.menu_settings:

Toast.makeText(this, "Settings bosildi", Toast.LENGTH_SHORT).show();

return true;

case R.id.menu_logout:

Toast.makeText(this, "Logout bosildi", Toast.LENGTH_SHORT).show();

return true;

}

return super.onOptionsItemSelected(item);

}

11 of 12

2. CONTEXT MENU (Uzoq bosiladigan menyu)

<TextView

android:id="@+id/myText"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Uzoq bos!"

android:textSize="20sp" />

12 of 12