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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:12:42+00:00 2026-05-18T11:12:42+00:00

I got 4 activites that all include a xml-footer which contains 4 buttons (one

  • 0

I got 4 activites that all include a xml-footer which contains 4 buttons (one for each activity).
I would now like to setup onclicklisteners to these buttons (it’s a self made menu in the footer).

The question is, how do I use listeners so that I can reuse code?
I have two ideas:

  1. Create a class that implements onclicklistener and in every activity i would get the buttons and then create a new instance of the listener class and do button.setOnClickListener(onClickListener)
    The problem is that in the listener class, how would i check which button called the event?
    And how would I create an intent to start an activity, usually i would do:
    Intent intent = new Intent(FromActivity.this, ToAcitivty.class)
    But i don’t have the reference to FromActivity.

  2. Create a base class that extends from activity and then the 4 activies will extend from the base class. I would then like to setup the listeners in the base class. The problem here is that i can’t get the references to the buttons by doing
    Button button1 = (Button)findViewById(R.id.menu_button1);
    button1 will be null. I haven’t even called setEventView because this should be done in the activity not in the base class.

Any ideas?

Thank you

  • 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-18T11:12:43+00:00Added an answer on May 18, 2026 at 11:12 am

    Same code is here:

        public class MyClass extends Activity implements View.OnClickListener{
            btnA=(Button)findViewById(R.id.btnA);
            btnA.setOnClickListener(this);
            btnB=(Button)findViewById(R.id.btnB);
            btnB.setOnClickListener(this);
    
    
        }
        @Override
        public void onClick(View v)
        {
            Button clickedButton = (Button) v;
            switch (clickedButton.getId())
            {
                case R.id.btnA:
                    Intent regIntent = new Intent(Home.this,Registration.class);
                    startActivityIfNeeded(regIntent, 1);
                    break;
                case R.id.btnB:
                    //Some code
                    break;
            }
        }
    

    (edited as the original first line is broken on code format.

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

Sidebar

Related Questions

Got a class that serializes into xml with XMLEncoder nicely with all the variables
I've been trying to use an android preference activity, I got it all set
I got an Activity that when it starts, it loads an image from the
Let's say I've got a SQL Server table that logs user activity. Let's say
So, Here is my scenario: I have two activity and one service --> all
Got a bluescreen in windows while cloning a mercurial repository. After reboot, I now
All, I'm attempting to write a Junit test that calls a Web Service client
I need to build an activity feed to go on each users profile page
I've got a common task that I do with some Activities - downloading data
I've got an Android app which has a periodic background Service. I want this

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.