Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
    }
}
like image 722
Lokesh Bhat Avatar asked Oct 17 '25 23:10

Lokesh Bhat


1 Answers

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 
    } 
} 
like image 58
tiny sunlight Avatar answered Oct 19 '25 14:10

tiny sunlight



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!