1

Just wondering if any of you guys could help me to access an element from an 'include' layout file which is inside not an activity but a fragment?

Most posts tell me to do something like that: How to access Button inside "include" layout but as I am not on an activity the listener for the image won't work.

Ps: The listener works (opens the nav drawer) when I call an element which is not in the include file but on the fragment layout.

That's what I have inside the onCreateView method for my fragment:

View myLayout = view.findViewById(R.id.layout_top_bar);
        image = (ImageView) myLayout.findViewById(R.id.image_left_top_bar);
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity mainActivity = (MainActivity)getActivity();
                mainActivity.openMenuDrawer();
            }
        });

That's my include layout file:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_top_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">

<ImageView
    android:id="@+id/image_left_top_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/hamburger_icon" />

And that's how I'm including it within the fragment layout file:

 <include layout="@layout/top_bar" />

Thanks very much for any help on that! :)

Francislainy Campos
  • 3,462
  • 4
  • 33
  • 81

3 Answers3

2

You can access it directly with the root layout of your fragment like this.

image = view.findViewById(R.id.image_left_top_bar);

In case we are adding the layout dynamically by inflating it, we use the approach you mentioned above but that's not needed here.

knight
  • 105
  • 2
  • 5
Ravinder Bhandari
  • 2,546
  • 21
  • 25
  • Sorry, tried that but my image listener won't work. Thanks, though! – Francislainy Campos Jul 18 '17 at 19:37
  • @FrancislainyCampos that's a different question. His answer is correct fo how to get the view. Define "Your image listener doesn't work"- does a breakpoint in there get hit, or does it not get called?. Also, do you have the same include used multiple times (in which case getting the view is hard due to id collision) or do you use the same id elsewhere in your main layout or any other includes (in which case you have an id collision). – Gabe Sechan Jul 18 '17 at 19:40
  • The listener works (opens the nav drawer) when I call an element which is not in the include file but on the fragment layout – Francislainy Campos Jul 18 '17 at 19:43
  • Very well explained. – Ravinder Bhandari Jul 18 '17 at 19:44
  • Check of onClick() gets called by adding a log inside it. – Ravinder Bhandari Jul 18 '17 at 19:55
  • I think I've figured out what was causing the problem. My include was placed within a NestedScrollView but before a ConstraintLayout within the fragment's layout. Its elements were then behind the fragment's, and I believe that's why the listener wasn't being called. Just changed its position and it works now. Thanks for the help! :) – Francislainy Campos Jul 19 '17 at 06:15
0

Put a id to the Layout and later access to the elemento.

View myLayout = findViewById( R.id.layout ); 
View myView = myLayout.findViewById( R.id.item );
josedlujan
  • 5,357
  • 2
  • 27
  • 49
0

Change:

getLayoutPosition()

For

getAdapterPosition()
Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167