Monday 4 November 2013

Context Menu Example

Extension of Menu for context menus providing functionality to modify the header of the context menu. Context menus do not support item shortcuts and item icons. To show a context menu on long click, most clients will want to call registerForContextMenu (View) and override onCreateContextMenu (ContextMenu, View, ContextMenu.ContextMenuInfo).

Example:







Code:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="Context Menu Example"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textStyle="bold" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="50dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="50dp" >

    </ListView>

</LinearLayout>


textstyle.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="10dp"
    android:paddingTop="20dp"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@color/blue"
    android:textSize="25sp" />

MainActivity.java


package in.androiddevelopmentanddiscussion.contextmenuexample;

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 lv;
ArrayAdapter<String> ad;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.listView1);
final String fruit[] = {"Apple","Mango","Banana","Orange"};
ad = new ArrayAdapter<String>(getApplicationContext(),R.layout.textstyle,fruit);
lv.setAdapter(ad);
registerForContextMenu(lv);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
menu.setHeaderTitle("Action Perform"); 
menu.add(0, v.getId(), 0, "Save");
menu.add(0, v.getId(), 0, "Delete");
super.onCreateContextMenu(menu, v, menuInfo);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getTitle() == "Save"){
function1(item.getItemId());
}
if(item.getTitle()=="Delete"){
function2(item.getItemId());
}
return super.onContextItemSelected(item);
}
private void function2(int itemId) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show();
}

private void function1(int itemId) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();
}
}


Download full project from here




Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en

 

No comments:

Post a Comment