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.
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).
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.
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.
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 −
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:
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.
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".
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()
.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With