Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answers provided by <em>cant0na</em> and <em>Juhani</em> are most likely the answers you are looking for (with a small note on <em>cant0na</em>'s answer). If you need a more self-maintained and fault tolerant solution you can define your very own "widget" which handles its own events. For this you'll need:</p> <ul> <li>A xml-layout file which will describe your header.</li> <li>A custom class which will (automatically) inflate the above XML layout and manage any "common events".</li> </ul> <p>The benefit of this solution is that you don't have to add a new instance of your common OnClickListener in each and every activity which will show your header. You simply add your header to your activitys layout-XML (see example code below) and nothing else. Foolproof. You also get a more "decoupled" code this way (your header doesn't depend on any implementation specifics of your application and its activities).</p> <p>The drawback is that it's a more complex solution and it might seem a bit over-kill for small projects. It's also a bit tricky to keep this solution "decoupled" if you want to do any activity specific actions on the button click. You might want to consider "default behaviour" in combination with "code injection" in the <code>MyHeader</code> class. The code injection would then require further manipulation on the header class (inject the onClick implementation) in the activities which deviates from the default behaviour.</p> <p><em>Example header.xml</em></p> <pre><code>&lt;com.dbm.widget.MyHeader android:layout_width="match_parent" android:layout_height="wrap_content" /&gt; &lt;ImageView android:layout_width="60dp" android:layout_height="20dp" android:src="@drawable/myIcon" android:id="@+id/myButton" /&gt; &lt;/com.dbm.widget.MyHeader&gt; </code></pre> <p><em>Example MyHeader.java</em></p> <pre><code>package com.dbm.widget; public class MyHeader extends LinearLayout implements OnClickListener { // Constructor. public MyButton() { ((ImageButton) findViewById(R.id.myButton)).setOnClickListener(this); } // OnClick event callback. public void onClick(View view) { // Do whatever you need to do here. } } </code></pre> <p><em>Example activity.xml</em></p> <pre><code>&lt;LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"&gt; &lt;com.dbm.MyHeader android:layout_width="match_parent" android:layout_height="20dp" /&gt; &lt;!-- Your other content goes here --&gt; &lt;/LinearLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload