I have a RelativeLayout with a button inside it. Once the user clicks on that button, I would like to change the background of the parent view (RelativeLayout). I know I can do this by storing the parent view in a variable or setting a tag on the button, but I'd lime to avoid that (I have very good reasons for not wanting this). Isn't there a way to just access the parent view from the button itself?
Asked
Active
Viewed 3.0k times
23
-
What's wrong with using getParent()? – Tal Pressman Jan 11 '11 at 09:18
1 Answers
66
Try View.getParent()
:
Button yourBtn = (Button) findViewById(R.id.your_btn);
RelativeLayout yourRelLay = (RelativeLayout) yourBtn.getParent();

Floern
- 33,559
- 24
- 104
- 119
-
I'll give it a shot, but I didn't realize that you can cast .getParent() to the view that you want. I'll see if that works – user496854 Jan 13 '11 at 04:36
-
1I knew that the ViewParent was the base class, but it didn't even occur to me that i could cast it to the type of parent. What a fool! Thanks Floern. – Dean Thomas Jan 30 '11 at 15:33