Ari
Ari

Reputation: 3127

Finding list item root view by id

How to find a root view for an list item by id?

For example:

Activity layout:

<LinearLayout android:id="@+id/activity_root">
    <FrameLayout android:id="@+id/activity_2">
        <FrameLayout android:id="@+id/activity_3">
            <ListView android:id="@android:id/list" />
        </FrameLayout>
    </FrameLayout>
</LinearLayout>

List item layout:

<RelativeLayout android:id="@+id/item_root">
    <FrameLayout android:id="@+id/item_2">
        <FrameLayout android:id="@+id/item_3">
            <Button android:id="@+id/item_button" />
        </FrameLayout>
    </FrameLayout>
</RelativeLayout>

Now I need to add some action on button click. All I have is button view. How do I get item_root view?

Upvotes: 1

Views: 464

Answers (3)

varun bhardwaj
varun bhardwaj

Reputation: 1522

In onClick get the root view using the argument i.e view and call view.getRoot view but if its a list view than you can go for following but pass the activity i.e Activity.this to the adapter and find the root view like this:

mActivity.getWindow().getDecorView().findViewById(android.R.id.content)

Any feedback is appreciated.

Upvotes: 0

Vinothkumar Arputharaj
Vinothkumar Arputharaj

Reputation: 4569

Try this recursive function

 View getParent(View view, int targetId) {
    System.out.println(view.getId() +" == "+ targetId);
    if (view.getId() == targetId) {
        return view;
    }
    View parentView = (View) view.getParent();
    if (parentView == null) {
        return null;
    }
    return getParent(parentView, targetId);
}

Upvotes: 1

Nathua
Nathua

Reputation: 8826

you can use

view.getParent() 

to find outer parent, to most outer view you need to call a few and make check control.

Upvotes: 0

Related Questions