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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:32:40+00:00 2026-05-21T12:32:40+00:00

I have an Activity that, for each widget in the layout, I call setOnClickListener()

  • 0

I have an Activity that, for each widget in the layout, I call setOnClickListener() to assign my OnClick() handler. In my OnClick() handler I use a switch statement to execute the desired code for each button based on the View parameter’s ID. Is there a way to assign a default handler to the main view instead of having to make individual listener assignment calls for each widget in the view?

================================================

UPDATE

Thanks to kcoppock’s starting sample I have coded up a complete implementation of a class that has a static method that sets the click handler for all View elements in an Activity to a common click handler. This is for those situations where you have a simple layout and you want to do all the event handling in a common click listener event that uses a switch statement based on the View parameter object’s ID. To use it from an Activity, just call Misc.setDefaultClickHandler(this, this). Naturally your Activity needs to implement the View.OnclickListener interface.

package {put your package name here};

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;

public class Misc {

    public Misc() {
        super();
    }

    // Returns the root view for a given activity.
    public static View getRootView(Activity activity) {
        return activity.findViewById(android.R.id.content).getRootView();
    }

    private static void assignClickHandler(View root, View.OnClickListener theOnClickListener) {

        // Is it a View or a View group?
        if (root instanceof ViewGroup) {

            // It's a ViewGroup, process all it's children.
            ViewGroup vg = (ViewGroup) root;

            for(int i = 0; i < vg.getChildCount(); i++) 
                // Make recursive call.
                assignClickHandler(vg.getChildAt(i), theOnClickListener);
        } 
        else 
        {
            // Child is a View.  Set the desired context for the click handler.
            root.setOnClickListener(theOnClickListener);
        }
    }

    public static void setDefaultClickHandler(Activity activity, View.OnClickListener theOnClickListener) {
        assignClickHandler(getRootView(activity), theOnClickListener);
    }
}

— roschler

  • 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-21T12:32:40+00:00Added an answer on May 21, 2026 at 12:32 pm

    Not to my knowledge, but you could just use a loop, something like this:

    ViewGroup root = findViewById(R.id.my_root_layout);
    final Context context = this;
    assignClickHandler(root);
    
    public void assignClickHandler(int root) {
        for(int i = 0; i < root.getChildCount(); i++) {
            if(root.getChildAt(i) instanceof ViewGroup) {
                assignClickHandler(root.getChildAt(i));
            }
            else {
                (root.getChildAt(i)).setOnClickListener(context);
            }
        }
    }
    

    Note it calls recursively for any nested layouts within as well. I haven’t tested this so I might have messed up some syntax, but that idea should work, if you’re just looking to avoid manually setting every one.

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

Sidebar

Related Questions

I have a widget that supposed to call an Activity of the main app
I have an activity that has a TabHost containing a set of TabSpecs each
I have an activity that calls the new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to); setListAdapter(notes);
I have a widget that contains 4 buttons to show 4 stock prices, each
I have an activity that has two tabs and a list view in each.
i have created a workflow activity that do give the item creater of a
I have a web-based application that notifies users of activity on the site via
I have a design conundrum that I could use some feedback on, so I'm
I have a Widget i am working on, that allows the user to have
On my desktop, I have a little widget that tells me my current CPU

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.