first you can create menuEx project, just like helloword .
Then create main.xml look like this.
--------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/ListView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<WebView android:id="@+id/_web" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
-----------------------------------
----------------------------------------------------------------------------
Now create mainmenu and contextmenu.xml file respectively
-------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/facebook" android:title="Facebook" android:icon="@drawable/facebook" />
<item android:id="@+id/twitter" android:title="Twitter" android:icon="@drawable/twitter" />
<item android:id="@+id/google" android:title="Google Plus" android:icon="@drawable/gooogle" />
<item android:id="@+id/linkedin" android:title="LinkedIn" android:icon="@drawable/linkedin" />
<item android:id="@+id/digg" android:title="Digg " android:icon="@drawable/digg" />
<item android:id="@+id/myspace" android:title="Myspace" android:icon="@drawable/myspace" />
</menu>
-----------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/edit" android:title="Edit" />
<item android:id="@+id/delete" android:title="Delete" />
<item android:id="@+id/copy" android:title="Copy" />
<item android:id="@+id/paste" android:title="Paste" />
</menu>
----------------------------------------------------------------------
copy past following code on that java file
MenuEx\src\com\horror\android\MexuExActivity.java
package com.android.horror;
import java.util.Arrays;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MenuExActivity extends Activity {
private ListView l;
private String countries[];
/* String place[] = { "New", "Option", "Help", "Exit" };*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
countries=getResources().getStringArray(R.array.Places);
Arrays.sort(countries);
l = (ListView) findViewById(R.id.ListView01);
l.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, countries));
registerForContextMenu(l);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflator = getMenuInflater();
inflator.inflate(R.menu.mainmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.facebook:
displayFacebook();
return true;
case R.id.twitter:
displayTwitter();
return true;
case R.id.google:
displayGoogle();
return true;
case R.id.linkedin:
displayLinkedin();
return true;
case R.id.digg:
displayDigg();
return true;
case R.id.myspace:
displayMyspace();
return true;
}
return super.onOptionsItemSelected(item);
}
private void displayFacebook() {
WebView facebook = (WebView) findViewById(R.id._web);
facebook.loadUrl("http://facebook.com/");
//Toast.makeText(this, "Facebook select", 100).show();
}
private void displayTwitter() {
WebView twitter = (WebView) findViewById(R.id._web);
twitter.loadUrl("http://twitter.com/");
//Toast.makeText(this, "Twitter select", 100).show();
}
private void displayGoogle() {
WebView google = (WebView) findViewById(R.id._web);
google.loadUrl("http://plus.google.com/");
//Toast.makeText(this, "Google plus select", 100).show();
}
private void displayLinkedin() {
WebView link = (WebView) findViewById(R.id._web);
link.loadUrl("http://linkedin.com/");
//Toast.makeText(this, "LinkedIn select", 100).show();
}
private void displayDigg() {
WebView digg = (WebView) findViewById(R.id._web);
digg.loadUrl("http://digg.com/");
//Toast.makeText(this, "Digg select", 100).show();
}
private void displayMyspace() {
WebView myspace = (WebView) findViewById(R.id._web);
myspace.loadUrl("http://myspace.com/");
//Toast.makeText(this, "Myspace select", 100).show();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
MenuInflater inf = getMenuInflater();
inf.inflate(R.menu.contextmenu, menu);
menu.setHeaderTitle("ContextMenu");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
editAction(info.id);
return true;
case R.id.delete:
deleteAction(info.id);
return true;
case R.id.copy:
copyAction(info.id);
return true;
case R.id.paste:
pasteAction(info.id);
return true;
default:
break;
}
return super.onContextItemSelected(item);
}
private void editAction(long id) {
Toast.makeText(this, "Edit Action Complete", 1000).show();
}
private void deleteAction(long id) {
Toast.makeText(this, "Delete Action Complete", 1000).show();
}
private void copyAction(long id) {
Toast.makeText(this, "Copy Action Complete", 1000).show();
}
private void pasteAction(long id) {
Toast.makeText(this, "Paste Action Complete", 1000).show();
}
}
----------------------------------------------------------------------------------------
following image should be necessary in to drawable folder.
Output looks like this;
More details information click here.
0 comments
Thanks for your comment