Contextual Menu ActionMode

September 13, 2013
ScreenShots:



activity_main.xml:
<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=".MainActivity" >

    <Button
        android:id="@+id/button_contextualmenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="166dp"
        android:text="Show Contextual Menu" />

</RelativeLayout>

MainActivity.java:
package com.ram.demo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.ram.contextualmenu.R;

@SuppressLint("NewApi")
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button_contextualmenu);

b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActionMode(mActionModeCallback);
}
});

}

@SuppressLint("NewApi")
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

// Called when the action mode is created; startActionMode() was called
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}

// Called each time the action mode is shown. Always called after
// onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}

// Called when the user selects a contextual menu item
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(getApplicationContext(), "item1 selected",
Toast.LENGTH_SHORT).show();
break;
case R.id.item2:
Toast.makeText(getApplicationContext(), "item2 selected",
Toast.LENGTH_SHORT).show();
break;
case R.id.item3:
Toast.makeText(getApplicationContext(), "item3 selected",
Toast.LENGTH_SHORT).show();
break;
}
return false;
}

@Override
public void onDestroyActionMode(ActionMode arg0) {
// TODO Auto-generated method stub

}

};

}



Artikel Terkait

Previous
Next Post »