title: Creating a custom view and reusing the same view
body: <p>I have a Table layout which consists of five table rows. Each table row have a custom view which renders some graph and a custom textview. My Layout looks like following</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"&gt;
  &lt;TableRow
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"&gt;
  
      &lt;com.company.views.FirstView
          android:id="@+id/firstView"
          android:layout_height="match_parent"
          android:layout_width="0dp"
          android:layout_weight=".80"
          android:background="#000000" /&gt;
      &lt;com.company.views.FirstTextView
          android:id="@+id/firstTextView"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight=".20"
          android:background="#000000"
          android:textColor="#00FF00"
          android:gravity="center"
          android:textStyle="bold|italic"
          android:textSize="40sp"
          android:inputType="number"
          android:digits="1234567890"
          android:cursorVisible="false"
          android:scrollbars = "vertical"/&gt;
  &lt;/TableRow&gt;
  &lt;TableRow
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"&gt;
      &lt;com.company.views.SecondView
          android:id="@+id/secondView"
          android:layout_height="match_parent"
          android:layout_width="0dp"
          android:layout_weight=".80"
          android:background="#000000" /&gt;
      &lt;com.company.views.SecondTextView
          android:id="@+id/secondTextView"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight=".20"
          android:background="#000000"
          android:textColor="#FFFF00"
          android:gravity="center"
          android:textStyle="bold|italic"
          android:textSize="40sp"
          android:inputType="number"
          android:digits="1234567890"
          android:cursorVisible="false"
          android:scrollbars = "vertical"/&gt;
  &lt;/TableRow&gt;
  &lt;/TableLayout&gt;
  </code></pre>
  
  <p>The above code is working (I have taken some code out for the sake of explanation) but the problem is that my firstview and secondview looks very similar in functionality and code and the same goes for my firstTextView and secondTextView.</p>
  
  <p>The question is instead of having five different custom views I want only one custom view and reuse it so the layout looks like following: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
  android:layout_height="match_parent"&gt;
  &lt;TableRow
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"&gt;
  
      &lt;com.company.views.FirstView
          android:id="@+id/firstView"
          android:layout_height="match_parent"
          android:layout_width="0dp"
          android:layout_weight=".80"
          android:background="#000000" /&gt;
      &lt;com.company.views.FirstTextView
          android:id="@+id/firstTextView"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight=".20"
          android:background="#000000"
          android:textColor="#00FF00"
          android:gravity="center"
          android:textStyle="bold|italic"
          android:textSize="40sp"
          android:inputType="number"
          android:digits="1234567890"
          android:cursorVisible="false"
          android:scrollbars = "vertical"/&gt;
  &lt;/TableRow&gt;
  &lt;TableRow
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"&gt;
      &lt;com.company.views.FirstView
          android:id="@+id/secondView"
          android:layout_height="match_parent"
          android:layout_width="0dp"
          android:layout_weight=".80"
          android:background="#000000" /&gt;
      &lt;com.company.views.FirstTextView
          android:id="@+id/secondTextView"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight=".20"
          android:background="#000000"
          android:textColor="#FFFF00"
          android:gravity="center"
          android:textStyle="bold|italic"
          android:textSize="40sp"
          android:inputType="number"
          android:digits="1234567890"
          android:cursorVisible="false"
          android:scrollbars = "vertical"/&gt;
  &lt;/TableRow&gt;
  &lt;/TableLayout&gt;
  </code></pre>
  
  <p>The Problem is that when from my firstTextView java class i do the following</p>
  
  <pre><code>private void init(AttributeSet attrs, int defStyle) {
      textView = (TextView)findViewById(R.id.firstTextView);
      textView.setText("My Awesome Text");;
  }
  </code></pre>
  
  <p>the code breaks with an inflate exception. I am stuck here as I don't know what I am doing wrong. Is there any other way to achieve the desired outcome. Or am I missing something obvious.</p>
  
  <p>My FirstTextView looks like following</p>
  
  <pre><code>public class FirstTextView extends AppCompatTextView {
  private int spinnerText=120;
  private float oldY=0;
  private TextView textView;
  private Context context;
  private PopupWindow mPopupWindow;
  
  public FirstTextView(Context context) {
      super(context);
      this.context = context;
      init(null, 0);
  }
  
  public FirstTextView(Context context, AttributeSet attrs) {
      super(context, attrs);
      this.context = context;
      init(attrs, 0);
  
  }
  
  public FirstTextView(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      this.context = context;
      init(attrs, defStyle);
  }
  
  private void init(AttributeSet attrs, int defStyle) {
      //cant do that as the findviewbyid return null
      textView = (TextView)findViewById(R.id.firstView);
      textView.setOnTouchListener(handleTouch);
      // Here i want to find which textview firstView or SecondView. 
      //if(firstview)
         //textView.setText(""+Physiology.getInstance().Model1.getValue());
      //else 
        //textView.setText(""+Physiology.getInstance().Model2.getValue());  
       textView.setText(""+Physiology.getInstance().Model2.getValue());
  }
  
  @Override
  public void setText(CharSequence text, BufferType type) {
      String newText="";
      if(text.length()&gt;0){
          newText=String.valueOf(text);
          int textasNumber=Integer.valueOf(newText);
          Physiology.getInstance().mBloodPressure.setSystolic(textasNumber);
          newText= Physiology.getInstance().mBloodPressure.getSystolic() + "/" +  (int)Physiology.getInstance().mBloodPressure.mDiastolic;
      }
  
      super.setText(newText,type);
  }
  
  public void onTextViewClick(View v) {
      // Initialize a new instance of LayoutInflater service
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
  
      // Inflate the custom layout/view
      NumberPadView customView = (NumberPadView) inflater.inflate(R.layout.number_popup,null);
  
      // Initialize a new instance of popup window
      mPopupWindow = new PopupWindow(
              customView,
              RelativeLayout.LayoutParams.WRAP_CONTENT,
              RelativeLayout.LayoutParams.WRAP_CONTENT
      );
  
      customView.init(Physiology.getInstance().mBloodPressure.getSystolic(),mPopupWindow,this);
  
      // Finally, show the popup window at the center location of root
      mPopupWindow.showAtLocation(v, Gravity.CENTER,0,0);
  }
    }
  </code></pre>