Here you'll get help for all programming languages like android,php, html, css, bootstrap, javaScript, java etc...

Context Menu in Android






activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:background="#ff6"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context="com.example.malik.blog.MainActivity">

    <ListView
        android:id="@+id/list1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="#ff0"
        android:dividerHeight="6dp"
        android:listSelector="#fff"></ListView>

</LinearLayout>



MainActivity.java


package com.example.malik.blog;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    String city[] = {"Ahmedabad", "Pune", "Banglore", "Hydrabad", "Goa", "Mount Abu", "Karnataka", "Gandhinagar", "Patna"};

    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.list1);

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, city);

        listView.setAdapter(arrayAdapter);

        registerForContextMenu(listView);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.my_context_menu, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.delete:
                Toast.makeText(getApplicationContext(), "You Select Delete", Toast.LENGTH_LONG).show();
                break;

            case R.id.share:
                Toast.makeText(getApplicationContext(), "You Select Share", Toast.LENGTH_LONG).show();
                break;

            case R.id.help:
                Toast.makeText(getApplicationContext(), "You Select Help", Toast.LENGTH_LONG).show();
                break;

            case R.id.menu:
                Toast.makeText(getApplicationContext(), "You Select Menu", Toast.LENGTH_LONG).show();
                break;


        }
        return super.onContextItemSelected(item);
    }
}



AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.malik.blog">


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



my_context_menu.xml   Inside res/menu folder


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

    <item
        android:id="@+id/share"
        android:title="Share" />

    <item
        android:id="@+id/help"
        android:title="Help" />

    <item
        android:id="@+id/menu"
        android:title="Menu" />
</menu>



build.gradle


apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.malik.blog"
        minSdkVersion 18
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}

User Help

If you want any source code or any other help just contact me at malik_himani@ymail.com. Your requirement's code will be uploaded in blog soon.