Context Menu:-Android context menu appears when user press long click on the element. It is also known as floating menu.
Required classes and xml layouts:-
1.MainActivity.java | activity_main.xml
Create a new project and add following code to your project:-
Step1:- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="supriya.com.contextmenu.MainActivity"
android:background="#DF013A">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list1"></ListView>
</RelativeLayout>
Step2:-MainActivity.java
Required classes and xml layouts:-
1.MainActivity.java | activity_main.xml
Create a new project and add following code to your project:-
Step1:- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="supriya.com.contextmenu.MainActivity"
android:background="#DF013A">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list1"></ListView>
</RelativeLayout>
Step2:-MainActivity.java
package supriya.com.contextmenu; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.ContextMenu; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { ListView listView; String[] list = {"Somya", "Supriya", "Pankaj", "Ankit", "Rajwari", "Honey", "Vikash", "Sunita", "Dipika", "Suman", "Akash", "Saurabh", "Mohan", "Ram"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.list1); ArrayAdapter<String> adp = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, list); listView.setAdapter(adp); registerForContextMenu(listView); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.setHeaderTitle("Select Option"); menu.add(0, v.getId(), 0, "Call"); menu.add(0,v.getId(),0,"Message"); menu.add(0,v.getId(),0,"Mail"); } @Override public boolean onContextItemSelected(MenuItem item) { if(item.getTitle()=="Call"){ Toast.makeText(getApplicationContext(),"Call:Selected",Toast.LENGTH_LONG).show(); } else if (item.getTitle()=="Message"){ Toast.makeText(getApplicationContext(),"Message:Selected",Toast.LENGTH_LONG).show(); } else if(item.getTitle()=="Mail"){ Toast.makeText(getApplicationContext(),"Mail:Selected",Toast.LENGTH_LONG).show(); } return super.onContextItemSelected(item); } }Output:-
Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.Android Training in velachery | Android Training in chennai | Android Training in chennai with placement
ReplyDeleteAndroid context menu appears when user press long click on the element. It is also known as floating menu.
ReplyDeleteIt doesn't support item shortcuts and icons.Android Training in chennai | Best Android Training in chennai|Android Training in chennai with placement | Android Training in velachery
Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
ReplyDeleteData Science Training in Chennai
Data science training in bangalore
Data science online training
Data science training in pune
Data science training in kalyan nagar
Data Science with Python training in chenni
I am really impressed with your efforts and really pleased to visit this post.
ReplyDeletejava training in annanagar | java training in chennai
java training in marathahalli | java training in btm layout
java training in rajaji nagar | java training in jayanagar
java training in chennai
Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
ReplyDeleteonline Python certification course
python training in OMR
python training course in chennai
Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
ReplyDeleteDevops training in marathahalli
Devops training in rajajinagar
Nice tips. Very innovative... Your post shows all your effort and great experience towards your work Your Information is Great if mastered very well.
ReplyDeleteangularjs-Training in sholinganallur
angularjs-Training in velachery
angularjs Training in bangalore
angularjs Training in bangalore
angularjs Training in btm
angularjs Training in electronic-city
It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful.Best Android Training Institute
ReplyDeletewow great and well post. I am also looking such as blog . I like that post.you can Book online Ticket through Redbus
ReplyDeletejava training in chennai
java training in velachery
aws training in chennai
aws training in velachery
python training in chennai
python training in velachery
selenium training in chennai
selenium training in velachery
Your work is very good and I appreciate you and hopping for some more informative posts celebrity net worth
ReplyDeleteangular js training in chennai
angular js training in annanagar
full stack training in chennai
full stack training in annanagar
php training in chennai
php training in annanagar
photoshop training in chennai
photoshop training in annanagar
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…
ReplyDeletehardware and networking training in chennai
hardware and networking training in tambaram
xamarin training in chennai
xamarin training in tambaram
ios training in chennai
ios training in tambaram
iot training in chennai
iot training in tambaram
Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
ReplyDeleteoracle training in chennai
oracle training in omr
oracle dba training in chennai
oracle dba training in omr
ccna training in chennai
ccna training in omr
seo training in chennai
seo training in omr