Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7011085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:04:55+00:00 2026-05-27T22:04:55+00:00

I have a window with several buttons. I want when a button is clicked

  • 0

I have a window with several buttons. I want when a button is clicked to show me context menu , but if other one is pressed another context menu shows up with other content in the context menu.
Here is my code

package com.a.c;    

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.Toast;

public class test extends Activity {
    /** Called when the activity is first created. */
    @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button btn = (Button) findViewById(R.id.button_example);
    registerForContextMenu(btn);
}

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

@Override
public boolean onContextItemSelected(MenuItem item) {
    if(item.getTitle()=="Action 1"){function1(item.getItemId());}
    else if(item.getTitle()=="Action 2"){function2(item.getItemId());}
    else {return false;}
return true;
}

public void function1(int id){
    Toast.makeText(this, "function 1 called", Toast.LENGTH_SHORT).show();
}
public void function2(int id){
    Toast.makeText(this, "function 2 called", Toast.LENGTH_SHORT).show();
}
}   

I have seen this Question which was helpful and accordingly I modified my code, but it doesn’t work. I don’t know what’s going wrong.
This is the modified one.

package com.a.c;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.Toast;

public class DemoActivity extends Activity{

    Button Buildings = (Button) findViewById(R.id.button1);   
    Button foodcourt = (Button) findViewById(R.id.button2);    

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


            registerForContextMenu(Buildings); 

            registerForContextMenu(foodcourt);   
        }   

    @Override  
       public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {   
        super.onCreateContextMenu(menu, v, menuInfo); 
        if (v == Buildings) { 
            menu.setHeaderTitle("Context Menu");   
            menu.add(0, v.getId(), 0, "Action 1");   
            menu.add(0, v.getId(), 0, "Action 2");  }

        else if (v == foodcourt) { 
                     menu.setHeaderTitle("Context Menu");   
                     menu.add(0, v.getId(), 0, "Action 1");   
                     menu.add(0, v.getId(), 0, "Action 2");  
                     menu.add(0, v.getId(), 0, "Action 5");
                     }    
       }   

       @Override  
        public boolean onContextItemSelected(MenuItem item) {
          if(item.getTitle()=="Action 1"){function1(item.getItemId());}   
          else if(item.getTitle()=="Action 2"){function2(item.getItemId());}   
        else if(item.getTitle()=="Action 3"){function3(item.getItemId());}
         else if(item.getTitle()=="Action 4"){function4(item.getItemId());}
          else if(item.getTitle()=="Action 5"){function5(item.getItemId());}

          else {return false;}   
         return true;   
         }   


        public void function1(int id){   
         Toast.makeText(this, "function 1 called", Toast.LENGTH_SHORT).show();   
       }   
       public void function2(int id){   
          Toast.makeText(this, "function 2 called", Toast.LENGTH_SHORT).show();   
       }   

      public void function3(int id){   
        Toast.makeText(this, "function 1 called", Toast.LENGTH_SHORT).show();   
      }   
      public void function4(int id){   
         Toast.makeText(this, "function 2 called", Toast.LENGTH_SHORT).show();   
      }
      public void function5(int id){   
          Toast.makeText(this, "function 1 called", Toast.LENGTH_SHORT).show();   
        } 

  }  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:04:56+00:00Added an answer on May 27, 2026 at 10:04 pm

    Take your Button initialization inside onCreate() like this:

    public class DemoActivity extends Activity{
    
        Button buildings;   
        Button foodcourt;    
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            buildings = (Button) findViewById(R.id.button1);
            foodcourt = (Button) findViewById(R.id.button2); 
    
            registerForContextMenu(Buildings); 
            registerForContextMenu(foodcourt);   
    }
    //rest of your code...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several buttons that when clicked I don't want them to get focus
I have the following code but I'd like to structure several buttons of certain
On my main window, I have several different buttons and fields that are already
I have a panel with a form layout, containing several buttons. now I want
I have several child windows, all with the same parent. I want to cascade/tile
If I have several OS-X Terminal.app windows open, how can I move one Terminal
I've read several articles about push button events in Qt but none seem to
In WPF most controls have MouseUp and MouseDown events (and the mouse-button-specific variations) but
I am using ExtJS to build a window containing several panels as items. One
I have several Facebook like buttons on myself that are being rendered via XFBML.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.