get parent's view from a layout

jquery get parent
javascript get parent element from child
jquery get parent id
how to get parent element id in javascript
htmldivelement get parent
javascript get parent element by class
javascript get parent div of button
jquery parent of parent

I have a FragmentActivity with this layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/menulabel"
    android:textSize="24sp" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="text"
    android:layout_below="@+id/textView1"
    android:hint="@string/search_title_hint" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/spinnersearch"
    android:layout_below="@+id/editText1" />

<LinearLayout
    android:id="@+id/layout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView2" >

    <Spinner
        android:id="@+id/spinner_search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:prompt="@string/spinnersearch" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/reset_search" />

</LinearLayout>

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/buttonnewcat"
    android:layout_below="@+id/layout1" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/buttondelcat" 
    android:layout_below="@+id/button2" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/buttoneditcat"
    android:layout_below="@+id/button3" />

<Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/buttonnewtext"
    android:layout_below="@+id/button4" />

<Button
    android:id="@+id/button6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/buttondeltext"
    android:layout_below="@+id/button5" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <side.menu.scroll.ScrollerLinearLayout
        android:id="@+id/menu_content_side_slide_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:background="@drawable/ab_solid_example"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/main_tmp_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/actionbar_background"
                android:src="@drawable/download" />

        </LinearLayout>
        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/bg_groud" />

    </side.menu.scroll.ScrollerLinearLayout>
</RelativeLayout>

and the ScrollerLinearLayout class is something like this:

public class ScrollerLinearLayout extends LinearLayout {
    // code of the class
}

How to access from ScrollerLinearLayout to the parent layout to get - for example - the editetxt? FragmentActivity and ScrollerLineraLayout are in different packages of the same projects. I tried to use the getParent() function in this way within the ScrollerLinearLayout but it doesn't work.

RelativeLayout r = (RelativeLayout) this.getParent().getParent();
int w = r.findViewById(R.id.editText1).getLayoutParams().width;

Someone help me? Thanks!

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 debug, split this up into multiple parts:

ViewParent parent = this.getParent();
RelativeLayout r;
if (parent == null) {
    Log.d("TEST", "this.getParent() is null");
}
else {
    if (parent instanceof ViewGroup) {
        ViewParent grandparent = ((ViewGroup) parent).getParent();
        if (grandparent == null) {
            Log.d("TEST", "((ViewGroup) this.getParent()).getParent() is null");
        }
        else {
            if (parent instanceof RelativeLayout) {
                r = (RelativeLayout) grandparent;
            }
            else {
                Log.d("TEST", "((ViewGroup) this.getParent()).getParent() is not a RelativeLayout");
            }
        }
    }
    else {
        Log.d("TEST", "this.getParent() is not a ViewGroup");
    }
}

//now r is set to the desired RelativeLayout.

jQuery parent() Method, Tip: To traverse a single level down the DOM tree, or all the way down to the last descendant (to return children or other descendants), use the children() or find()� The parent() method returns the direct parent element of the selected element. The DOM tree: This method only traverse a single level up the DOM tree. To traverse all the way up to the document's root element (to return grandparents or other ancestors), use the parents() or the parentsUntil() method.

If you are trying to find a View from your Fragment then try doing it like this:

int w = ((EditText)getActivity().findViewById(R.id.editText1)).getLayoutParams().width;

.parent(), Description: Get the parent of each element in the current set of matched elements, optionally filtered by a selector. version added: 1.0.parent( [selector ] ). selector. So what can parents do to ease the strain on their finances? Here are four ways to get your parent PLUS debt under control this year. 1. Income-contingent parent PLUS loan repayment plan. Pros: Lowers monthly payments and offers parent PLUS loan forgiveness after 25 years. Cons: Likely increases total interest charges. Requires paying a higher

The RelativeLayout (i.e. the ViewParent) should have a resource Id defined in the layout file (for example, android:id=@+id/myParentViewId). If you don't do that, the call to getId will return null. Look at this answer for more info.

.parents(), Description: Get the ancestors of each element in the current set of matched elements, optionally filtered by a selector. version added: 1.0.parents( [selector ] ) . Changing the parent will modify the parent-relative position, scale and rotation but keep the world space position, rotation and scale the same. See Also: SetParent.

This also works:

this.getCurrentFocus()

It gets the view so I can use it.

Node.parentElement, parentElement read-only property returns the DOM node's parent Element, or null if the node either has no parent, or its parent isn't a DOM Element. Get the latest and greatest from MDN delivered straight to your inbox. The Window.parent property is a reference to the parent of the current window or subframe.. If a window does not have a parent, its parent property is a reference to itself.

Check my answer here

The use of Layout Inspector tool can be very convenient when you have a complex view or you are using a third party library where you can't add an id to a view

Getting the parent div of element, You're looking for parentNode , which Element inherits from Node : parentDiv = pDoc.parentNode;. Handy References: DOM2 Core� The Node.parentElement read-only property returns the DOM node's parent Element, or null if the node either has no parent, or its parent isn't a DOM Element. Syntax parentElement = node.parentElement. parentElement is the parent element of the current node. This is always a DOM Element object, or null. Example

JavaScript Get the Parent Element parentNode, Summary: in this tutorial, you will learn how to get the parent node of an element by using the JavaScript parentNode attribute of the Node object. Get ready to rock your block party this Fourth of July! Gather friends into your star-spangled backyard for a day of silly costumes, serious lip-syncing, and macaroni necklaces. The 'Parent

GetParent function (winuser.h), GetParent function. 12/05/2018; 2 minutes to read. In this article. Syntax; Parameters; Return value; Remarks; Requirements; See also. Retrieves a handle to the� You can only get summaries if your student is using Classroom with a G Suite account, which looks like student_name@school.edu. If you’re not sure, ask the teacher or school administrator. Get email summaries. To get email summaries of your student’s work, you must accept an email invitation from a teacher or administrator.

jQuery, The parent() is an inbuilt method in jQuery which is used to find the parent In the above code only the parent element of the selected element is get deep� Parent definition is - one that begets or brings forth offspring. How to use parent in a sentence.

Comments
  • What exactly does not work?
  • @FD_ it is a nullpointerexception on RelativeLayout r = (RelativeLayout) ((ViewGroup) this.getParent()).getParent();
  • Where in ScrollerLinearLayout do you call this code?
  • @FD_ in a method init()
  • Do you add it to a ViewGroup before calling init() ?
  • i tried to do this but it doesn't work. the error is nullpointexception on RelativeLayout r = (RelativeLayout) ((ViewGroup) this.getParent()).getParent(); i don't understand what can be...
  • @Rajin, I added a section of my post to help you debug this issue.
  • i checked the log and i found that ViewParent parent = this.getParent(); returns null. I have this public class ScrollerLinearLayout extends LinearLayout { RelativeLayout r; // some code public void init(){ // your code } } is correct where i call the getParent() method?
  • the getActivity() method is undefined for the type ScrollerLineraLayout which extends LinearLayout class
  • this simply return the parenting view not the actual layout like just level one parent