Last time, we implemented a very popular UI patterns in Android called Navigation drawer : Navigation Drawer Implementation in Android Now we learn how to use contextual menus in our application.
Android context menu appears when the user press long click on the element. It is also known as floating menu.
Pre-Requisites:
1. Ellipse SDK
2. Android SDK
3. ADT plugin
Or Android Studio and a compatible version of JAVA SDK
Install and configure the above utilities.
Now create a new Android project namely “MindStickContextMenu”.
Creating Layouts and views for our GUI:
Now, we need to add views in our view groups, i.e. add widgets to our GUI.
We need
1. A Relative or Linear Layout consists of list view to hold the items to be shown
in list.
For this, navigate to res/Layout/activity_main.xml from package explorer in Eclipse.
Open activity_main.xml
Add following code into it:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list"></ListView>
</RelativeLayout>
Main Activity Class Implementation:
Now, navigate to res/src/MainActivity from package explorer in Eclipse. Open MAinActivity.java
Add following code into it:
package com.example.msclient010.mindstickcontextmenu;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends ActionBarActivity {
// list view to display list items
ListView list;
//list of companies consist of item to be contained in list view
List<String> companies;
// position of item in the list int pos;
//Array adapter to hold the Array list in the list view
ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getting reference of list view
list = (ListView) findViewById(R.id.list);
//adding company names in array List to display in list view
companies = new ArrayList<String>();
companies.add("MindStick");
companies.add("Google");
companies.add("Amazon");
companies.add("Apple");
companies.add("Microsoft");
companies.add("Oracle");
//creating an adapter object
adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, android.R.id.text1, companies);
//setting adapter to list view
list.setAdapter(adapter);
//setting on long press click handler on list view items
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { //registering for context menu items
registerForContextMenu(list);
pos = position;
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return false;
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); //setting header for menu
menu.setHeaderTitle("Select An Action");
//setting menu options
menu.add(0, v.getId(), 0, "Delete");//groupId, itemId, order, title
menu.add(0, v.getId(), 0, "Update");
menu.add(0, v.getId(), 0, "Cancel");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
//deleting an item from company list and list view
if (item.getTitle() == "Delete") {
companies.remove(pos);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Company is deleted", Toast.LENGTH_LONG).show(); pos = 0;
return true;
} else if (item.getTitle() == "Update") {
Toast.makeText(getApplicationContext(), "Please update company", Toast.LENGTH_LONG).show(); } else {
return false;
}
return true;
}
}
Just hit on “Run” button and our app will be launched and our list view consist of company names will be displayed:
Long press click on any company name, a contextual menu will be appeared on the selected item.
Click on delete button the selected company will be deleted from the list view and a Toast message “Company is deleted” will be appeared at the bottom of the screen
Next we are going to see some Intent concepts in Android : Start a new activity within an Activity in Android
Thanks for reading this post.
Happy Coding!!J
Leave Comment