title: Add ImageView dynamically to View
body: <p>I am making an Android game which uses the View class and I am not using an XML-layout.</p>
  <p>All my images are drawn with canvas. Now my problem is the I cannot use a bitmap.</p>
  <p>I am trying to add an ImageView dynamically to my View class, to use the touchable event.</p>
  <p>Why dynamically? Because I could not use XML-layout.<br />
  Here is my code:</p>
  <pre><code>package com.example.poolmaster;
  
  import java.util.ArrayList;
  
  import android.annotation.SuppressLint;
  import android.app.Activity;
  import android.content.Context;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.graphics.Canvas;
  import android.graphics.Matrix;
  import android.graphics.drawable.BitmapDrawable;
  import android.graphics.drawable.Drawable;
  import android.os.Bundle;
  import android.util.DisplayMetrics;
  import android.view.Gravity;
  import android.view.Menu;
  import android.view.MotionEvent;
  import android.view.View;
  import android.view.View.OnTouchListener;
  import android.widget.ImageView;
  import android.widget.LinearLayout;
  
  public class MainActivity extends Activity {
      private int cuePosx, cuePosy;
  
      int cueHeight, cueWeight;
      double rotatingAngle;
      int height;
      int wwidth;
      int cueHeight2, cueWeight2;
      Bitmap table, stick, raise;
  
      ImageView button = new ImageView(this);
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      
          rotatingAngle = 0;
          DisplayMetrics displaymetrics = new DisplayMetrics();
          getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
          height = displaymetrics.heightPixels;
          wwidth = displaymetrics.widthPixels;
          cueHeight2 = height / 2;
          cueWeight2 = wwidth / 2;
          System.out.println(&quot;**************************************&quot;);
          System.out.println(&quot;height &quot; + height);
          System.out.println(&quot;weight&quot; + wwidth);
          System.out.println(&quot;**************************************&quot;);
        
          // Set generic layout parameters
          GamePlay custom = new GamePlay(this);
  
          setContentView(custom);
         // setContentView(R.layout.activity_main);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.activity_main, menu);
          return true;
      }
  
      @Override
      public boolean onTouchEvent(MotionEvent ev) {
          if (ev.getAction() == MotionEvent.ACTION_MOVE){
              // cuePosy += 10;
              // System.out.println(&quot;xCORDİNATES!!!! &quot; +ev.getX());
              // System.out.println(&quot;yCORDİNATES!!!! &quot; +ev.getY());
              rotatingAngle=getAngle(ev.getX(), ev.getY());
              System.out.println(&quot;Angle &quot; +rotatingAngle);
          }
          if (ev.getAction()==MotionEvent.ACTION_DOWN){
              System.out.println(&quot;****************** ACTİON DOWN ****************&quot;);
              // cueHeight2 += 10;
              // cueWeight2 += 20;
              // cuePosy = 320;
          }
          if (ev.getAction()==MotionEvent.ACTION_UP){
              System.out.println(&quot;****************** ACTİON DOWN ****************&quot;);
              // cueHeight2 -= 10;
              // cueWeight2 -= 20;
              // cuePosy = 320;
          }
          return true;
      }
      private double getAngle(double xTouch, double yTouch) {
          double theta;
          
          theta = Math.toDegrees(Math.atan2(height / 2 - yTouch, xTouch - wwidth / 2));
          return theta;
      }
      
      public class GamePlay extends View {
          public GamePlay(Context context) {
              super(context);
              // TODO Auto-generated constructor stub
              table = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
              table = Bitmap.createScaledBitmap(table, wwidth, height, true);
              stick = BitmapFactory.decodeResource(getResources(), R.drawable.stick);
  
              raise = BitmapFactory.decodeResource(getResources(), R.drawable.raise);
              cueHeight = stick.getHeight();
              System.out.println(&quot;ıstaka &quot; + cueHeight);
              cueWeight = stick.getWidth();
              cuePosx = wwidth / 2;
              cuePosy = height - cueHeight - 180;
          }
          @SuppressLint({ &quot;DrawAllocation&quot;, &quot;DrawAllocation&quot; })
          @Override
          public void onDraw(Canvas canvas) {
              super.onDraw(canvas);
  
              Matrix matrix = new Matrix();
              matrix.setTranslate(cueWeight2, cueHeight2);
              matrix.postRotate((float)rotatingAngle, cueWeight2, cueHeight2); // anti-clockwise by 90 degrees
                 
              // create a new bitmap from the original using the matrix to transform the result
              Bitmap rotatedBitmap = Bitmap.createBitmap(stick, 0, 0, stick.getWidth(), stick.getHeight(), matrix, false);
                 
              canvas.save(); // Save the position of the canvas.
              canvas.restore();
                 
              // Rotate the canvas.
              canvas.drawBitmap(table, 0, 0, null); // Draw the ball on the rotated canvas.
              canvas.drawBitmap(stick, matrix, null);
              canvas.drawBitmap(raise, 0, 0, null);
              invalidate();
          }
      }
  }
  </code></pre>