title: How do I add a GestureOverlayView programmatically?
body: <p>This is likely to be a very novice question, but I'm having trouble adding a GestureOverlayView programmatically.  I have a basic hello world app going that I'm experimenting with before merging with my real app.  Frankly I'm a bit over my head since this will be my first Android app.</p>
  
  <p>Anyways, getting along with the code, my fragment_main.xml looks like this:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/relative_layout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context="pha.viz.gesturetest.MainActivity$PlaceholderFragment" &gt;
  
   &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/hello_world" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>And my MainActivity.java right now is:</p>
  
  <pre><code>public class MainActivity extends ActionBarActivity {
      private GestureLibrary mLibrary;
      RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.relative_layout);
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.fragment_main);
  
  
  
          mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
          mLibrary.load();
          GestureOverlayView gestures = new GestureOverlayView(this);
          gestures.setGestureStrokeAngleThreshold(90.0f);
          gestures.addOnGesturePerformedListener(handleGestureListener);
  
          mRelativeLayout.addView(gestures);
  
          Display display = getWindowManager().getDefaultDisplay(); 
  
          LayoutParams params = gestures.getLayoutParams();
          params.width = display.getWidth() / 2;
          gestures.setLayoutParams(params);
          gestures.setTranslationX(0);
          }
  
      private OnGesturePerformedListener handleGestureListener = new
              OnGesturePerformedListener() {
          @Override
          public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
              ArrayList&lt;Prediction&gt; predictions = mLibrary.recognize(gesture);
              // one prediction needed
              if (predictions.size() &gt; 0) {
                  Prediction prediction = predictions.get(0);
                  // checking prediction
                  if (prediction.score &gt; 1.0) {
                      // and action
                      Toast.makeText(getApplicationContext(), prediction.name,Toast.LENGTH_SHORT).show();
                  }
              }       
          }
      };
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
  
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      /**
       * A placeholder fragment containing a simple view.
       */
      public static class PlaceholderFragment extends Fragment {
  
          public PlaceholderFragment() {
          }
  
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState) {
              View rootView = inflater.inflate(R.layout.fragment_main, container,
                      false);
              return rootView;
          }
      }
  
  }
  </code></pre>
  
  <p>The issue is that it's going into a crash.  Admittedly I'm following the same steps I would with a TextView, and I assumed it would work, so I may be missing something quite obvious.</p>
  
  <p>I am also aware the the code works if I am manually adding the GestureOverlayView onto the fragment_main.xml and just referring to it within the MainActivity, but the moment I try to change it to create a "new" GestureOverlayView in the code and add it to my RelativeLayout, it crashes.</p>
  
  <p>Thank you for reading all this, I appreciate any help or insight you may give me.</p>
  
  <p>PS.  I thought it might interest some of you as to WHY I am going through the hassle of doing this programmatically rather than through the XML file.  The reason is that I am trying to recognize a large number of gestures rapidly.  I found with another test file that having a single GestureOverlayView was slow, you would recognize them one at a time.  However, side by side, they would be able to create predictions simultaneously, giving the illusion of "faster".  With a bit of work, I am hoping to create additional GestureOverlayViews on touch, and destroy them once the command is complete.  But I couldn't even get through the obvious in this simple test, which is the creation of the view.</p>