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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:28:17+00:00 2026-05-23T00:28:17+00:00

Pre-Honeycomb (Android 3), each Activity was registered to handle button clicks via the onClick tag

  • 0

Pre-Honeycomb (Android 3), each Activity was registered to handle button clicks via the onClick tag in a Layout’s XML:

android:onClick="myClickMethod"

Within that method you can use view.getId() and a switch statement to do the button logic.

With the introduction of Honeycomb I’m breaking these Activities into Fragments which can be reused inside many different Activities. Most of the behavior of the buttons is Activity independent, and I would like the code to reside inside the Fragments file without using the old (pre 1.6) method of registering the OnClickListener for each button.

final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Perform action on click
    }
});

The problem is that when my layout’s are inflated it is still the hosting Activity that is receiving the button clicks, not the individual Fragments. Is there a good approach to either

  • Register the fragment to receive the button clicks?
  • Pass the click events from the Activity to the fragment they belong to?
  • 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-23T00:28:18+00:00Added an answer on May 23, 2026 at 12:28 am

    You could just do this:

    Activity:

    Fragment someFragment;    
    
    //...onCreate etc instantiating your fragments
    
    public void myClickMethod(View v) {
        someFragment.myClickMethod(v);
    }
    

    Fragment:

    public void myClickMethod(View v) {
        switch(v.getId()) {
            // Just like you were doing
        }
    }    
    

    In response to @Ameen who wanted less coupling so Fragments are reuseable

    Interface:

    public interface XmlClickable {
        void myClickMethod(View v);
    }
    

    Activity:

    XmlClickable someFragment;    
    
    //...onCreate, etc. instantiating your fragments casting to your interface.
    
    public void myClickMethod(View v) {
        someFragment.myClickMethod(v);
    }
    

    Fragment:

    public class SomeFragment implements XmlClickable {
    
    //...onCreateView, etc.
    
    @Override
    public void myClickMethod(View v) {
        switch(v.getId()){
            // Just like you were doing
        }
    }    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use the Android support library to use fragments in pre Honeycomb Applications. If
Is there some library that: Brings ActionBar API to pre-Honeycomb Android versions. Is based
I have an Android application for pre-Honeycomb devices that uses custom tabs to launch
I read http://android-developers.blogspot.com/2012/01/say-goodbye-to-menu-button.html but have some issues. For pre-honeycomb I want a custom title,
If you want to pre-populate a database (SQLite) in Android, this is not that
I've created three app widgets with different sizes for pre Honecomb versions of Android
I see some kind of action bars in some pre-honeycomb apps. For example in
I have an app where you enter some data. The PRE-Honeycomb behavior is the
I'm trying to implement a drag-drop functionality for pre-honeycomb devices. I'd like to touch
Pre-API Level 14 there is no switch preference. If I use preferences.xml to create

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.