Skip to main content

Context Menu

Create context-menu.xml in menu folder in res folder
then past this code in xml file
------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/view"
        android:title="View"/>
    <item
        android:id="@+id/save"
        android:title="Save"/>
    <item
        android:id="@+id/edit"
        android:title="Edit"/>
    <item
        android:id="@+id/delete"
        android:title="Delete"/>
</menu>
---------------------------------------------------------------------------------------------------------------
Create in a String.xml File:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Contactno</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
<string-array name="name_list">
        <item>A</item>
        <item>B</item>
        <item>C</item>
        <item>D</item>
        <item>E</item>
        <item>F</item>
        <item>G</item>
        <item>H</item>
        <item>I</item>
        <item>J</item>
        <item>K</item>
        <item>L</item>
        <item>M</item>
        <item>N</item>
        <item>O</item>
        <item>P</item>
</string-array>
</resources>

------------------------------------------------------------------------------------------------------------------
For Java File

 public class MainActivity extends ListActivity {
     private String selectedName = "";
        private String[] nameList;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            nameList = getResources().getStringArray(R.array.name_list);

            setListAdapter(new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, nameList));

            registerForContextMenu(getListView());

        }

        public void onCreateContextMenu(ContextMenu menu, View v,
                ContextMenuInfo menuInfo) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.context_menu, menu);
        }

        public boolean onContextItemSelected(MenuItem item) {

            AdapterContextMenuInfo adapInfo = (AdapterContextMenuInfo) item
                    .getMenuInfo();
            selectedName = nameList[(int) adapInfo.id];

            switch (item.getItemId()) {
            case R.id.view:
                Toast.makeText(MainActivity.this,
                        "You have pressed View Context Menu for " + selectedName,
                        Toast.LENGTH_LONG).show();
                return true;
            case R.id.save:
                Toast.makeText(MainActivity.this,
                        "You have pressed Save Context Menu for " + selectedName,
                        Toast.LENGTH_LONG).show();
                return true;
            case R.id.edit:
                Toast.makeText(MainActivity.this,
                        "You have pressed Edit Context Menu for " + selectedName,
                        Toast.LENGTH_LONG).show();
                return true;
            case R.id.delete:
                Toast.makeText(MainActivity.this,
                        "You have pressed Delete Context Menu for " + selectedName,
                        Toast.LENGTH_LONG).show();
                return true;
            }
            return false;
        }

}

Output:
Long Press On Menu
-----------------------









Comments

Popular posts from this blog

Create Shared Prefrence Via XML In Android

Download Source Code : Click Here Output:

Sandeep Maheshwari on zee news

Network Security Exception in Android Retrofit Cleartext HTTP traffic not permitted

Create One directory in res/xml Create file in xml directory Past give below code : < network-security-config > < base-config cleartextTrafficPermitted= "true" /> < domain-config > < domain includeSubdomains= "true" >www.xyz.com</ domain >> < trust-anchors > < certificates src= "system" /> </ trust-anchors > </ domain-config > </ network-security-config > Add one Line into AndroidMenifest.xm File in application tag : android :networkSecurityConfig= "@xml/yourfilename" android :usesCleartextTraffic= "true"