title: Acquire a custom view's xml id
body: <p>I have a custom view <code>ZoneSwitchItem</code> (extends LinearLayout) which I use in a fragment layout xml.</p>
  
  <p>From inside the custom view, I need to get the id that was assigned to it in the fragment xml. So I use <code>attrs.getIdAttribute();</code> but it returns <code>null</code> instead of the expected id <code>zone1</code>.</p>
  
  <p>I could add a custom attribute <code>ZoneSwitchItemId</code> but would like to avoid that if I could use the default id attribute. </p>
  
  <p>The usage in the fragment xml:</p>
  
  <pre><code>&lt;RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@color/white"&gt;
  
      &lt;com.android.common.ZoneSwitchItem
          android:id="@+id/zone1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="@dimen/cardPadding4x"
          android:gravity="center_horizontal"
          app:zoneItemStyle="ArmourFont_HeadlineBig" /&gt;
  
      ...
  </code></pre>
  
  <p>The custom view:</p>
  
  <pre><code>public class ZoneSwitchItem extends LinearLayout {
  
      private TextView itemValue;
      private TextView itemTitle;
  
      public ZoneSwitchItem(Context context) {
          super(context);
          init(context, null);
      }
  
      public ZoneSwitchItem(Context context, AttributeSet attrs) {
          super(context, attrs);
          init(context, attrs);
      }
  
      public ZoneSwitchItem(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
          init(context, attrs);
      }
  
      public void onStart() {
          EventBus.getDefault().register(this);
      }
  
      public void onStop() {
          EventBus.getDefault().unregister(this);
      }
  
      private void init(final Context context, AttributeSet attrs) {
          inflate(getContext(), R.layout.zone_switch_item, this);
  
          itemValue = findViewById(R.id.itemValue);
          itemTitle = findViewById(R.id.itemTitle);
  
          if (attrs != null) {
              TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ZoneSwitchItem, 0, 0);
              try {
                  if (typedArray.getString(R.styleable.ZoneSwitchItem_zoneItemStyle) != null &amp;&amp;
                          typedArray.getString(R.styleable.ZoneSwitchType_zoneItemImage).equals("ArmourFont_HeadlineBig")) {
                      itemValue.setTextAppearance(context, R.style.ArmourFont_HeadlineBig);
                  }
              } finally {
                  typedArray.recycle();
              }
          }
  
          final String id = attrs.getIdAttribute(); // &lt;&lt;== returns null
          itemValue.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  EventBus.getDefault().post(new OnZoneSwitchItemClickedEvent(id));
              }
          });
      }
  
      public void setSelected(boolean selected) {
          float alpha;
  
          if (selected) {
              alpha = 1.0f;
          } else {
              alpha = 0.5f;
          }
  
          itemValue.setAlpha(alpha);
          itemTitle.setAlpha(alpha);
      }
  }
  </code></pre>