title: How to access an element from an <include> layout file which is inside not an activity but a fragment?
body: <p>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? </p>
  
  <p>Most posts tell me to do something like that:
  <a href="https://stackoverflow.com/questions/4787008/how-to-access-button-inside-include-layout">How to access Button inside &quot;include&quot; layout</a> but as I am not on an activity the listener for the image won't work.</p>
  
  <p>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.</p>
  
  <p>That's what I have inside the onCreateView method for my fragment:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>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();
              }
          });</code></pre>
  </div>
  </div>
  </p>
  
  <p>That's my include layout file:</p>
  
  <pre><code>&lt;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"&gt;
  
  &lt;ImageView
      android:id="@+id/image_left_top_bar"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/hamburger_icon" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>And that's how I'm including it within the fragment layout file:</p>
  
  <pre><code> &lt;include layout="@layout/top_bar" /&gt;
  </code></pre>
  
  <p>Thanks very much for any help on that! :)</p>