Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

If I'm understanding you correctly, you could simply have a few different CustomButton extensions: say RelativeLayoutButton and LinearLayoutButton, and have them implement OnClickListener. Override onClick() within the extended class, and handle the implementation per Button type.

You can access the entire View hierarchy if needed. Say, something like this:

//assuming a reference to Button button

//get a reference to the parent
View v = button.getParent();
ViewGroup parent;
ArrayList<View> siblings = new ArrayList<>();

if(v instanceof ViewGroup) parent = (ViewGroup) v;
for(int i = 0; i < parent.getChildCount(); i++) {
    siblings.add(parent.getChildAt(i));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top