title: How do I get the OnClickListener working on a custom view with a RelativeLayout?
body: <h1>Question</h1>
  
  <p>Hello,</p>
  
  <p>i have built a custom view with a <code>RelativeLayout</code> to use it as custom image button.
  So far, selecting it works (PIC2) and even when I click it (using GoogleTV Remote), the view successfully changes it's state to PIC3 (thanks to <code>android:duplicateParentState="true"</code>)</p>
  
  <p>But unfortunately the <code>onClickListener</code> does not fire (doesn't matter if I click the View with the Remote "OK" Button or I use the touchpad..)</p>
  
  <p>I really need the same behavior like a normal button.
  How to accomplish that? I already spent a few hours on searching Google and StackOverflow... (BTW. when setting <code>android:clickable="false"</code> for the <code>RelativeLayout</code>, the <code>OnClickListener</code> is working, but only when I use the mouse pointer (Touchpad) and afterwards the focus is lost and the state (pic 3) is not displayed)</p>
  
  <h1>Pictures</h1>
  
  <h2>PIC1</h2>
  
  <p><img src="https://i.stack.imgur.com/ruyfN.png" alt="Custom View Normal"></p>
  
  <h2>PIC2</h2>
  
  <p><img src="https://i.stack.imgur.com/Ai710.png" alt="Custom View Focused"></p>
  
  <h2>PIC3</h2>
  
  <p><img src="https://i.stack.imgur.com/7dW4f.png" alt="Custom View Clicked"></p>
  
  <h1>Code</h1>
  
  <h2>rounded_button.xml</h2>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:clickable="true"
  android:focusable="true"
  android:focusableInTouchMode="false"&gt;
  
  &lt;TextView
      android:id="@+id/caption"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerVertical="true"
      android:layout_marginLeft="30dp"
      android:background="@drawable/btn_rounded_corners"
      android:paddingLeft="25dp"
      android:textSize="15sp" 
      android:duplicateParentState="true"/&gt;
  
  &lt;ImageView
      android:id="@+id/icon"
      style="@style/Menu_Button"
      android:layout_width="50dp"
      android:layout_height="50dp"
      android:layout_marginRight="-50dp"
      android:layout_toLeftOf="@id/caption"
      android:background="@drawable/btn_main_menu_back_shape"
      tools:ignore="ContentDescription"
      android:duplicateParentState="true" /&gt;
  </code></pre>
  
  <p></p>
  
  <h2>RoundedButton.java</h2>
  
  <pre><code>public class RoundedButton extends RelativeLayout {
  private String label;
  private int icon;
  
  /**
   * @param context
   */
  public RoundedButton(Context context)
  {
      super(context);
      initAttributes(context, null);
  }
  
  /**
   * @param context
   * @param attrs
   */
  public RoundedButton(Context context, AttributeSet attrs)
  {
      super(context, attrs);
      initAttributes(context, attrs);
  }
  
  /**
   * @param context
   * @param attrs
   * @param defStyle
   */
  public RoundedButton(Context context, AttributeSet attrs, int defStyle)
  {
      super(context, attrs, defStyle);
      initAttributes(context, attrs);
  }
  
  private void initAttributes(Context context, AttributeSet attrs)
  {
      LayoutInflater.from(context).inflate(R.layout.rounded_button, this, true);
  
      TypedArray a = 
          context.obtainStyledAttributes(attrs, R.styleable.RoundedButton);
  
      final int N = a.getIndexCount();
      for (int i = 0; i &lt; N; ++i)
      {
          int attr = a.getIndex(i);
          switch (attr)
          {
              case R.styleable.RoundedButton_text:
                  setLabel(a.getString(attr));
                  break;
              case R.styleable.RoundedButton_icon:
                  setIcon(a.getResourceId(attr, 0));
                  break;
          }
      }
      a.recycle();
  }
  
  public String getLabel()
  {
      return this.label;
  }
  
  public void setLabel(final String label)
  {
      this.label = label;
      ((TextView)findViewById(R.id.caption)).setText(this.label);
  }
  
  /**
   * @return the icon
   */
  public int getIcon()
  {
      return icon;
  }
  
  /**
   * @param icon the icon to set
   */
  public void setIcon(int icon)
  {
      this.icon = icon;
      ((ImageView)findViewById(R.id.icon)).setImageResource(this.icon);
  }
  }
  </code></pre>
  
  <h2>Relevant part of activity_main.xml</h2>
  
  <pre><code>&lt;eu.test.custom_views.RoundedButton
      android:id="@+id/custombutton"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      custom:icon="@drawable/hand_icon_green_left"
      custom:text="Normal state" /&gt;
  </code></pre>
  
  <h2>Main Activity</h2>
  
  <pre><code>public class MainActivity extends Activity implements OnClickListener {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      ((RoundedButton) findViewById(R.id.custombutton)).setOnClickListener(this);
  }
  
  @Override
  public void onClick(View arg0) {
      if(arg0.getId() == R.id.custombutton) { Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show(); }
  }
  }
  </code></pre>