title: Add a button to View
body: <p>I'm drawing a graph then add some button/label/textbox for input data. But the button on main layout is't show up. how to add button to a View (Because i draw graph using canvas on a View)</p>
  
  <p>here is draw canvas class</p>
  
  <pre><code>public class DrawCross extends View {
      Paint   paint   = new Paint();
  
      public DrawCross(Context context) {
          super(context);
          paint.setColor(Color.BLACK);
          Button btn = new Button(context);
      }
  
      @Override
      public void onDraw(Canvas canvas){
          canvas.drawLine(250, 450, 250, 20, paint);
          canvas.drawLine(245, 25, 250, 20, paint);
          canvas.drawLine(250, 20, 255, 25, paint);
  
          canvas.drawLine(20, 300, 400, 300, paint);
          canvas.drawLine(395, 295, 400, 300, paint);
          canvas.drawLine(400, 300, 395, 305, paint); 
      }
  }
  </code></pre>
  
  <p>this is main class</p>
  
  <pre><code>public class MainActivity extends Activity {
      DrawCross   drawView;
  
      @Override
      protected void onCreate(Bundle savedInstanceState){
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          drawView = new DrawCross(this);
          setContentView(drawView);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu){
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  }
  </code></pre>
  
  <p>Fixed:</p>
  
  <p>DrawCross class:</p>
  
  <pre><code>public class DrawCross extends View {
      Paint   paint   = new Paint();
  
      public DrawCross(Context context) {
          super(context);
      }
  
      @Override
      public void onDraw(Canvas canvas){
          canvas.drawLine(250, 450, 250, 20, paint);
          canvas.drawLine(245, 25, 250, 20, paint);
          canvas.drawLine(250, 20, 255, 25, paint);
          canvas.drawLine(20, 300, 400, 300, paint);
          canvas.drawLine(395, 295, 400, 300, paint);
          canvas.drawLine(400, 300, 395, 305, paint);
      }
  }
  </code></pre>
  
  <p>main class:</p>
  
  <pre><code>public class MainActivity extends Activity {
      DrawCross   drawView;
  
      @Override
      protected void onCreate(Bundle savedInstanceState){
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu){
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  }
  </code></pre>
  
  <p>activity_main layout:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      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=".MainActivity" &gt;
  
      &lt;LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="horizontal" &gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="a" /&gt;
  
          &lt;EditText
              android:layout_width="20dp"
              android:layout_height="wrap_content" /&gt;
  
          &lt;Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="draw" /&gt;
  
      &lt;/LinearLayout&gt;
      &lt;com.example.khibong.DrawCross
          android:id="@+id/myView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Manifest</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.khibong"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="17" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.example.khibong.MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>and the logcat:</p>
  
  <pre><code>05-12 12:56:16.831: E/Trace(1715): error opening trace file: No such file or directory (2)
  05-12 12:56:16.841: D/AndroidRuntime(1715): Shutting down VM
  05-12 12:56:16.841: W/dalvikvm(1715): threadid=1: thread exiting with uncaught exception (group=0xb3e28288)
  05-12 12:56:16.841: E/AndroidRuntime(1715): FATAL EXCEPTION: main
  05-12 12:56:16.841: E/AndroidRuntime(1715): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.khibong/com.example.khibong.MainActivity}: android.view.InflateException: Binary XML file line #31: Error inflating class com.example.khibong.DrawCross
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at android.os.Handler.dispatchMessage(Handler.java:99)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at android.os.Looper.loop(Looper.java:137)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at android.app.ActivityThread.main(ActivityThread.java:4745)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at java.lang.reflect.Method.invokeNative(Native Method)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at java.lang.reflect.Method.invoke(Method.java:511)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at dalvik.system.NativeStart.main(Native Method)
  05-12 12:56:16.841: E/AndroidRuntime(1715): Caused by: android.view.InflateException: Binary XML file line #31: Error inflating class com.example.khibong.DrawCross
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at android.view.LayoutInflater.createView(LayoutInflater.java:596)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at android.app.Activity.setContentView(Activity.java:1867)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at com.example.khibong.MainActivity.onCreate(MainActivity.java:18)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at android.app.Activity.performCreate(Activity.java:5008)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     ... 11 more
  05-12 12:56:16.841: E/AndroidRuntime(1715): Caused by: java.lang.NoSuchMethodException: &lt;init&gt; [class android.content.Context, interface android.util.AttributeSet]
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at java.lang.Class.getConstructor(Class.java:431)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     at android.view.LayoutInflater.createView(LayoutInflater.java:561)
  05-12 12:56:16.841: E/AndroidRuntime(1715):     ... 22 more
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/Ms7nH.jpg" alt="enter image description here"></p>