home top ad

Android Context Menu Example

Android Context Menu Example

The Android context menu appears when a user presses a long object into an object. Also known as a floating menu.
Affects selected content while taking action on it.
It does not support object shortcuts and icons.

Android Context Menu Example

activity_main.xml

Now you can create or create listView in activity_main by dragging or designing it.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="5dp" />

</RelativeLayout>
After this, right-clicking on res will create context_menu by clicking on Android Resource Directory.

context_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/item1"
        android:title="Item1"/>

    <item android:id="@+id/item2"
        android:title="Item2"/>


    <item android:id="@+id/item3"
        android:title="Item3"/>
</menu>
Now we will implement this layout in MainActivity.java

MainActivity.java

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    ListView listView;
    String str[]={"C","C++","JAVA","PYTHON","KOTLIN","C#","ANDROID"};
    ArrayAdapter arrayAdapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.context_menus);
         listView=findViewById(R.id.listView);
         arrayAdapter=new ArrayAdapter(ContextMenu.this,android.R.layout.simple_list_item_1,str);
         listView.setAdapter(arrayAdapter);
          registerForContextMenu(listView);


    }

    @Override
    public void onCreateContextMenu(android.view.ContextMenu menu, View v, android.view.ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.context_menu,menu);
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.item1:
                Toast.makeText(this, "Item 1", Toast.LENGTH_SHORT).show();
                break;

            case R.id.item2:
                Toast.makeText(this, "Item 2", Toast.LENGTH_SHORT).show();
                break;

            case R.id.item3:
                Toast.makeText(this, "Item 3", Toast.LENGTH_SHORT).show();
                break;

            default:


        }
        return super.onContextItemSelected(item);
    }
}
After this you can see the output by running this project and below is given a small screen shot of how this app will work.
Here we can see all the items in all the listViews.
After long clicking on the listView item, you will see the context menu.
After clicking on the context menu.
Hope you find this article very useful.

Thanks you....

Post a Comment

0 Comments