I understand you want to show different context menu for short clicks and long clicks on a GridView item.
First, you just need to set listener for short click since the default behavior will automatically show context menu on long clicks.
Next, set a boolean flag to true in the OnItemClickListener. The default value is false for long clicks.
Finally, in onCreateContextMenu() check if its a short click and show a different context menu (standard) and set flag to false. Else let it show the default context menu (options).
Here is some code to demonstrate the idea.
public class MainActivity extends Activity {
private static final String[] arr = {"A", "B", "C", "D", "E", "F", "G", "H","I"};
private GridView mTGrid;
private boolean isShort;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTGrid = (GridView) findViewById(R.id.gridView1);
registerForContextMenu(mTGrid);
mTGrid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
isShort = true;
openContextMenu(view);
}
});
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.cell, arr);
mTGrid.setAdapter(adapter);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
if(isShort) {
getMenuInflater().inflate(R.menu.context_standard, menu);
menu.setHeaderTitle("Standard Menu for "+arr[info.position]);
isShort = false;
}
else {
getMenuInflater().inflate(R.menu.context_options, menu);
menu.setHeaderTitle("Options Menu for "+arr[info.position]);
}
}
}
Or u can do this thing completely remove the **onItemLongClick** listener and just use the following code to get the selected cell in **onContextItemSelected()**
I understand you want to show different context menu for short clicks and long clicks on a GridView item.
First, you just need to set listener for short click since the default behavior will automatically show context menu on long clicks.
Next, set a boolean flag to true in the OnItemClickListener. The default value is false for long clicks.
Finally, in onCreateContextMenu() check if its a short click and show a different context menu (standard) and set flag to false. Else let it show the default context menu (options).
Here is some code to demonstrate the idea.
public class MainActivity extends Activity {
private static final String[] arr = {"A", "B", "C", "D", "E", "F", "G", "H","I"};
private GridView mTGrid;
private boolean isShort;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTGrid = (GridView) findViewById(R.id.gridView1);
registerForContextMenu(mTGrid);
mTGrid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
isShort = true;
openContextMenu(view);
}
});
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.cell, arr);
mTGrid.setAdapter(adapter);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
if(isShort) {
getMenuInflater().inflate(R.menu.context_standard, menu);
menu.setHeaderTitle("Standard Menu for "+arr[info.position]);
isShort = false;
}
else {
getMenuInflater().inflate(R.menu.context_options, menu);
menu.setHeaderTitle("Options Menu for "+arr[info.position]);
}
}
}
**Sample Application**:
You can download a sample application to see the behavior. [GridExample_eclipse_project][1]
[1]: http://www.appsrox.com/shared/GridExample_eclipse_project.zip