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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:25:47+00:00 2026-05-31T02:25:47+00:00

As you know we can override a few methods for each type of View

  • 0

As you know we can override a few methods for each type of View that we have created. I am not doing any xml layout code because a lot of stuff changes form at runtime and there are things that are created dynamically so the programmatic solution is the best route for me here.

So to get to the gist of my issue lets say that i have 50 buttons in my Android App. These buttons could potentially be on one Activity but more than likely could span out across multiple screens(Activites).

I have created a button class called CustomButton that overrides the onClick(View view) method. What if some of the buttons are supposed to do some action(lets say they are a part of a linearlayout) and some are part of something like a relative layout and in each relative layout you want to get information about the relative layout that this button resides in(perhaps you what to get information about all the textviews in the same relative layout, etc etc).

One solution of course is that you could get the ID of each button and do a switch(case) or something and depending on what the ID of the button that is returned you can do something. The problem is is i have 50 buttons. What if i had 200? Should i really have to do 200 case checks in my switch statement to figure out what action i need to take?

So i am trying to figure out what information is available to me that i am not aware of that I could use when override the onClick.

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    if(!getClickInfo()){
        ((CustomButton)v).setText("I clicked the button");
        setClickInfo(true);
    }
    else{
        isClicked = false;
        ((CustomButton)v).setText("This is the button that i want to click");


    }

}

so here is an example. I have passed in the View that represents the button that was clicked and i can change the text of the button as i click on it over and over again. But what if the Button was part of a relative layout? Can i get any other information about other views that also are a part of the same relative layout? What if some buttons i wanted to change the background color of each time i clicked it instead of changing the text? If I have X number of buttons I am trying to stay away from creating X number of IDs — as i pointed out you could potentially have hundreds of them.

Was wondering if anyone might have any ideas on what would be a better route here than having a potentially unlimited about of case checks inside a switch statement. If i could find out which view the button came from(in my case the button was part of an outer view) then i could do a switch on my views instead of just my buttons – and making my switch statement much less cumbersome and easier to maintain. thanks.

  • 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-31T02:25:48+00:00Added an answer on May 31, 2026 at 2:25 am

    I’m not sure as what your end goal is. But from your view (button) you can get its parent (e.g. a RelativeLayout). Then once you have your view group you can get its children and do whatever you need to.

    For example:

    @Override
    public void onClick(View v) {
        ViewParent parent = v.getParent();
        if (parent instanceof RelativeLayout) {
            // your button is inside a RelativeLayout
            RelativeLayout rl = (RelativeLayout) parent;
            int numChildren = rl.getChildCount();
            // loop through children if you need to (use rl.getChildAt(index) to get each child)
            ...
        }
    }
    

    If you don’t need to know if it’s actually a RelativeLayout or LinearLayout, or whatever just use ViewGroup.

    Hope it helps.

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

Sidebar

Related Questions

I know you can override delete and save methods in DJango models, but can
I have a query that I know can be done using a subselect, but
I have a custom class that has quite a few accessor methods for customizing
I'm doing an admin page that can view the users that exist in the
I don't know why , i can't override back button: I tried everything I
I want to know can we have a JPanel with a Layout other than
Sometimes, I'll end up having to catch an exception that I know can never
I know you can not set a key value dynamically, but what about the
I know that there are a few Questions here on SO relating to this,
Okay, I know you can override the to_xml method for a single instance of

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.