1

I am inflating a layout inside an Android Fragment. The layout to be inflated has the following include tag:

<include layout="@layout/middle_multi_game_card"  //NEED TO LOCATE VIEW INSIDE THIS LAYOUT
    android:id="@+id/includeID"
    android:tag="@+id/big_game_card_tag"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="6"
    />

The layout in which the view reference I need has the following structure:

//middle_multi_game_card:

<androidx.coordinatorlayout.widget.CoordinatorLayout
  android:id="@+id/theroot_"
 >

<androidx.constraintlayout.widget.ConstraintLayout

>

</androidx.constraintlayout.widget.ConstraintLayout>

<androidx.appcompat.widget.AppCompatImageView   // **I NEED A REFERENCE TO THIS VIEW!**
 android:id="@+id/my_image_view"
 android:tag="thegamepiece_"
</androidx.appcompat.widget.AppCompatImageView>

 
</androidx.coordinatorlayout.widget.CoordinatorLayout>

I have thought of using a ViewTreeObserver object to get the reference to the view, but I already use the ViewTreeObserver and I don't want to use it again because of performance issues.

I have tried the following inside onViewCreated method:

onViewCreated(@NonNull View view, @Nullable Bundle 
savedInstanceState)
{ 
View viewf = view.findViewById(R.id.includeID); //this is found!!
View view_ = viewf.findViewById(R.id.theroot_); //not found at all!
 imageview = 
(AppCompatImageView)view_.findViewById(R.id.my_image_view);//not found at all!
} 

How can I get a reference to this ImageView via findViewByID or findViewByTag calls?

i_o
  • 777
  • 9
  • 25
  • Does this answer your question? [How to access Button inside "include" layout](https://stackoverflow.com/questions/4787008/how-to-access-button-inside-include-layout) – talhatek Jul 10 '22 at 19:40

2 Answers2

0

The findViewByID will recursively search the whole tree hierarchy. That being that you easily call

fragmentView.findViewByID(R.id. my_image_view)

in your onCreateView or onViewCreated methods

Updated, try to use:

onViewCreated(@NonNull View view, @Nullable Bundle 
savedInstanceState)
{ 
AppCompatImageView imageview = 
(AppCompatImageView)view.findViewById(R.id.my_image_view);
} 
Ihor Bykov
  • 1,843
  • 3
  • 15
  • 22
  • you mean it will even search inside the layout reference by the include tag? – i_o Jul 10 '22 at 19:40
  • I tried this and it did not work: // inside onviewcreated of the fragment see: : public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { View view_ = view.findViewById(R.id.root); imageview = view_.findViewByTag("thegamepiece_"); } – i_o Jul 10 '22 at 19:58
  • Can you share how do you inflate view in your fragment? – Ihor Bykov Jul 10 '22 at 19:59
  • you are calling a tag, you need to call `AppCompatImageView imageView = view.findViewById< AppCompatImageView>(R.id.my_image_view)` – Ihor Bykov Jul 10 '22 at 20:01
  • '@'Nullable '@'Override public View onCreateView('@'NonNull LayoutInflater inflater, '@'Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { main_layout = (LinearLayout) inflater.inflate(R.layout.my_multi_game_layout,container,false); return main_layout; } ignore the ' ' – i_o Jul 10 '22 at 20:02
  • What is `my_multi_game_layout`? – Ihor Bykov Jul 10 '22 at 20:06
  • Have you tried this `AppCompatImageView imageView = view.findViewById(R.id.my_image_view)` ? – Ihor Bykov Jul 10 '22 at 20:08
  • android studio is not recognizing: findviewbyid<> the notation <> is not recognizable! – i_o Jul 10 '22 at 20:17
  • ok, try casting then `AppCompatImageView imageView = (AppCompatImageView)view.findViewById(R.id.my_image_view)` – Ihor Bykov Jul 10 '22 at 20:28
  • I have done the casting but It did not work. Please read the update of the question. It seems that I can only get a reference to the included layout via its android ID, but this reference cannot get reference to its own root! – i_o Jul 10 '22 at 20:32
  • Don't use `findViewByTag`, please use `findViewById`. Please read all my comments carefully. Instead of `findViewByTag` use `AppCompatImageView imageView = (AppCompatImageView)view.findViewById(R.id.my_image_view)` – Ihor Bykov Jul 10 '22 at 20:33
  • I did used findviewbyID also.. the Logs tell me that the root of the included layout is not found.. it is null! So, I can not access the imageview! Read the update part of my question at the bottom of the question see what I did try on the method onviewcreated() – i_o Jul 10 '22 at 20:35
  • You DON'T need to access root from included layout, you can use `view` object that you received in `onViewCreated` – Ihor Bykov Jul 10 '22 at 20:37
  • I was using view object from onviewcreated before but It was still telling me that it could not find imageview! I will try again! – i_o Jul 10 '22 at 20:39
  • I updated answear – Ihor Bykov Jul 10 '22 at 20:39
0

Ok I was able to find the correct answer:

in the include tag we have to make sure we have the same android id of the root view within the included layout!

<include layout="@layout/middle_multi_game_card"  //NEED TO LOCATE VIEW INSIDE THIS LAYOUT
android:id="@+id/includeID"
android:tag="@+id/big_game_card_tag"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
/>

Now, in the root view of the included layout, which is: middle_multi_game_card.xml

WE MUST HAVE THE SAME ID OF THE INCLUDE TAG! WHICH IS FROM ABOVE: includeID

Now, we can access all the views from inside the included layout!

i_o
  • 777
  • 9
  • 25