{"id":2481,"date":"2017-05-01T15:29:23","date_gmt":"2017-05-01T15:29:23","guid":{"rendered":"http:\/\/webartdevelopers.com\/?p=2481"},"modified":"2017-05-01T15:33:50","modified_gmt":"2017-05-01T15:33:50","slug":"basic-concept-of-listview-control-and-how-to-create-context-menu-in-android","status":"publish","type":"post","link":"https:\/\/webartdevelopers.com\/blog\/basic-concept-of-listview-control-and-how-to-create-context-menu-in-android\/","title":{"rendered":"Basic Concept of ListView Control and How to Create Context Menu in Android"},"content":{"rendered":"<p>In this tutorial following is shown:<\/p>\n<ul>\n<li>Basic Concept of ListView Control in Android<\/li>\n<li>How to create Context Menu in Android<\/li>\n<\/ul>\n<p>Hints:<\/p>\n<ul>\n<li>To view Context menu, long press the option displayed on the ListView Control.<\/li>\n<\/ul>\n<p>Code:<\/p>\n<p>activity_main.xml<\/p>\n<p>&lt;RelativeLayout xmlns:android=&#8221;http:\/\/schemas.android.com\/apk\/res\/android&#8221;<br \/>\nxmlns:tools=&#8221;http:\/\/schemas.android.com\/tools&#8221;<br \/>\nandroid:layout_width=&#8221;match_parent&#8221;<br \/>\nandroid:layout_height=&#8221;match_parent&#8221;<br \/>\nandroid:paddingBottom=&#8221;@dimen\/activity_vertical_margin&#8221;<br \/>\nandroid:paddingLeft=&#8221;@dimen\/activity_horizontal_margin&#8221;<br \/>\nandroid:paddingRight=&#8221;@dimen\/activity_horizontal_margin&#8221;<br \/>\nandroid:paddingTop=&#8221;@dimen\/activity_vertical_margin&#8221;<br \/>\ntools:context=&#8221;.MainActivity&#8221; &gt;<\/p>\n<p>&lt;ListView<br \/>\nandroid:id=&#8221;@+id\/listView1&#8243;<br \/>\nandroid:layout_width=&#8221;match_parent&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_alignParentTop=&#8221;true&#8221;<br \/>\nandroid:layout_centerHorizontal=&#8221;true&#8221;<br \/>\nandroid:layout_marginTop=&#8221;43dp&#8221; &gt;<br \/>\n&lt;\/ListView&gt;<br \/>\n&lt;\/RelativeLayout&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>MainActivity.java<\/p>\n<p>package com.example.demo;<\/p>\n<p>import android.os.Bundle;<br \/>\nimport android.app.Activity;<br \/>\nimport android.view.ContextMenu;<br \/>\nimport android.view.ContextMenu.ContextMenuInfo;<br \/>\nimport android.view.Menu;<br \/>\nimport android.view.MenuItem;<br \/>\nimport android.view.View;<br \/>\nimport android.widget.ArrayAdapter;<br \/>\nimport android.widget.ListView;<br \/>\nimport android.widget.Toast;<\/p>\n<p>public class MainActivity extends Activity{<\/p>\n<p>ListView lv1;<br \/>\nString [] contacts={&#8220;John&#8221;,&#8221;Abby&#8221;,&#8221;David&#8221;,&#8221;Marie&#8221;,&#8221;Peter&#8221;};<\/p>\n<p>@Override<br \/>\nprotected void onCreate(Bundle savedInstanceState) {<br \/>\nsuper.onCreate(savedInstanceState);<br \/>\nsetContentView(R.layout.activity_main);<br \/>\nlv1=(ListView)findViewById(R.id.listView1);<br \/>\nArrayAdapter &lt;String&gt; ad1=new ArrayAdapter &lt;String&gt; (this,android.R.layout.simple_list_item_1,contacts);<br \/>\nlv1.setAdapter(ad1);<br \/>\nregisterForContextMenu(lv1);<br \/>\n}<\/p>\n<p>@Override<br \/>\npublic void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo)<br \/>\n{<br \/>\nsuper.onCreateContextMenu(menu, v, menuInfo);<br \/>\nmenu.setHeaderTitle(&#8220;Select the Action&#8221;);<br \/>\nmenu.setHeaderIcon(R.drawable.ic_launcher);<br \/>\nmenu.add(0,v.getId(),0,&#8221;Call&#8221;);<br \/>\nmenu.add(0,v.getId(),0,&#8221;SMS&#8221;);<br \/>\n}<\/p>\n<p>@Override<br \/>\npublic boolean onContextItemSelected(MenuItem item)<br \/>\n{<br \/>\nif(item.getTitle()==&#8221;Call&#8221;)<br \/>\n{<br \/>\nToast.makeText(getApplicationContext(), &#8220;Starting Call&#8221;, Toast.LENGTH_LONG).show();<\/p>\n<p>}<br \/>\nelse if(item.getTitle()==&#8221;SMS&#8221;)<br \/>\n{<br \/>\nToast.makeText(getApplicationContext(), &#8220;Sending SMS&#8221;, Toast.LENGTH_LONG).show();<\/p>\n<p>}<br \/>\nelse<br \/>\n{<br \/>\nreturn false;<br \/>\n}<br \/>\nreturn true;<br \/>\n}<br \/>\n\/\/public boolean onCreateOptionsMenu(Menu menu) {<br \/>\n\/\/ Inflate the menu; this adds items to the action bar if it is present.<br \/>\n\/\/getMenuInflater().inflate(R.menu.main, menu);<br \/>\n\/\/return true;<br \/>\n}<\/p>\n<p><a href=\"https:\/\/youtu.be\/NV-F67v0PKg\">https:\/\/youtu.be\/NV-F67v0PKg<\/a><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"In this tutorial following is shown: Basic Concept of ListView Control in Android How to create Context Menu in Android Hints: To view Context menu, long press the option displayed on the ListView Control. Code: activity_main.xml &lt;RelativeLayout xmlns:android=&#8221;http:\/\/schemas.android.com\/apk\/res\/android&#8221; xmlns:tools=&#8221;http:\/\/schemas.android.com\/tools&#8221; android:layout_width=&#8221;match_parent&#8221; android:layout_height=&#8221;match_parent&#8221; android:paddingBottom=&#8221;@dimen\/activity_vertical_margin&#8221; android:paddingLeft=&#8221;@dimen\/activity_horizontal_margin&#8221; android:paddingRight=&#8221;@dimen\/activity_horizontal_margin&#8221; android:paddingTop=&#8221;@dimen\/activity_vertical_margin&#8221; tools:context=&#8221;.MainActivity&#8221; &gt; &lt;ListView android:id=&#8221;@+id\/listView1&#8243; android:layout_width=&#8221;match_parent&#8221; android:layout_height=&#8221;wrap_content&#8221; android:layout_alignParentTop=&#8221;true&#8221; android:layout_centerHorizontal=&#8221;true&#8221; android:layout_marginTop=&#8221;43dp&#8221; &gt; &lt;\/ListView&gt; &lt;\/RelativeLayout&gt; &nbsp; MainActivity.java package com.example.demo; import android.os.Bundle; import android.app.Activity; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity{ ListView lv1; String  contacts={&#8220;John&#8221;,&#8221;Abby&#8221;,&#8221;David&#8221;,&#8221;Marie&#8221;,&#8221;Peter&#8221;}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv1=(ListView)findViewById(R.id.listView1); ArrayAdapter &lt;String&gt; ad1=new ArrayAdapter &lt;String&gt; (this,android.R.layout.simple_list_item_1,contacts); lv1.setAdapter(ad1); registerForContextMenu(lv1); } @Override public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); <!-- AddThis Advanced Settings generic via filter on get_the_excerpt -->","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62],"tags":[85],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2481"}],"collection":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/comments?post=2481"}],"version-history":[{"count":3,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2481\/revisions"}],"predecessor-version":[{"id":2484,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2481\/revisions\/2484"}],"wp:attachment":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/media?parent=2481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/categories?post=2481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/tags?post=2481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}