Creating a Floating Context Menu
in Android Studio(Java)
A Context Menu provides additional options related to a specific view—the same concept as a right-click on the desktop.
Step 1: Create a new project in Android Studio.
Step 2: Create the menu by creating a new file in res/ menu called context_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_cast"
android:title="Cast"/>
<item android:id="@+id/menu_print"
android:title="Print"/>
</menu>Step 3: Create a new file in the res/layout directory called activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/showContextualMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Long press for menu"
android:textSize="20sp"
android:textStyle="bold"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 4: Create a new java file called MainActivity.java
Step 4-a: Create global variable to store the ActionMode instance returned when we call startActionMode().
ActionMode mActionMode;
Step 4-b: Create an ActionMode callback to pass to startActionMode().
private final ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.context_menu, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id. menu_cast:
Toast.makeText(MainActivity.this, "Cast", Toast.LENGTH_SHORT).show();
mode.finish();
return true;
case R.id. menu_print:
Toast.makeText(MainActivity.this, "Print", Toast.LENGTH_SHORT).show();
mode.finish();
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};Step 4-c: With the ActionMode callback created, we just need to call startActionMode() to begin Contextual Mode
findViewById(R.id.showContextualMenu).setOnLongClickListener(v -> {
if (mActionMode != null) return false;
mActionMode = startActionMode(mActionModeCallback);
return true;
});Step 4-d: Final code of MainActivity.java
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
//Create global variable to store the ActionMode instance returned when we call startActionMode()
ActionMode mActionMode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialise view &
//handle the long click at TextView
findViewById(R.id.showContextualMenu).setOnLongClickListener(v -> {
if (mActionMode != null) return false;
mActionMode = startActionMode(mActionModeCallback);
return true;
});
}
//Create an ActionMode callback to pass to startActionMode()
private final ActionMode.Callback mActionModeCallback = new ActionMode.
Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.context_menu, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id. menu_cast:
Toast.makeText(MainActivity.this, "Cast", Toast.LENGTH_SHORT).show();
mode.finish();
return true;
case R.id. menu_print:
Toast.makeText(MainActivity.this, "Print", Toast.LENGTH_SHORT).show();
mode.finish();
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
}

0 Comments