Click here to Skip to main content
15,898,979 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to android.I want to know how to create button programatically.


Java
public class SamActivity extends Activity
{
	  private Paint       mPaint;
	    private MaskFilter  mEmboss;
	    private MaskFilter  mBlur;
	    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
        
	  view();
	  
        
    }
   
  
   
   
    public class MyView extends ImageView {

    	

        private Bitmap  mBitmap;
        private Canvas  mCanvas;
        private Path    mPath;
        private Paint   mBitmapPaint;
        
        public MyView(Context c) {
            super(c);

            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);
            
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);
        }
        
        @Override
        protected void onDraw(Canvas canvas) 
        {
        	mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
           
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(12);
mPaint.setColor(Color.RED);
            mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
                                           0.4f, 6, 3.5f);

            mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
           
            Bitmap mBitmap1= BitmapFactory.decodeResource(getResources(), R.drawable.base);
            
            Bitmap bMapScaled = Bitmap.createScaledBitmap(mBitmap1, 10, 10, true);
        	canvas.drawBitmap(bMapScaled, 0,0, mPaint);
            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
            canvas.drawPath(mPath, mPaint);
            super.onDraw(canvas);
            invalidate();
        }

        private float mX, mY;
        private static final float TOUCH_TOLERANCE = 4;

        private void touch_start(float x, float y)
        {
        	
            mPath.reset();
            mPath.moveTo(x, y);
            mX = x;
            mY = y;
        }
        private void touch_move(float x, float y) {
            float dx = Math.abs(x - mX);
            float dy = Math.abs(y - mY);
            if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
                mX = x;
                mY = y;
            }
        }
        private void touch_up() {
            mPath.lineTo(mX, mY);
            // commit the path to our offscreen
            mCanvas.drawPath(mPath, mPaint);
            // kill this so we don't double draw
            mPath.reset();
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) 
        {
        	
        	
            float x = event.getX();
            float y = event.getY();

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    touch_start(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_MOVE:
                    touch_move(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    touch_up();
                    invalidate();
                    break;
            }
            return true;
        }
        
        
    }
   public void view()
   {
	   
	   MyView mp = new MyView(this);
	   setContentView(mp);
	  
   }
  
}




This is my code.In this i draw a line on the image.But i cannot create a button in this.please help me.
Posted
Updated 6-Oct-11 22:23pm
v2
Comments
Sudhakar Shinde 27-Mar-13 4:34am    
Read below mentioned link and try.
http://developer.android.com/reference/android/widget/Button.html

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900