Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to get the ID of a parent View?

Tags:

android

View.getRoot() returns View, so we can easily figure out which is the root view by using getResourceName(View.getId()).

View.getParent()..., while I expect it also returns View that is the parent, actually only returns an instance of ViewParent that seems to have very very few useful method/fields. It sucks.

So, is there any way to know the ID of the parent? I believe a View's parent is also View, thus it should has mID field.

I really wonder why Google didn't let View.getParent() just returns View. It makes sense, only when something else other than View could be the parent, and as far as I know, it's limited to View and its subclasses.

like image 495
Quv Avatar asked Jan 01 '13 16:01

Quv


People also ask

What is parent View in android?

A ViewGroup "is a special view that can contain other views (called children.)" It's the superclass for all of the Layout classes. Parent view refers to a specific parent-child relationship (This RelativeLayout is the parent of this ImageView).

What is parent View and child View android?

In android (and most other technologies), views can have subviews, aka "children". Most views can have children and can be a child of a parent view. It's kind of like a "tree". The most obvious feature of being a child of some other view is that the child moves with the parent view.

What is View parent?

Defines the responsibilities for a class that will be a parent of a View. This is the API that a view sees when it wants to interact with its parent.

How to add child view in Android Studio using Java?

Step 1 − Create a new project in Android Studio,go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above example we don't have any view, we have created a root view /parent view. we can add child view in java as shown below −

How do I apply an ID to a view element?

However, if you have already defined an ID resource (and it is not already used), then you can apply that ID to a View element by excluding the plus symbol in the android:id value. The height and width value can be expressed using any of the dimension units supported by Android (px, dp, sp, pt, in, mm) or with the following keywords:

How to get parent of a viewgroup?

The getParent method returns a ViewParent, not a View. You need to cast the first call to getParent() also: RelativeLayout r = (RelativeLayout) ((ViewGroup) this.getParent()).getParent(); As stated in the comments by the OP, this is causing a NPE.

How to find a view programmatically in Android?

To find a view programmatically, the View (LinearLayout / TextView / Button / ImageView / EditText etc.) should have been set with an id in the layout xml file as shown below : Button View with id, in layout xml file. <Button. android:id="@+id/button_submit".


3 Answers

ViewParent is just an interface that any View that can have children implements. Most of the times the class you get back will be an instance of a ViewGroup, like LinearLayout or RelativeLayout. I'm not exactly sure what you mean by "name" but if you want to get the class name you can do so like always: view.getParent().getClass().getName().

like image 141
dmon Avatar answered Oct 22 '22 06:10

dmon


The docs state that the parent is not necessarily a View:

public final ViewParent getParent ()

Added in API level 1 Gets the parent of this view. Note that the parent is a ViewParent and not necessarily a View.

Returns Parent of this view.

However all implementations of ViewParent inherit from View. This should be a design decision to decouple the parent from a View using the ViewParent interface, although all implementations in the SDK are views.

like image 6
aromero Avatar answered Oct 22 '22 06:10

aromero


Try casting it first. For example, if the parent of the View that you're trying to get id of is a TableRow, then do

((TableRow)View.getParent()).getID()
like image 3
Emrys90 Avatar answered Oct 22 '22 07:10

Emrys90