Android Tutorials for Beginners
A context menu provides actions that affect a specific item or context frame in the UI.
Context Menu appears when user long press on a View like ListView, GridView etc.
For Ex: Below Is Context menu registerd with List View
When A User long Press on a contacts he/she gets two option Call and SMS through Context Menu.
Some More Good Android Topics
Customizing Toast In Android
Showing Toast for Longer Time
Customizing Checkboxes In Android
Customizing Progress Bar
Creating a context menu
To create a context menu:- Register the
View
to which the context menu should be associated by callingregisterForContextMenu()
and pass it theView
. Here we have used Context Menu with ListView
- Implement the
onCreateContextMenu()
method in yourActivity
When the registered view receives a long-click event, the system calls your
onCreateContextMenu()
method. This is where you define the menu items, usually by inflating a menu resource.
1: Registering The View(here List View) for Context menu.
We have to register the ListView for ContextMenu in onCreate method
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.show_contacts);
listViewSmartContacts=(ListView)findViewById(R.id.listSmartContacts);
//getList();
contactListAdapter=new ContactListAdapter(this);
listViewSmartContacts.setAdapter(contactListAdapter);
// Register the ListView for Context menu
registerForContextMenu(listViewSmartContacts);
}
2: Implement the onCreateContextMenu()
When the registered view receives a long-click event, the system calls your onCreateContextMenu()
method.
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select The Action");
menu.add(0, v.getId(), 0, "Call");
menu.add(0, v.getId(), 0, "Send SMS");
}
MenuInflater
allows you to inflate the context menu from a menu resource
3: Implement
onContextItemSelected method
When the user selects a menu item, the system calls this method so you can perform the appropriate action.
@Override
public boolean onContextItemSelected(MenuItem item)
{
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
if(item.getTitle()=="Call")
{
// Code to execute when clicked on This Item
}
else if(item.getTitle()=="Send SMS")
{
// Code to execute when clicked on This Item
}
else
{return false;}
return true;
}
}
The Completye Code:
package com.mtracker;
import com.mtracker.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
public class ShowSmartContactsActivity extends Activity
{
ListView listViewSmartContacts;
ContactListAdapter contactListAdapter;
//ArrayList<String> numberList;
String number;
public ShowSmartContactsActivity()
{
/// numberList=new ArrayList<String>();
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.show_contacts);
LinearLayout layoutContacts=(LinearLayout)findViewById(R.id.layoutSmartContacts);
int APILevel=android.os.Build.VERSION.SDK_INT ;
if(APILevel>=14)
{
layoutContacts.setBackgroundResource(R.color.Default);
}
listViewSmartContacts=(ListView)findViewById(R.id.listSmartContacts);
//getList();
contactListAdapter=new ContactListAdapter(this);
listViewSmartContacts.setAdapter(contactListAdapter);
registerForContextMenu(listViewSmartContacts);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select The Action");
menu.add(0, v.getId(), 0, "Call");
menu.add(0, v.getId(), 0, "Send SMS");
}
@Override
public boolean onContextItemSelected(MenuItem item)
{
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
String number;
try
{
number=new ContactListAdapter(this).numberList.get(info.position);
if(item.getTitle()=="Call")
{
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+number));
startActivity(callIntent);
}
else if(item.getTitle()=="Send SMS")
{
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", number);
startActivity(smsIntent);
}
else
{return false;}
return true;
}
catch(Exception e)
{
return true;
}
}
}
Advance Android Topics
Customizing Toast In Android
Showing Toast for Longer Time
Customizing the Display Time of Toast
Using TimePickerDialog and DatePickerDialog In android
Animating A Button In Android
Populating ListView With DataBase
Customizing Checkboxes In Android
Increasin Size of Checkboxes
Android ProgressBar
Designing For Different Screen Sizes
Handling Keyboard Events
More Android Topics:
Android : Introduction
Eclipse Setup for Android Development
Configuring Eclipse for Android DevelopmentBegging With Android
Creating Your First Android ProjectUnderstanding Android Manifest File of your android app
Working With Layouts
Understanding Layouts in AndroidWorking with Linear Layout (With Example)
Nested Linear Layout (With Example)
Table Layout
Frame Layout(With Example)
Absolute Layout
Grid Layout
Activity
Activity In AndroidActivity Life Cycle
Starting Activity For Result
Sending Data from One Activity to Other in Android
Returning Result from Activity
Working With Views
Using Buttons and EditText in AndroidUsing CheckBoxes in Android
Using AutoCompleteTextView in Android
Grid View
Toast
Customizing Toast In AndroidCustomizing the Display Time of Toast
Customizing Toast At Runtime
Adding Image in Toast
Showing Toast for Longer Time
Dialogs In Android
Working With Alert DialogAdding Radio Buttons In Dialog
Adding Check Boxes In Dialog
Creating Customized Dialogs in Android
Adding EditText in Dialog
Creating Dialog To Collect User Input
DatePicker and TimePickerDialog
Using TimePickerDialog and DatePickerDialog In androidWorking With SMS
How to Send SMS in AndroidHow To Receive SMS
Accessing Inbox In Android
ListView:
Populating ListView With DataBaseMenus In Android
Creating Option MenuCreating Context Menu In Android
TelephonyManager
Using Telephony Manager In AndroidWorking With Incoming Calls
How To Handle Incoming Calls in AndroidHow to Forward an Incoming Call In Android
CALL States In Android
Miscellaneous
Notifications In AndroidHow To Vibrate The Android Phone
Sending Email In Android
Opening a webpage In Browser
How to Access PhoneBook In Android
Prompt User Input with an AlertDialog
Storage: Storing Data In Android
Shared Prefferences In Android
SharedPreferences In AndroidFiles: File Handling In Android
Reading and Writing files to Internal StoarageReading and Writing files to SD Card
DataBase : Working With Database
Working With Database in AndroidCreating Table In Android
Inserting, Deleting and Updating Records In Table in Android
How to Create DataBase in Android
Accessing Inbox In Android
No comments:
Post a Comment