title: Best way to get nested level Views without chaining getChildAt() repeatedly
body: <p>I've discovered I have a frequent usage pattern where I string together a chain of ugly <code>getChildAt(index)</code> calls. My code might look like this:</p>
  
  <p><strong>View Hierarchy:</strong></p>
  
  <pre><code>  LinearLayout
      LinearLayout
        EditText
        EditText
      /LinearLayout
      LinearLayout
        Button
        Button
      /LinearLayout
    /LinearLayout
  </code></pre>
  
  <p><strong>The ugly code:</strong></p>
  
  <pre><code>layout = (LinearLayout) inflater.inflate(R.layout.dialog_createbuddy, null);
  
  String number = 
    ((TextView) ((LinearLayout) layout.getChildAt(0)).getChildAt(1)).getText().toString();
  </code></pre>
  
  <p>Guided by my software development experience, this feels wrong.</p>
  
  <p><strong>1</strong>: The magic numbers have no readable semantics. It is not easily apparent that <code>0</code> is a <code>CheckBox</code> or a <code>TextView</code>, and <code>1</code> is some other widget, and these numbers probably have different meanings in different contexts (layouts).</p>
  
  <p><strong>2</strong>: If somebody were to ever change the layout, all the numbers might be invalidated</p>
  
  <p><strong>3</strong>: The chain of calls and casts is ugly and easy to screw up.</p>
  
  <h2>What is the correct replacement to this practice?</h2>
  
  <p>Here is a more complete code example:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" &gt;
  
    &lt;LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" &gt;
  
      &lt;EditText
        android:id="@+id/create_buddy_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="5"
        android:hint="Name" /&gt;
  
      &lt;EditText
        android:id="@+id/create_buddy_num"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="5"
        android:hint="Number"
        android:inputType="number|phone" /&gt;
  
    &lt;/LinearLayout&gt;
  
    &lt;LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal"
      style="?android:attr/buttonBarStyle" &gt;
  
      &lt;Button
        android:id="@+id/cancelBuddyBtn"
        android:text="Cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        style="?android:attr/buttonBarButtonStyle" /&gt;
  
      &lt;Button
        android:id="@+id/createBuddyBtn"
        android:text="Create"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        style="?android:attr/buttonBarButtonStyle" /&gt;
  
    &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>And then I have a DialogFragment:</p>
  
  <pre><code>public class CreateBuddyDialog extends DialogFragment 
  {
    ...
  
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    {
    ...
  
    layout = (LinearLayout) inflater.inflate(R.layout.dialog_createbuddy, null);
  
    Button button = (Button) ((LinearLayout) layout.getChildAt(1)).getChildAt(1);
  
    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) 
      {
        String name = 
          ((TextView) ((LinearLayout) layout.getChildAt(0)).getChildAt(0)).getText().toString();
  
        String number = 
          ((TextView) ((LinearLayout) layout.getChildAt(0)).getChildAt(1)).getText().toString();
  
        addBuddiesActivity.createBuddyConfirmed(new Buddy(name, number));
      }
  });
  </code></pre>