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 7439177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:40:06+00:00 2026-05-29T10:40:06+00:00

Is it possible to pass an argument to the listener when a button is

  • 0

Is it possible to pass an argument to the listener when a button is clicked:

protected void onCreate(Bundle savedInstanceState) {
    ....

    String myArg = "Hello";
    Button button1 = (Button)findViewById(R.id.myButton);
    button1.setOnClickListener(myListener);

    String myArg = "Goodye";
    Button button2 = (Button)findViewById(R.id.myOtherButton);
    button2.setOnClickListener(myListener);
}

OnClickListener clickListener = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
            // print the value from the passed in argument myArg
        Log.v("TEST", myArg);           
    }
};

This is just an example and not a reflection on what I actually wan’t to do but it does illustrate the question.

Basically I want to pass in myArg to the listener so when the button is clicked it has access to the myArg variable.

I know I could do this as an inline anonymous class but I will have lots of buttons and the click logic is the same for all of them. I need the argument as it will be different for each button.

FYI: My real usage requires that the argument be a View object.

  • 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-29T10:40:07+00:00Added an answer on May 29, 2026 at 10:40 am

    Why not just create a class that implements View.OnClickListener? This would ideal if you are reusing the code for each button as well.

    public class MyListener implements View.OnClickListener 
    {
        private View mView;
    
        public MyListener(View v) {
            mView = v;
        }
    
        @Override
        public void onClick(View v) {
            // Use mView here if needed             
        }
    }
    

    And then use it like you would normally:

    OnClickListener listener = new MyListener(myView);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible for a client to pass an RMI object as an argument
Possible Duplicate: Pass a PHP string to a Javascript variable (and escape newlines) I
Is it possible to pass a App setting string in the web.config to a
I'm wondering if is possible pass types by argument in Java. Let me explain
Is it possible to pass an array as an argument from a C# code
Is it possible to pass a function pointer as an argument to a function
Is it possible to pass a javascript function with arguments as an argument? Example:
Is it possible to pass the name of a function(say A) as an argument
is there possible to pass more than one keyword argument to a function in
Is it possible to pass an anonymous function as an argument, and have it

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.