cspam
cspam

Reputation: 2941

How to determine which RelativeLayout is the parent of a button?

Lets say that I have two different types of RelativeLayouts. That is to say these 2 RelativeLayouts differ because they contain different views. One might have textviews, an image view etc and the other might have also have some textviews which mean something completely different than the other set of textviews in the other relativelayout. Lets say however that both have a Submit Button. So to make my point more clear here is some code:

     @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        ViewParent parent = v.getParent();
        if(parent instanceof CustomRelativeLayout){

            CustomRelativeLayout aRelativeLayout = (CustomRelativeLayout)parent;                    

            for(int childrenIndex = 0; childrenIndex < r.getChildCount(); childrenIndex++){

                View childView = aRelativeLayout.getChildAt(childrenIndex);    


            }
        }

    }

The problem here is is that we don't know which CustomRelativeLayout aRelativeLayout is referring to. Depending on which CustomRelativeLayout it is will depend on what specific childViews i will want to search for and what logic I implement relevant to those views. I would like to be able to have a switch statement to check which type of CustomRelativeLayout is the parent.

So the questions that i would like to ask is:

  1. how do i get more information about which instance of CustomRelativeLayout refers to the button that was clicked? Is there a way to get the instance variable name?

  2. Once i have found out that information how do i get specific information about the children of the parent view that i am working on? The thought is, is that i might have 30 child views in the parent but i am only interested in one specific view(i might want to get the text of one specific textview as an example). I will know to look for it specifically because i would have done a switch statement on the different instances of my CustomRelativeLayouts(the first question) and therefore i know which view i want to look at, which logic to perform or what other methods that i need to call.

Would appreciate any thoughts or help with this.

Upvotes: 0

Views: 1217

Answers (2)

blazeroni
blazeroni

Reputation: 8350

For your first question, there are couple options:

  • Use separate OnClickListeners for each button. Then, each button will only trigger its own listener's onClick() method.
  • You can give each button a different id either in XML (via the android:id property) or in code (via setId(int id)). Then in onClick() you can check the id of the View that was passed as the argument.

For your second question:

Since you have the parent ViewGroup, you can find specific views within it by using:

TextView interestingView = (TextView) parent.findViewById(R.id.interesting);

This will only search the children of the parent view.

If you need to get an unknown number of views the best strategy is probably iterating through them like you are now. You can identify groups of views by setting a tag either in XML (android:tag) or code (setTag(Object tag) and check them as you iterate. For example, if you have a set of TextViews and in each one is either a color or an animal, you might handle that like this:

// defined elsewhere
private static final String TAG_COLOR = "color";
private static final String TAG_ANIMAL = "animal";

...

int count = parent.getChildCount();
for (int i = 0; i < count; i++){
    View view = parent.getChildAt(i);
    if (TAG_COLOR.equals(view.getTag()) {
        // handle color
    } else if (TAG_ANIMAL.equals(view.getTag()) {
        // handle animal
    }
}

Upvotes: 2

Davidsun
Davidsun

Reputation: 721

There are several ways to do this.

  1. Use "id" to identify views. You can give different id to different views, and then it will be possible to identify them. See http://developer.android.com/reference/android/view/View.html#getId() (the API documentation of View.getId()) for more information.

  2. Mark each view with different tags, and identify them through tags. See http://developer.android.com/reference/android/view/View.html#getTag() (the API documentation of View.getTag()) for more information.

  3. If you want to customize more, just inherit default Android views, and use "instanceof" to identify them.

Upvotes: 0

Related Questions