androidcontextmenuandroid-contextmenu

Context menu single click Android


I'm new to the Android development. I was trying to add a context menu to my app. I understood that by default it requires long click on the button to open the context menu. But I need to make it to appear on the single click. I tried all the other solutions here in stackoverflow but none of them are really helping me.

I have posted my code below. kindly let me know what are the modifications to be done to make it working.

public class ThirdActivity extends ActionBarActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.third_layout);
        confirmButton = (Button) findViewById(R.id.confirmButton);
        registerForContextMenu(confirmButton);
}

public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Select Menu");
    menu.add(0, v.getId(), 0, "Action 1");
}


public boolean onContextItemSelected(MenuItem item) {

      if (item.getTitle() == "Action 1") {
        //do something
    }
}

Solution

  • just :
    
    public class ThirdActivity extends ActionBarActivity { 
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.third_layout);
            confirmButton = (Button) findViewById(R.id.confirmButton);
        confirmButton .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                confirmButton .performLongClick();
            }
        });
            registerForContextMenu(confirmButton); 
    } 
    
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Select Menu");
        menu.add(0, v.getId(), 0, "Action 1");
    } 
    
    
    public boolean onContextItemSelected(MenuItem item) {
    
          if (item.getTitle() == "Action 1") {
            //do something 
        } 
    }