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

  • Home
  • SEARCH
  • 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 6173103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:30:34+00:00 2026-05-23T23:30:34+00:00

I am a learning via a book so please forgive this newbie question. I

  • 0

I am a learning via a book so please forgive this newbie question.

I have a bunch of imageButtons in my xml, here is how one of them looks:

<ImageButton android:src="@drawable/level1" android:layout_width="wrap_content" 
android:id="@+id/imageButton1" android:layout_height="wrap_content" 
android:onClick="button_clicked1"></ImageButton>

and processing code:

public void button_clicked1(View v) {
    text1.setText("clicked");

    }

rather than have each button have its separate onClick code, is there anyway I can pass which button was clicked? for example button_clicked(1) and then button_clicked(2) instead of button_clicked1 like it is now (in the above example xml code)

or i have no choice but have to do it separately?

  • 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-23T23:30:34+00:00Added an answer on May 23, 2026 at 11:30 pm

    Kind of – what I like to do is make my View or Activity implement View.OnClickListener.

    public class MyView extends ImageButton implements OnClickListener
    

    Then during onCreate, I do something like:

    ((ImageButton)findViewById(R.id.imageButton1)).setOnClickListener(this);
    

    then, in my onclick:

    public void onClick(View view){
     switch(view.getId()){
       case R.id.imageButton1:
          // do something.
          break;
       case R.id.imageButton2:
          // do somethign else.
          break;
     }
    

    Of course, you can definitely get creative and toss the switch statement if any of your buttons should trigger the same event behavior. Also, I’m not in a place where I can easily view my droid references so there may be an OnClickListener specific to ImageButton – if so, implement that on your containing View or Activity to consolidate the handlers…

    Hope that makes sense – happy coding!

    B

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a noob learning Android via a book, i have a quick question.
I am learning via a book and it gives me this example: Handler handler=new
I'm learning to write custom type conversions in C# and I have a question
This is going to sound like a silly question, but I'm still learning C,
Am a noob learning Android via a book and for threading I see there
I am learning Android via a book and would just like to confirm something.
I am learning this via Sams Teach Yourself Android in 24 hours. This is
I'm just learning Java... I have 2 custom classes. One is a Fraction and
Sorry in advance if this is a basic question. I'm reading a book on
I am just learning about the impressive SlickGrid library; and here's my question: We

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.