Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Making changes to a button's parent view

Tags:

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?

like image 697
user496854 Avatar asked Jan 11 '11 09:01

user496854


People also ask

How to set parent activity in android?

Declare a Parent Activity You can do this in the app manifest, by setting an android:parentActivityName attribute. The android:parentActivityName attribute was introduced in Android 4.1 (API level 16). To support devices with older versions of Android, define a <meta-data> name-value pair, where the name is "android.

What is android view view?

What is Android View? A View is a simple building block of a user interface. It is a small rectangular box that can be TextView, EditText, or even a button. It occupies the area on the screen in a rectangular area and is responsible for drawing and event handling.

What is view tag in android?

Tags are essentially an extra piece of information that can be associated with a view. They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure. Reference: http://developer.android.com/reference/android/view/View.html.

What is GUI button Android?

A button consists of text or an icon (or both text and an icon) that communicates what action occurs when the user touches it. Depending on whether you want a button with text, an icon, or both, you can create the button in your layout in three ways: With text, using the Button class: <Button.


1 Answers

Try View.getParent():

Button yourBtn = (Button) findViewById(R.id.your_btn); RelativeLayout yourRelLay = (RelativeLayout) yourBtn.getParent(); 
like image 127
Floern Avatar answered Oct 06 '22 00:10

Floern