How to get instance of view's parent fragment:
Solution 1
I have a general advice for you that would solve your problem and help you in the future -
don't use android:onClick in the xml file, use setOnClickListener in the code itself - need to avoid mixing your views with other parts of the app as much as possible.
Try to keep the Fragment independent of its activity.
If the image is part of the fragment, why does the listener is part of the FragmentActivity?
Use setOnClickListener in the fragment itself, and you might be able to use this Framgent in other pats of the app without being depended on the Activity.
It would also solve your problem of identifying the fragment in which the image was clicked.
Solution 2
v is not an instance of Fragment
, that's why Eclipse does not like your code. If you want the instance of a fragment you have to use the FragmentManager
and one of its findFragmentByXXX methods.
Emil Adz
Education: B.S. Computer Science, 2012, HIT. Occupation: Mobile Architect & Co-Founder at Digital Cherry Ever seen a dog chase its tail? Now that's an infinite loop ; ) #SOreadytohelp
Updated on June 04, 2022Comments
-
Emil Adz almost 2 years
I have a fragment with an
XML
layout file. in it I have an 2 clickableImageView
s. for eachImageView
I set anonClick
method for example: android:onClick="commentFragmentRemoveOnClick".In the FragmentActivity (The Activity no the Fragment) I defined it this way:
public void commentFragmentRemoveOnClick(View v) { }
No this Fragment is of type
CommentFragment
and it has apublic void getFragmentTag()
method to get it's tag that I save in earlier time. I need to get an instance of the fragment in which the image was clicked to get it's tag.I tried:
((CommentFragment)v).getParentFragment().getFragmentTag();
and:
((CommentFragment)v).getParent().getFragmentTag();
but eclipse gives me error on both of them, how is this done properly?
To make it more clear this is my
CommentFragment
:public class CommentFragment extends Fragment { private final static String TAG = CommentFragment.class.getSimpleName(); private String fragmentTag; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.comment_fragment_layout, container, false); Bundle bundle = getArguments(); String text = bundle.getString("comment"); String fullUser = bundle.getString("user"); String user = fullUser.substring(0, fullUser.indexOf("@")); String at = bundle.getString("at"); TextView tvCmment = (TextView) rootView.findViewById(R.id.tvComment); TextView tvUser = (TextView) rootView.findViewById(R.id.tvUser); TextView tvAt = (TextView) rootView.findViewById(R.id.tvDate); tvCmment.setText(text); tvUser.setText(user); tvAt.setText(at); return rootView; } public void setText(String item) { TextView view = (TextView) getView().findViewById(R.id.tvComment); view.setText(item); } public void setFragmentTag(String tag) { this.fragmentTag = tag; } public String getFragmentTag() { return this.fragmentTag; } }
and the layout file:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/llCommentContainer" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="@drawable/try2"> <TextView android:id="@+id/tvUser" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tvComment" android:layout_alignParentTop="true" android:background="@color/my_gray" android:text="demo" android:textStyle="bold" android:paddingLeft="5dp" android:paddingRight="5dp" android:textColor="@color/my_even_darker_gray" /> <TextView android:id="@+id/tvComment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/tvDate" android:padding="5dp" android:text="This task is described in more details if you click on it." android:textColor="@color/my_even_darker_gray" /> <TextView android:id="@+id/tvAt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:paddingRight="5dp" android:textColor="@color/my_even_darker_gray" android:layout_toRightOf="@+id/tvUser" android:background="@color/my_gray" android:text="at" /> <TextView android:id="@+id/tvDate" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/tvAt" android:layout_alignBottom="@+id/tvAt" android:layout_toRightOf="@+id/tvAt" android:background="@color/my_gray" android:text="12/02" android:textColor="@color/my_even_darker_gray" /> <ImageView android:id="@+id/iEdit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/tvComment" android:layout_marginRight="4dp" android:clickable="true" android:contentDescription="@drawable/add_comment_button" android:onClick="commentFragmentEditOnClick" android:src="@drawable/add_comment_button" /> <ImageView android:id="@+id/iRemove" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/iEdit" android:layout_toRightOf="@+id/iEdit" android:layout_marginRight="4dp" android:clickable="true" android:contentDescription="@drawable/add_comment_button" android:onClick="commentFragmentRemoveOnClick" android:src="@drawable/add_comment_button" /> </RelativeLayout>
I would love for a little assistance.
Thanks.
-
Emil Adz about 11 yearsI know v is not a fragment, v is a ImageView inside of a fragment, I need to get the instance of this fragment by this ImageView. My fragments are added dynamicly and have no id's. they only have tags which i assign to them via the setFragmentTag and getFragmentTag which I'm tring to use right now.
-
Cristian about 11 yearsIn that case you use findFragmentByTag. Also, if you know v is not a fragment, why did you do
((CommentFragment)v)
? -
Emil Adz about 11 yearsYou are not getting my question: To use findFragmentByTag method via FragmentManager I need to get instance of the fragment in which the view was clicked in , and from it extract it's assigned tag that I assigned to it earlier. And that's what I'm trying to do if you look carefully you will see that I am trying to get the parent of the click View, I only don't know exactly how it done.
-
Cristian about 11 yearsNow I got it. So... a few tips: try to avoid using
android:onClick
and rather assign your click listeners manually. Also, if you have dynamically added fragments you can set the tag of the fragment to your image views. That way, if you want the fragment tag of the fragment containing the clickedImageView
you just have to dov.getTag()
-
Emil Adz about 11 yearsA little more background: if you look here: stackoverflow.com/questions/15484530/… it might be more clear what I'm trying to achieve. and please look at the question again, I have added code. So if I set the Tag to the ImageView I still have to use the getFragmentTag method. is there no way to get it's instance? it's the container of the view that I am clicking on, there must a connection between them I can use.
-
Emil Adz about 11 yearsThanks @Pinhassi, but if you look at the code a supplied yesterday as an answer, that exactly what I did. but thanks again for your help and Hag sameah : )